blob: 27120baf1ff3fd901c17abf3cc31d8de8d470e67 [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>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
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 */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust26d36302016-09-22 13:39:05 -0400331static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
334{
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336
337 ops->test_and_free_expired(server, stateid, cred);
338}
339
340static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
343{
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
346}
347
348static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
351{
352 nfs4_stateid tmp;
353
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
356}
357
NeilBrown8478eaa2014-09-18 16:09:27 +1000358static long nfs4_update_delay(long *timeout)
359{
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
370}
371
Trond Myklebust65de8722008-12-23 15:21:44 -0500372static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
373{
374 int res = 0;
375
376 might_sleep();
377
NeilBrown8478eaa2014-09-18 16:09:27 +1000378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 return res;
383}
384
385/* This is the error handling routine for processes that are allowed
386 * to sleep.
387 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400388static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500390{
391 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500392 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400393 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500394 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 int ret = errorcode;
396
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400397 exception->delay = 0;
398 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400400
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
403
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 switch(errorcode) {
405 case 0:
406 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
415 }
416 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400417 if (inode) {
418 int err;
419
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
427 }
428 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500429 if (state == NULL)
430 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500436 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500447#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400458 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500459#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
464 */
465 ret = -EBUSY;
466 break;
467 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400471 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400472 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400473 exception->delay = 1;
474 return 0;
475
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
491 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500492 }
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500495wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400496 exception->recovering = 1;
497 return 0;
498}
499
500/* This is the error handling routine for processes that are allowed
501 * to sleep.
502 */
503int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504{
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
507
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
512 }
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
518 }
519 return ret;
520out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500524}
525
Trond Myklebust037fc982015-09-20 15:51:00 -0400526static int
527nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
529{
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
532
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
537 }
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
543 }
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
551}
552
553static int
554nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
556{
557 struct nfs4_exception exception = {
558 .state = state,
559 };
560
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
573}
574
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400575/*
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
578 */
579static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580{
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
586
587 return false;
588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Peng Taocb04ad22014-06-11 05:24:15 +0800627int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400631{
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 struct nfs4_slot *slot;
633
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
637
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
641
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust0a014a42016-09-22 13:38:51 -0400650 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400651 args->sa_slot = slot;
652 res->sr_slot = slot;
653
654out_start:
655 rpc_call_start(task);
656 return 0;
657
658out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
666}
Peng Taocb04ad22014-06-11 05:24:15 +0800667EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400668
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400669static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400670{
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
673
Chuck Lever3bd23842013-08-09 12:49:19 -0400674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
679
680 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400681}
682
683static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
685{
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400688 return 1;
689}
690
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691#if defined(CONFIG_NFS_V4_1)
692
Trond Myklebustd185a332010-06-16 09:52:25 -0400693static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400696 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500698 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400699
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500702
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
707
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500708 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
711 */
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
714
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500716 send_new_highest_used_slotid = false;
717 goto out_unlock;
718 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500719 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500720
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500723out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500724 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500726 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400727 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400730}
731
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400732static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400734{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500738 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500739 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400740
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500741 if (slot == NULL)
742 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400745 goto out;
746
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500747 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500748
Trond Myklebustac20d162012-12-15 15:36:07 -0500749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
752 }
753
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500755 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400756 switch (res->sr_status) {
757 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400758 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400759 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500760 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500761 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500762 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400763 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
764 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500765 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400766 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500767 case 1:
768 /*
769 * sr_status remains 1 if an RPC level error occurred.
770 * The server may or may not have processed the sequence
771 * operation..
772 * Mark the slot as having hosted an interrupted RPC call.
773 */
774 slot->interrupted = 1;
775 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 case -NFS4ERR_DELAY:
777 /* The server detected a resend of the RPC call and
778 * returned NFS4ERR_DELAY as per Section 2.10.6.2
779 * of RFC5661.
780 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500781 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500784 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400785 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500786 case -NFS4ERR_BADSLOT:
787 /*
788 * The slot id we used was probably retired. Try again
789 * using a different slot id.
790 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500791 goto retry_nowait;
792 case -NFS4ERR_SEQ_MISORDERED:
793 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500794 * Was the last operation on this sequence interrupted?
795 * If so, retry after bumping the sequence number.
796 */
797 if (interrupted) {
798 ++slot->seq_nr;
799 goto retry_nowait;
800 }
801 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 * Could this slot have been previously retired?
803 * If so, then the server may be expecting seq_nr = 1!
804 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500805 if (slot->seq_nr != 1) {
806 slot->seq_nr = 1;
807 goto retry_nowait;
808 }
809 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500810 case -NFS4ERR_SEQ_FALSE_RETRY:
811 ++slot->seq_nr;
812 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 default:
814 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400815 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816 }
817out:
818 /* The session may be reset by one of the error handlers. */
819 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500820out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500821 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500822retry_nowait:
823 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400824 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500825 task->tk_status = 0;
826 ret = 0;
827 }
828 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400829out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400830 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 goto out;
832 rpc_delay(task, NFS4_POLL_RETRY_MAX);
833 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400834}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835
836int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
837{
838 if (!nfs41_sequence_process(task, res))
839 return 0;
840 if (res->sr_slot != NULL)
841 nfs41_sequence_free_slot(res);
842 return 1;
843
844}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500845EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400846
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400847static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (res->sr_slot == NULL)
850 return 1;
851 if (res->sr_slot->table->session != NULL)
852 return nfs41_sequence_process(task, res);
853 return nfs40_sequence_done(task, res);
854}
855
856static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
857{
858 if (res->sr_slot != NULL) {
859 if (res->sr_slot->table->session != NULL)
860 nfs41_sequence_free_slot(res);
861 else
862 nfs40_sequence_free_slot(res);
863 }
864}
865
Peng Tao2c4b1312014-06-11 05:24:16 +0800866int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400867{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500868 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400869 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400870 if (!res->sr_slot->table->session)
871 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400872 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873}
Peng Tao2c4b1312014-06-11 05:24:16 +0800874EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400875
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000876int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400879 struct rpc_task *task)
880{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400881 struct nfs4_slot *slot;
882 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400883
884 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400885 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400887 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 tbl = &session->fc_slot_table;
890
Trond Myklebust69d206b2012-11-22 13:21:02 -0500891 task->tk_timeout = 0;
892
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400894 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400895 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500896 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500897 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400899 }
900
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500901 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500902 if (IS_ERR(slot)) {
903 /* If out of memory, try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400906 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400907 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400908 }
909 spin_unlock(&tbl->slot_tbl_lock);
910
Trond Myklebust0a014a42016-09-22 13:38:51 -0400911 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500912 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400913
Chuck Levere8d92382013-08-09 12:47:51 -0400914 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500915 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916
Benny Halevydfb4f3092010-09-24 09:17:01 -0400917 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500918 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400919 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400920 /*
921 * sr_status is only set in decode_sequence, and so will remain
922 * set to 1 if an rpc level failure occurs.
923 */
924 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400925 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400926out_success:
927 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400929out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400930 /* Privileged tasks are queued with top priority */
931 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400932 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
933 NULL, RPC_PRIORITY_PRIVILEGED);
934 else
935 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400936 spin_unlock(&tbl->slot_tbl_lock);
937 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000939EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940
Chuck Lever5a580e02013-08-09 12:48:09 -0400941static int nfs4_setup_sequence(const struct nfs_server *server,
942 struct nfs4_sequence_args *args,
943 struct nfs4_sequence_res *res,
944 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400946 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 int ret = 0;
948
Chuck Lever3bd23842013-08-09 12:49:19 -0400949 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800950 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
951 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400952
Chuck Levere8d92382013-08-09 12:47:51 -0400953 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400954 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400955 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400956
Trond Myklebust9d12b212012-01-17 22:04:25 -0500957 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400958
Andy Adamsonce5039c2009-04-01 09:22:13 -0400959 dprintk("<-- %s status=%d\n", __func__, ret);
960 return ret;
961}
962
Andy Adamsonce5039c2009-04-01 09:22:13 -0400963static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
964{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400965 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400966 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967
Trond Myklebust035168ab2010-06-16 09:52:26 -0400968 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
969
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400970 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971}
972
Andy Adamson69ab40c2009-04-01 09:22:19 -0400973static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400976
Trond Myklebust14516c32010-07-31 14:29:06 -0400977 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978}
979
Trond Myklebust17280172012-03-11 13:11:00 -0400980static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983};
984
Chuck Lever3bd23842013-08-09 12:49:19 -0400985#else /* !CONFIG_NFS_V4_1 */
986
Chuck Lever5a580e02013-08-09 12:48:09 -0400987static int nfs4_setup_sequence(const struct nfs_server *server,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 struct rpc_task *task)
991{
Peng Taocb04ad22014-06-11 05:24:15 +0800992 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
993 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400994}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400995
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400996static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997{
998 return nfs40_sequence_done(task, res);
999}
1000
1001static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002{
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1005}
1006
Peng Tao2c4b1312014-06-11 05:24:16 +08001007int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001009{
Chuck Lever3bd23842013-08-09 12:49:19 -04001010 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001011}
Peng Tao2c4b1312014-06-11 05:24:16 +08001012EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001013
1014#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001015
Chuck Lever9915ea72013-08-09 12:48:27 -04001016static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1017{
1018 struct nfs4_call_sync_data *data = calldata;
1019 nfs4_setup_sequence(data->seq_server,
1020 data->seq_args, data->seq_res, task);
1021}
1022
1023static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1024{
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_sequence_done(task, data->seq_res);
1027}
1028
1029static const struct rpc_call_ops nfs40_call_sync_ops = {
1030 .rpc_call_prepare = nfs40_call_sync_prepare,
1031 .rpc_call_done = nfs40_call_sync_done,
1032};
1033
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001034static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001035 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001039{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 int ret;
1041 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 struct nfs_client *clp = server->nfs_client;
1043 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001044 .seq_server = server,
1045 .seq_args = args,
1046 .seq_res = res,
1047 };
1048 struct rpc_task_setup task_setup = {
1049 .rpc_client = clnt,
1050 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .callback_data = &data
1053 };
1054
1055 task = rpc_run_task(&task_setup);
1056 if (IS_ERR(task))
1057 ret = PTR_ERR(task);
1058 else {
1059 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001060 rpc_put_task(task);
1061 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 return ret;
1063}
1064
Bryan Schumaker7c513052011-03-24 17:12:24 +00001065int nfs4_call_sync(struct rpc_clnt *clnt,
1066 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001067 struct rpc_message *msg,
1068 struct nfs4_sequence_args *args,
1069 struct nfs4_sequence_res *res,
1070 int cache_reply)
1071{
Chuck Levera9c92d62013-08-09 12:48:18 -04001072 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001073 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1077{
1078 struct nfs_inode *nfsi = NFS_I(dir);
1079
1080 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001081 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001082 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001084 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001085 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001086 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock(&dir->i_lock);
1088}
1089
1090struct nfs4_opendata {
1091 struct kref kref;
1092 struct nfs_openargs o_arg;
1093 struct nfs_openres o_res;
1094 struct nfs_open_confirmargs c_arg;
1095 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001096 struct nfs4_string owner_name;
1097 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001098 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001100 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001102 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct nfs4_state_owner *owner;
1104 struct nfs4_state *state;
1105 struct iattr attrs;
1106 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001107 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001108 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001109 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001110 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int cancelled;
1112};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001113
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001114static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1115 int err, struct nfs4_exception *exception)
1116{
1117 if (err != -EINVAL)
1118 return false;
1119 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1120 return false;
1121 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1122 exception->retry = 1;
1123 return true;
1124}
1125
Trond Myklebust6ae37332015-01-30 14:21:14 -05001126static u32
1127nfs4_map_atomic_open_share(struct nfs_server *server,
1128 fmode_t fmode, int openflags)
1129{
1130 u32 res = 0;
1131
1132 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1133 case FMODE_READ:
1134 res = NFS4_SHARE_ACCESS_READ;
1135 break;
1136 case FMODE_WRITE:
1137 res = NFS4_SHARE_ACCESS_WRITE;
1138 break;
1139 case FMODE_READ|FMODE_WRITE:
1140 res = NFS4_SHARE_ACCESS_BOTH;
1141 }
1142 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1143 goto out;
1144 /* Want no delegation if we're using O_DIRECT */
1145 if (openflags & O_DIRECT)
1146 res |= NFS4_SHARE_WANT_NO_DELEG;
1147out:
1148 return res;
1149}
1150
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001151static enum open_claim_type4
1152nfs4_map_atomic_open_claim(struct nfs_server *server,
1153 enum open_claim_type4 claim)
1154{
1155 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1156 return claim;
1157 switch (claim) {
1158 default:
1159 return claim;
1160 case NFS4_OPEN_CLAIM_FH:
1161 return NFS4_OPEN_CLAIM_NULL;
1162 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1163 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1165 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1166 }
1167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170{
1171 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001172 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 p->o_res.seqid = p->o_arg.seqid;
1174 p->c_res.seqid = p->c_arg.seqid;
1175 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001176 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001178 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179}
1180
Al Viro82a2c1b2011-06-22 18:30:55 -04001181static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001183 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001184 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001185 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001186 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187{
Al Viro82a2c1b2011-06-22 18:30:55 -04001188 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001189 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_opendata *p;
1193
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001194 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 if (p == NULL)
1196 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001197
1198 p->f_label = nfs4_label_alloc(server, gfp_mask);
1199 if (IS_ERR(p->f_label))
1200 goto err_free_p;
1201
Kinglong Meea49c2692015-07-27 15:31:38 +08001202 p->a_label = nfs4_label_alloc(server, gfp_mask);
1203 if (IS_ERR(p->a_label))
1204 goto err_free_f;
1205
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1207 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001208 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001209 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001210 nfs_sb_active(dentry->d_sb);
1211 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 p->dir = parent;
1213 p->owner = sp;
1214 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 p->o_arg.open_flags = flags;
1216 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001217 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1218 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1220 * will return permission denied for all bits until close */
1221 if (!(flags & O_EXCL)) {
1222 /* ask server to check for all possible rights as results
1223 * are cached */
1224 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1225 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 }
David Howells7539bba2006-08-22 20:06:09 -04001227 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001228 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1229 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001230 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001231 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001232 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001233 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001234 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1236 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1241 break;
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001246 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001248 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001249 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001250
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 p->o_arg.u.attrs = &p->attrs;
1252 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001253
1254 verf[0] = jiffies;
1255 verf[1] = current->pid;
1256 memcpy(p->o_arg.u.verifier.data, verf,
1257 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001259 p->c_arg.fh = &p->o_res.fh;
1260 p->c_arg.stateid = &p->o_res.stateid;
1261 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001265
1266err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001267 nfs4_label_free(p->a_label);
1268err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001269 nfs4_label_free(p->f_label);
1270err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 kfree(p);
1272err:
1273 dput(parent);
1274 return NULL;
1275}
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 struct nfs4_opendata *p = container_of(kref,
1280 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282
1283 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001284 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285 if (p->state != NULL)
1286 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001288
Kinglong Meea49c2692015-07-27 15:31:38 +08001289 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001290 nfs4_label_free(p->f_label);
1291
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 dput(p->dentry);
1294 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001295 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001296 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 kfree(p);
1298}
1299
1300static void nfs4_opendata_put(struct nfs4_opendata *p)
1301{
1302 if (p != NULL)
1303 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304}
1305
Trond Myklebust06f814a2006-01-03 09:55:07 +01001306static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1307{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001308 int ret;
1309
Trond Myklebust06f814a2006-01-03 09:55:07 +01001310 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001311 return ret;
1312}
1313
Trond Myklebust24311f82015-09-20 10:50:17 -04001314static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1315 fmode_t fmode)
1316{
1317 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1318 case FMODE_READ|FMODE_WRITE:
1319 return state->n_rdwr != 0;
1320 case FMODE_WRITE:
1321 return state->n_wronly != 0;
1322 case FMODE_READ:
1323 return state->n_rdonly != 0;
1324 }
1325 WARN_ON_ONCE(1);
1326 return false;
1327}
1328
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330{
1331 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001332
Trond Myklebust536e43d2012-01-17 22:04:26 -05001333 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 goto out;
1335 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001336 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001337 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1338 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 break;
1340 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001341 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1342 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 break;
1344 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001345 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1346 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 return ret;
1350}
1351
Trond Myklebust2a606182015-08-19 22:30:00 -05001352static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1353 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001355 if (delegation == NULL)
1356 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001359 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1360 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001361 switch (claim) {
1362 case NFS4_OPEN_CLAIM_NULL:
1363 case NFS4_OPEN_CLAIM_FH:
1364 break;
1365 case NFS4_OPEN_CLAIM_PREVIOUS:
1366 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1367 break;
1368 default:
1369 return 0;
1370 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001371 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 return 1;
1373}
1374
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001376{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001378 case FMODE_WRITE:
1379 state->n_wronly++;
1380 break;
1381 case FMODE_READ:
1382 state->n_rdonly++;
1383 break;
1384 case FMODE_READ|FMODE_WRITE:
1385 state->n_rdwr++;
1386 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001388}
1389
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001390#ifdef CONFIG_NFS_V4_1
1391static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1392{
1393 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1394 return true;
1395 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1396 return true;
1397 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1398 return true;
1399 return false;
1400}
1401#endif /* CONFIG_NFS_V4_1 */
1402
Trond Myklebust4f14c192014-02-12 19:15:06 -05001403static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001404{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001405 struct nfs_client *clp = state->owner->so_server->nfs_client;
1406 bool need_recover = false;
1407
1408 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1409 need_recover = true;
1410 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1411 need_recover = true;
1412 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1413 need_recover = true;
1414 if (need_recover)
1415 nfs4_state_mark_reclaim_nograce(clp, state);
1416}
1417
Trond Myklebuste999e802014-02-10 18:20:47 -05001418static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001419 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001420{
1421 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1422 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001423 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001424 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001425 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001426 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001427 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001428 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1429 return true;
1430 return false;
1431}
1432
Trond Myklebustf95549c2015-01-23 18:06:09 -05001433static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1434{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001435 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1436 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001437 if (state->n_wronly)
1438 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1439 if (state->n_rdonly)
1440 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1441 if (state->n_rdwr)
1442 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001443 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444}
1445
Trond Myklebust226056c2014-02-11 10:41:07 -05001446static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001447 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001448 nfs4_stateid *stateid, fmode_t fmode)
1449{
1450 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1451 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1452 case FMODE_WRITE:
1453 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 break;
1455 case FMODE_READ:
1456 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 break;
1458 case 0:
1459 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1461 clear_bit(NFS_OPEN_STATE, &state->flags);
1462 }
1463 if (stateid == NULL)
1464 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001465 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001466 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1467 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1468 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001469 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001470 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001471 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001472 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001473 nfs4_stateid_copy(&state->stateid, stateid);
1474 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001475}
1476
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001477static void nfs_clear_open_stateid(struct nfs4_state *state,
1478 nfs4_stateid *arg_stateid,
1479 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001480{
1481 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001482 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001483 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001484 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1485 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486}
1487
Trond Myklebust1393d962016-09-22 13:39:13 -04001488static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1489 const nfs4_stateid *stateid, fmode_t fmode,
1490 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001491{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001492 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001493 case FMODE_READ:
1494 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1495 break;
1496 case FMODE_WRITE:
1497 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1498 break;
1499 case FMODE_READ|FMODE_WRITE:
1500 set_bit(NFS_O_RDWR_STATE, &state->flags);
1501 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001502 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001503 return;
1504 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1505 nfs4_stateid_copy(&state->stateid, stateid);
1506 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001507}
1508
Trond Myklebust1393d962016-09-22 13:39:13 -04001509static void __update_open_stateid(struct nfs4_state *state,
1510 const nfs4_stateid *open_stateid,
1511 const nfs4_stateid *deleg_stateid,
1512 fmode_t fmode,
1513 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001515 /*
1516 * Protect the call to nfs4_state_set_mode_locked and
1517 * serialise the stateid update
1518 */
Andrew Elble361cad32015-12-02 09:20:57 -05001519 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001520 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001521 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001522 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001523 set_bit(NFS_DELEGATED_STATE, &state->flags);
1524 }
1525 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001526 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001527 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001528 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001529 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Trond Myklebust1393d962016-09-22 13:39:13 -04001532static int update_open_stateid(struct nfs4_state *state,
1533 const nfs4_stateid *open_stateid,
1534 const nfs4_stateid *delegation,
1535 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001536{
Trond Myklebust1393d962016-09-22 13:39:13 -04001537 struct nfs_server *server = NFS_SERVER(state->inode);
1538 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001539 struct nfs_inode *nfsi = NFS_I(state->inode);
1540 struct nfs_delegation *deleg_cur;
Trond Myklebust1393d962016-09-22 13:39:13 -04001541 nfs4_stateid freeme = {0};
Trond Myklebust34310432008-12-23 15:21:38 -05001542 int ret = 0;
1543
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001544 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001545
1546 rcu_read_lock();
1547 deleg_cur = rcu_dereference(nfsi->delegation);
1548 if (deleg_cur == NULL)
1549 goto no_delegation;
1550
1551 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001552 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001553 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001554 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001555 goto no_delegation_unlock;
1556
1557 if (delegation == NULL)
1558 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001559 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001560 goto no_delegation_unlock;
1561
Trond Myklebustb7391f42008-12-23 15:21:52 -05001562 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001563 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1564 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001565 ret = 1;
1566no_delegation_unlock:
1567 spin_unlock(&deleg_cur->lock);
1568no_delegation:
1569 rcu_read_unlock();
1570
1571 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001572 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001573 ret = 1;
1574 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001575 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001576 nfs4_schedule_state_manager(clp);
1577 if (freeme.type != 0)
1578 nfs4_test_and_free_stateid(server, &freeme,
1579 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001580
1581 return ret;
1582}
1583
Trond Myklebust39071e62015-01-24 15:07:56 -05001584static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1585 const nfs4_stateid *stateid)
1586{
1587 struct nfs4_state *state = lsp->ls_state;
1588 bool ret = false;
1589
1590 spin_lock(&state->state_lock);
1591 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1592 goto out_noupdate;
1593 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1594 goto out_noupdate;
1595 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1596 ret = true;
1597out_noupdate:
1598 spin_unlock(&state->state_lock);
1599 return ret;
1600}
Trond Myklebust34310432008-12-23 15:21:38 -05001601
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001602static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001603{
1604 struct nfs_delegation *delegation;
1605
1606 rcu_read_lock();
1607 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001609 rcu_read_unlock();
1610 return;
1611 }
1612 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001613 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001614}
1615
Trond Myklebust6ee41262007-07-08 14:11:36 -04001616static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617{
1618 struct nfs4_state *state = opendata->state;
1619 struct nfs_inode *nfsi = NFS_I(state->inode);
1620 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001621 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001622 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001623 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624 nfs4_stateid stateid;
1625 int ret = -EAGAIN;
1626
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001628 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001630 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001631 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001632 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001633 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001634 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001635 rcu_read_lock();
1636 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001637 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001638 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001639 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001640 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001641 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001642 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001644 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001645 if (!opendata->is_recover) {
1646 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1647 if (ret != 0)
1648 goto out;
1649 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001651
1652 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001653 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001654 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001656out:
1657 return ERR_PTR(ret);
1658out_return_state:
1659 atomic_inc(&state->count);
1660 return state;
1661}
1662
Andy Adamsone23008e2012-10-02 21:07:32 -04001663static void
1664nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1665{
1666 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1667 struct nfs_delegation *delegation;
1668 int delegation_flags = 0;
1669
1670 rcu_read_lock();
1671 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1672 if (delegation)
1673 delegation_flags = delegation->flags;
1674 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001675 switch (data->o_arg.claim) {
1676 default:
1677 break;
1678 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1679 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001680 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1681 "returning a delegation for "
1682 "OPEN(CLAIM_DELEGATE_CUR)\n",
1683 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001684 return;
1685 }
1686 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001687 nfs_inode_set_delegation(state->inode,
1688 data->owner->so_cred,
1689 &data->o_res);
1690 else
1691 nfs_inode_reclaim_delegation(state->inode,
1692 data->owner->so_cred,
1693 &data->o_res);
1694}
1695
1696/*
1697 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1698 * and update the nfs4_state.
1699 */
1700static struct nfs4_state *
1701_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1702{
1703 struct inode *inode = data->state->inode;
1704 struct nfs4_state *state = data->state;
1705 int ret;
1706
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001707 if (!data->rpc_done) {
1708 if (data->rpc_status) {
1709 ret = data->rpc_status;
1710 goto err;
1711 }
1712 /* cached opens have already been processed */
1713 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001714 }
1715
Andy Adamsone23008e2012-10-02 21:07:32 -04001716 ret = nfs_refresh_inode(inode, &data->f_attr);
1717 if (ret)
1718 goto err;
1719
1720 if (data->o_res.delegation_type != 0)
1721 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001722update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 update_open_stateid(state, &data->o_res.stateid, NULL,
1724 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001725 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001726
1727 return state;
1728err:
1729 return ERR_PTR(ret);
1730
1731}
1732
1733static struct nfs4_state *
1734_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001735{
1736 struct inode *inode;
1737 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001738 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001739
Trond Myklebustaac00a82007-07-05 19:02:21 -04001740 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001741 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001742 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001743 goto out;
1744 }
1745
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001746 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001748 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001749 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001750 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001751 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001752 goto err;
1753 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 state = nfs4_get_open_state(inode, data->owner);
1755 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001757 if (data->o_res.delegation_type != 0)
1758 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001759 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001760 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001763 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001765err_put_inode:
1766 iput(inode);
1767err:
1768 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769}
1770
Andy Adamsone23008e2012-10-02 21:07:32 -04001771static struct nfs4_state *
1772nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1773{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001774 struct nfs4_state *ret;
1775
Andy Adamsone23008e2012-10-02 21:07:32 -04001776 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001777 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1778 else
1779 ret = _nfs4_opendata_to_nfs4_state(data);
1780 nfs4_sequence_free_slot(&data->o_res.seq_res);
1781 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001782}
1783
Trond Myklebust864472e2006-01-03 09:55:15 +01001784static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1785{
1786 struct nfs_inode *nfsi = NFS_I(state->inode);
1787 struct nfs_open_context *ctx;
1788
1789 spin_lock(&state->inode->i_lock);
1790 list_for_each_entry(ctx, &nfsi->open_files, list) {
1791 if (ctx->state != state)
1792 continue;
1793 get_nfs_open_context(ctx);
1794 spin_unlock(&state->inode->i_lock);
1795 return ctx;
1796 }
1797 spin_unlock(&state->inode->i_lock);
1798 return ERR_PTR(-ENOENT);
1799}
1800
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001801static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1802 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001803{
1804 struct nfs4_opendata *opendata;
1805
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001806 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001807 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001808 if (opendata == NULL)
1809 return ERR_PTR(-ENOMEM);
1810 opendata->state = state;
1811 atomic_inc(&state->count);
1812 return opendata;
1813}
1814
Trond Myklebust24311f82015-09-20 10:50:17 -04001815static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1816 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001817{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001818 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 int ret;
1820
Trond Myklebust24311f82015-09-20 10:50:17 -04001821 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001822 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001823 opendata->o_arg.open_flags = 0;
1824 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001825 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1826 NFS_SB(opendata->dentry->d_sb),
1827 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001828 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1829 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1830 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001831 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 if (ret != 0)
1833 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001834 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001835 if (IS_ERR(newstate))
1836 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001837 if (newstate != opendata->state)
1838 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001839 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001840 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001841}
1842
1843static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1844{
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 int ret;
1846
Trond Myklebust4f14c192014-02-12 19:15:06 -05001847 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1848 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1849 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1850 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001851 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001852 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001853 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001854 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001855 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1856 if (ret != 0)
1857 return ret;
1858 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1859 if (ret != 0)
1860 return ret;
1861 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1862 if (ret != 0)
1863 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001864 /*
1865 * We may have performed cached opens for all three recoveries.
1866 * Check if we need to update the current stateid.
1867 */
1868 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001869 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001870 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001871 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001872 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001873 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001874 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001875 return 0;
1876}
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
1879 * OPEN_RECLAIM:
1880 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001882static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001884 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001886 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int status;
1888
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001889 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1890 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001891 if (IS_ERR(opendata))
1892 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001893 rcu_read_lock();
1894 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001895 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001896 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001897 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001898 opendata->o_arg.u.delegation_type = delegation_type;
1899 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001900 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return status;
1902}
1903
Trond Myklebust539cd032007-06-05 11:46:42 -04001904static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct nfs_server *server = NFS_SERVER(state->inode);
1907 struct nfs4_exception exception = { };
1908 int err;
1909 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001910 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001911 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001912 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1913 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001914 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001915 break;
1916 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 } while (exception.retry);
1918 return err;
1919}
1920
Trond Myklebust864472e2006-01-03 09:55:15 +01001921static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1922{
1923 struct nfs_open_context *ctx;
1924 int ret;
1925
1926 ctx = nfs4_state_find_open_context(state);
1927 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001928 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001929 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001930 put_nfs_open_context(ctx);
1931 return ret;
1932}
1933
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001934static 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 -07001935{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001936 switch (err) {
1937 default:
1938 printk(KERN_ERR "NFS: %s: unhandled error "
1939 "%d.\n", __func__, err);
1940 case 0:
1941 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001942 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001943 case -ESTALE:
1944 break;
1945 case -NFS4ERR_BADSESSION:
1946 case -NFS4ERR_BADSLOT:
1947 case -NFS4ERR_BAD_HIGH_SLOT:
1948 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1949 case -NFS4ERR_DEADSESSION:
1950 set_bit(NFS_DELEGATED_STATE, &state->flags);
1951 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1952 return -EAGAIN;
1953 case -NFS4ERR_STALE_CLIENTID:
1954 case -NFS4ERR_STALE_STATEID:
1955 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001956 /* Don't recall a delegation if it was lost */
1957 nfs4_schedule_lease_recovery(server->nfs_client);
1958 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001959 case -NFS4ERR_MOVED:
1960 nfs4_schedule_migration_recovery(server);
1961 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001962 case -NFS4ERR_LEASE_MOVED:
1963 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1964 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001965 case -NFS4ERR_DELEG_REVOKED:
1966 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001967 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001968 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001969 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 nfs_inode_find_state_and_recover(state->inode,
1971 stateid);
1972 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001973 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001974 case -NFS4ERR_DELAY:
1975 case -NFS4ERR_GRACE:
1976 set_bit(NFS_DELEGATED_STATE, &state->flags);
1977 ssleep(1);
1978 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001979 case -ENOMEM:
1980 case -NFS4ERR_DENIED:
1981 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1982 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return err;
1985}
1986
Trond Myklebust24311f82015-09-20 10:50:17 -04001987int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1988 struct nfs4_state *state, const nfs4_stateid *stateid,
1989 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001990{
1991 struct nfs_server *server = NFS_SERVER(state->inode);
1992 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001993 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001994
1995 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1996 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1997 if (IS_ERR(opendata))
1998 return PTR_ERR(opendata);
1999 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002000 write_seqlock(&state->seqlock);
2001 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2002 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002003 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2004 switch (type & (FMODE_READ|FMODE_WRITE)) {
2005 case FMODE_READ|FMODE_WRITE:
2006 case FMODE_WRITE:
2007 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2008 if (err)
2009 break;
2010 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2011 if (err)
2012 break;
2013 case FMODE_READ:
2014 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2015 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002016 nfs4_opendata_put(opendata);
2017 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2018}
2019
Chuck Leverbe05c862013-08-09 12:49:47 -04002020static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2021{
2022 struct nfs4_opendata *data = calldata;
2023
Peng Taocb04ad22014-06-11 05:24:15 +08002024 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2025 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002026}
2027
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2029{
2030 struct nfs4_opendata *data = calldata;
2031
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002032 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002033
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002034 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002035 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002036 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002037 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002038 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002039 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002040 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041}
2042
2043static void nfs4_open_confirm_release(void *calldata)
2044{
2045 struct nfs4_opendata *data = calldata;
2046 struct nfs4_state *state = NULL;
2047
2048 /* If this request hasn't been cancelled, do nothing */
2049 if (data->cancelled == 0)
2050 goto out_free;
2051 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002052 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002053 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002054 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002055 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002056 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002057out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002058 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002059}
2060
2061static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002062 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002063 .rpc_call_done = nfs4_open_confirm_done,
2064 .rpc_release = nfs4_open_confirm_release,
2065};
2066
2067/*
2068 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2069 */
2070static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2071{
David Howells2b0143b2015-03-17 22:25:59 +00002072 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002074 struct rpc_message msg = {
2075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2076 .rpc_argp = &data->c_arg,
2077 .rpc_resp = &data->c_res,
2078 .rpc_cred = data->owner->so_cred,
2079 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002080 struct rpc_task_setup task_setup_data = {
2081 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002082 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002083 .callback_ops = &nfs4_open_confirm_ops,
2084 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002085 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002086 .flags = RPC_TASK_ASYNC,
2087 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int status;
2089
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002090 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002091 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002092 data->rpc_done = 0;
2093 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002094 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002095 if (data->is_recover)
2096 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002097 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002098 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002099 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002100 status = nfs4_wait_for_completion_rpc_task(task);
2101 if (status != 0) {
2102 data->cancelled = 1;
2103 smp_wmb();
2104 } else
2105 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002106 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return status;
2108}
2109
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002110static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112 struct nfs4_opendata *data = calldata;
2113 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002114 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002115 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002116
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002118 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002119 /*
2120 * Check if we still need to send an OPEN call, or if we can use
2121 * a delegation instead.
2122 */
2123 if (data->state != NULL) {
2124 struct nfs_delegation *delegation;
2125
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002126 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002127 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002128 rcu_read_lock();
2129 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002130 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002131 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002132 rcu_read_unlock();
2133 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002134 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002135 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002136 switch (claim) {
2137 default:
2138 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002139 case NFS4_OPEN_CLAIM_PREVIOUS:
2140 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2141 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002142 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002143 case NFS4_OPEN_CLAIM_FH:
2144 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002145 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2146 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002147 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002148 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002149 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002150 &data->o_res.seq_res,
2151 task) != 0)
2152 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002153
2154 /* Set the create mode (note dependency on the session type) */
2155 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2156 if (data->o_arg.open_flags & O_EXCL) {
2157 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2158 if (nfs4_has_persistent_session(clp))
2159 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2160 else if (clp->cl_mvops->minor_version > 0)
2161 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2162 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002163 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002164unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002165 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002166 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002167out_no_action:
2168 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002169out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002170 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002171}
2172
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002173static void nfs4_open_done(struct rpc_task *task, void *calldata)
2174{
2175 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002177 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002178
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002179 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002180 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002181
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002182 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002183 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2184 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002185 case S_IFREG:
2186 break;
2187 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002189 break;
2190 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002192 break;
2193 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002194 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002195 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002196 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002197 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002198 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2199 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002200 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002201 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204static void nfs4_open_release(void *calldata)
2205{
2206 struct nfs4_opendata *data = calldata;
2207 struct nfs4_state *state = NULL;
2208
2209 /* If this request hasn't been cancelled, do nothing */
2210 if (data->cancelled == 0)
2211 goto out_free;
2212 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002213 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002214 goto out_free;
2215 /* In case we need an open_confirm, no cleanup! */
2216 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2217 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002219 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002220 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002222 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002223}
2224
2225static const struct rpc_call_ops nfs4_open_ops = {
2226 .rpc_call_prepare = nfs4_open_prepare,
2227 .rpc_call_done = nfs4_open_done,
2228 .rpc_release = nfs4_open_release,
2229};
2230
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002231static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232{
David Howells2b0143b2015-03-17 22:25:59 +00002233 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002234 struct nfs_server *server = NFS_SERVER(dir);
2235 struct nfs_openargs *o_arg = &data->o_arg;
2236 struct nfs_openres *o_res = &data->o_res;
2237 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002238 struct rpc_message msg = {
2239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2240 .rpc_argp = o_arg,
2241 .rpc_resp = o_res,
2242 .rpc_cred = data->owner->so_cred,
2243 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002244 struct rpc_task_setup task_setup_data = {
2245 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002246 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002247 .callback_ops = &nfs4_open_ops,
2248 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002249 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002250 .flags = RPC_TASK_ASYNC,
2251 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002252 int status;
2253
Chuck Levera9c92d62013-08-09 12:48:18 -04002254 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002255 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002256 data->rpc_done = 0;
2257 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002258 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002259 data->is_recover = 0;
2260 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002261 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002262 data->is_recover = 1;
2263 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002265 if (IS_ERR(task))
2266 return PTR_ERR(task);
2267 status = nfs4_wait_for_completion_rpc_task(task);
2268 if (status != 0) {
2269 data->cancelled = 1;
2270 smp_wmb();
2271 } else
2272 status = data->rpc_status;
2273 rpc_put_task(task);
2274
2275 return status;
2276}
2277
2278static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2279{
David Howells2b0143b2015-03-17 22:25:59 +00002280 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002281 struct nfs_openres *o_res = &data->o_res;
2282 int status;
2283
2284 status = nfs4_run_open_task(data, 1);
2285 if (status != 0 || !data->rpc_done)
2286 return status;
2287
Trond Myklebust6926afd12012-01-07 13:22:46 -05002288 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2289
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002290 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2291 status = _nfs4_proc_open_confirm(data);
2292 if (status != 0)
2293 return status;
2294 }
2295
2296 return status;
2297}
2298
Trond Myklebustf3792d62014-07-10 08:54:32 -04002299/*
2300 * Additional permission checks in order to distinguish between an
2301 * open for read, and an open for execute. This works around the
2302 * fact that NFSv4 OPEN treats read and execute permissions as being
2303 * the same.
2304 * Note that in the non-execute case, we want to turn off permission
2305 * checking if we just created a new file (POSIX open() semantics).
2306 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002307static int nfs4_opendata_access(struct rpc_cred *cred,
2308 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002309 struct nfs4_state *state, fmode_t fmode,
2310 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002311{
2312 struct nfs_access_entry cache;
2313 u32 mask;
2314
2315 /* access call failed or for some reason the server doesn't
2316 * support any access modes -- defer access call until later */
2317 if (opendata->o_res.access_supported == 0)
2318 return 0;
2319
2320 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002321 /*
2322 * Use openflags to check for exec, because fmode won't
2323 * always have FMODE_EXEC set when file open for exec.
2324 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002325 if (openflags & __FMODE_EXEC) {
2326 /* ONLY check for exec rights */
2327 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002328 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002329 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002330
2331 cache.cred = cred;
2332 cache.jiffies = jiffies;
2333 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2334 nfs_access_add_cache(state->inode, &cache);
2335
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002336 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002337 return 0;
2338
2339 /* even though OPEN succeeded, access is denied. Close the file */
2340 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002341 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002342}
2343
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002344/*
2345 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2346 */
2347static int _nfs4_proc_open(struct nfs4_opendata *data)
2348{
David Howells2b0143b2015-03-17 22:25:59 +00002349 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002350 struct nfs_server *server = NFS_SERVER(dir);
2351 struct nfs_openargs *o_arg = &data->o_arg;
2352 struct nfs_openres *o_res = &data->o_res;
2353 int status;
2354
2355 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002356 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002357 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002358 if (status != 0) {
2359 if (status == -NFS4ERR_BADNAME &&
2360 !(o_arg->open_flags & O_CREAT))
2361 return -ENOENT;
2362 return status;
2363 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002364
Trond Myklebust6926afd12012-01-07 13:22:46 -05002365 nfs_fattr_map_and_free_names(server, &data->f_attr);
2366
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002367 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002368 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002369 if (o_arg->open_flags & O_EXCL)
2370 data->file_created = 1;
2371 else if (o_res->cinfo.before != o_res->cinfo.after)
2372 data->file_created = 1;
2373 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002374 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2375 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002377 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002379 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002382 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Andy Adamsond83217c2011-03-01 01:34:17 +00002386static int nfs4_recover_expired_lease(struct nfs_server *server)
2387{
2388 return nfs4_client_recover_expired_lease(server->nfs_client);
2389}
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391/*
2392 * OPEN_EXPIRED:
2393 * reclaim state on the server after a network partition.
2394 * Assumes caller holds the appropriate lock
2395 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002396static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002398 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002399 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002401 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002402 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002403 if (IS_ERR(opendata))
2404 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002405 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002406 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002407 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002408 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002409 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002412static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002413{
Trond Myklebust539cd032007-06-05 11:46:42 -04002414 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002415 struct nfs4_exception exception = { };
2416 int err;
2417
2418 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002419 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002420 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002421 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2422 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002423 switch (err) {
2424 default:
2425 goto out;
2426 case -NFS4ERR_GRACE:
2427 case -NFS4ERR_DELAY:
2428 nfs4_handle_exception(server, err, &exception);
2429 err = 0;
2430 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002432out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002433 return err;
2434}
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002439 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Trond Myklebust864472e2006-01-03 09:55:15 +01002441 ctx = nfs4_state_find_open_context(state);
2442 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002443 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002444 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002445 put_nfs_open_context(ctx);
2446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Trond Myklebust41020b62016-09-22 13:38:58 -04002449static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2450 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002451{
Trond Myklebust41020b62016-09-22 13:38:58 -04002452 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002453 write_seqlock(&state->seqlock);
2454 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2455 write_sequnlock(&state->seqlock);
2456 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2457}
2458
2459static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2460{
2461 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002462 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002463}
2464
2465static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2466{
2467 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2468 nfs40_clear_delegation_stateid(state);
2469 return nfs4_open_expired(sp, state);
2470}
2471
Trond Myklebust45870d62016-09-22 13:38:59 -04002472static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2473 nfs4_stateid *stateid,
2474 struct rpc_cred *cred)
2475{
2476 return -NFS4ERR_BAD_STATEID;
2477}
2478
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002479#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002480static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2481 nfs4_stateid *stateid,
2482 struct rpc_cred *cred)
2483{
2484 int status;
2485
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002486 switch (stateid->type) {
2487 default:
2488 break;
2489 case NFS4_INVALID_STATEID_TYPE:
2490 case NFS4_SPECIAL_STATEID_TYPE:
2491 return -NFS4ERR_BAD_STATEID;
2492 case NFS4_REVOKED_STATEID_TYPE:
2493 goto out_free;
2494 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002495
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002496 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002497 switch (status) {
2498 case -NFS4ERR_EXPIRED:
2499 case -NFS4ERR_ADMIN_REVOKED:
2500 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002501 break;
2502 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002503 return status;
2504 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002505out_free:
2506 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002507 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002508 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509}
2510
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002511static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002512{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002513 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002514 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002515 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002516 struct rpc_cred *cred;
2517 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002518
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002519 /* Get the delegation credential for use by test/free_stateid */
2520 rcu_read_lock();
2521 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002522 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002523 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002524 return;
2525 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002526
2527 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002528 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2529 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002530 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002531 return;
2532 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002533
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002534 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2535 rcu_read_unlock();
2536 return;
2537 }
2538
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002539 cred = get_rpccred(delegation->cred);
2540 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002541 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002543 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002544 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002545
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002546 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002547}
2548
2549/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002550 * nfs41_check_expired_locks - possibly free a lock stateid
2551 *
2552 * @state: NFSv4 state for an inode
2553 *
2554 * Returns NFS_OK if recovery for this stateid is now finished.
2555 * Otherwise a negative NFS4ERR value is returned.
2556 */
2557static int nfs41_check_expired_locks(struct nfs4_state *state)
2558{
2559 int status, ret = NFS_OK;
2560 struct nfs4_lock_state *lsp;
2561 struct nfs_server *server = NFS_SERVER(state->inode);
2562
2563 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2564 goto out;
2565 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2566 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2567 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2568
2569 status = nfs41_test_and_free_expired_stateid(server,
2570 &lsp->ls_stateid,
2571 cred);
2572 trace_nfs4_test_lock_stateid(state, lsp, status);
2573 if (status == -NFS4ERR_EXPIRED ||
2574 status == -NFS4ERR_BAD_STATEID) {
2575 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002576 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002577 if (!recover_lost_locks)
2578 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2579 } else if (status != NFS_OK) {
2580 ret = status;
2581 break;
2582 }
2583 }
2584 };
2585out:
2586 return ret;
2587}
2588
2589/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002590 * nfs41_check_open_stateid - possibly free an open stateid
2591 *
2592 * @state: NFSv4 state for an inode
2593 *
2594 * Returns NFS_OK if recovery for this stateid is now finished.
2595 * Otherwise a negative NFS4ERR value is returned.
2596 */
2597static int nfs41_check_open_stateid(struct nfs4_state *state)
2598{
2599 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002600 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002601 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002602 int status;
2603
Trond Myklebustb134fc42016-09-22 13:39:16 -04002604 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002605 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2606 if (nfs4_have_delegation(state->inode, state->state))
2607 return NFS_OK;
2608 return -NFS4ERR_OPENMODE;
2609 }
Trond Myklebustb134fc42016-09-22 13:39:16 -04002610 return -NFS4ERR_BAD_STATEID;
2611 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002612 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002613 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002614 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002615 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2616 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2617 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002618 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002619 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002620 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002621 if (status != NFS_OK)
2622 return status;
2623 if (nfs_open_stateid_recover_openmode(state))
2624 return -NFS4ERR_OPENMODE;
2625 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002626}
2627
2628static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2629{
Chuck Levereb64cf92012-07-11 16:30:05 -04002630 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002631
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002632 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002633 status = nfs41_check_expired_locks(state);
2634 if (status != NFS_OK)
2635 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002636 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002637 if (status != NFS_OK)
2638 status = nfs4_open_expired(sp, state);
2639 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002640}
2641#endif
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002644 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2645 * fields corresponding to attributes that were used to store the verifier.
2646 * Make sure we clobber those fields in the later setattr call
2647 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002648static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2649 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002650{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002651 const u32 *attrset = opendata->o_res.attrset;
2652
2653 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002654 !(sattr->ia_valid & ATTR_ATIME_SET))
2655 sattr->ia_valid |= ATTR_ATIME;
2656
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002657 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002658 !(sattr->ia_valid & ATTR_MTIME_SET))
2659 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002660
2661 /* Except MODE, it seems harmless of setting twice. */
2662 if ((attrset[1] & FATTR4_WORD1_MODE))
2663 sattr->ia_valid &= ~ATTR_MODE;
2664
2665 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2666 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002667}
2668
Trond Myklebustc21443c2013-02-07 14:26:21 -05002669static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2670 fmode_t fmode,
2671 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002672 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002673{
2674 struct nfs4_state_owner *sp = opendata->owner;
2675 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002676 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002677 struct nfs4_state *state;
2678 unsigned int seq;
2679 int ret;
2680
2681 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2682
2683 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002684 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002685 goto out;
2686
2687 state = nfs4_opendata_to_nfs4_state(opendata);
2688 ret = PTR_ERR(state);
2689 if (IS_ERR(state))
2690 goto out;
2691 if (server->caps & NFS_CAP_POSIX_LOCK)
2692 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002693 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2694 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002695
Trond Myklebust275bb302013-05-29 13:11:28 -04002696 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002697 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002698 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002699 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002700 alias = d_exact_alias(dentry, state->inode);
2701 if (!alias)
2702 alias = d_splice_alias(igrab(state->inode), dentry);
2703 /* d_splice_alias() can't fail here - it's a non-directory */
2704 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002705 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002706 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002707 }
2708 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002709 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002710 }
2711
Trond Myklebustc21443c2013-02-07 14:26:21 -05002712 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2713 if (ret != 0)
2714 goto out;
2715
Trond Myklebust3efb9722013-05-29 13:17:04 -04002716 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002717 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002718 nfs_inode_attach_open_context(ctx);
2719 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2720 nfs4_schedule_stateid_recovery(server, state);
2721 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002722out:
2723 return ret;
2724}
2725
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002726/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002727 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
Andy Adamson82be4172012-05-23 05:02:35 -04002729static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002730 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002731 int flags,
2732 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002733 struct nfs4_label *label,
2734 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 struct nfs4_state_owner *sp;
2737 struct nfs4_state *state = NULL;
2738 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002739 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002740 struct dentry *dentry = ctx->dentry;
2741 struct rpc_cred *cred = ctx->cred;
2742 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2743 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002744 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002745 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002746 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002750 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2751 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2753 goto out_err;
2754 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002755 status = nfs4_recover_expired_lease(server);
2756 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002757 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002758 if (d_really_is_positive(dentry))
2759 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002760 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002761 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002762 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002763 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002764 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002765 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002766 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
David Quigley14c43f72013-05-22 12:50:43 -04002768 if (label) {
2769 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2770 if (IS_ERR(olabel)) {
2771 status = PTR_ERR(olabel);
2772 goto err_opendata_put;
2773 }
2774 }
2775
Trond Myklebuste911b812014-03-26 13:24:37 -07002776 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2777 if (!opendata->f_attr.mdsthreshold) {
2778 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2779 if (!opendata->f_attr.mdsthreshold)
2780 goto err_free_label;
2781 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002782 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002783 }
David Howells2b0143b2015-03-17 22:25:59 +00002784 if (d_really_is_positive(dentry))
2785 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002786
Trond Myklebust3efb9722013-05-29 13:17:04 -04002787 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002788 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002789 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002790 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002791
NeilBrownefcbc042015-07-30 13:00:56 +10002792 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002793 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002794 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002795 /*
2796 * send create attributes which was not set by open
2797 * with an extra setattr.
2798 */
2799 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2800 nfs_fattr_init(opendata->o_res.f_attr);
2801 status = nfs4_do_setattr(state->inode, cred,
2802 opendata->o_res.f_attr, sattr,
2803 state, label, olabel);
2804 if (status == 0) {
2805 nfs_setattr_update_inode(state->inode, sattr,
2806 opendata->o_res.f_attr);
2807 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2808 }
David Quigley1775fd32013-05-22 12:50:42 -04002809 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002810 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002811 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002812 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002813
Trond Myklebuste911b812014-03-26 13:24:37 -07002814 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002815 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002816 opendata->f_attr.mdsthreshold = NULL;
2817 }
Andy Adamson82be4172012-05-23 05:02:35 -04002818
David Quigley14c43f72013-05-22 12:50:43 -04002819 nfs4_label_free(olabel);
2820
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002821 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002824err_free_label:
2825 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002826err_opendata_put:
2827 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002828err_put_state_owner:
2829 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return status;
2832}
2833
2834
Andy Adamson82be4172012-05-23 05:02:35 -04002835static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002836 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002837 int flags,
2838 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002839 struct nfs4_label *label,
2840 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002842 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 struct nfs4_exception exception = { };
2844 struct nfs4_state *res;
2845 int status;
2846
2847 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002848 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002849 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002850 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (status == 0)
2852 break;
2853 /* NOTE: BAD_SEQID means the server and client disagree about the
2854 * book-keeping w.r.t. state-changing operations
2855 * (OPEN/CLOSE/LOCK/LOCKU...)
2856 * It is actually a sign of a bug on the client or on the server.
2857 *
2858 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002859 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * have unhashed the old state_owner for us, and that we can
2861 * therefore safely retry using a new one. We should still warn
2862 * the user though...
2863 */
2864 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002865 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002866 " returned a bad sequence-id error!\n",
2867 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 exception.retry = 1;
2869 continue;
2870 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002871 /*
2872 * BAD_STATEID on OPEN means that the server cancelled our
2873 * state before it received the OPEN_CONFIRM.
2874 * Recover by retrying the request as per the discussion
2875 * on Page 181 of RFC3530.
2876 */
2877 if (status == -NFS4ERR_BAD_STATEID) {
2878 exception.retry = 1;
2879 continue;
2880 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002881 if (status == -EAGAIN) {
2882 /* We must have found a delegation */
2883 exception.retry = 1;
2884 continue;
2885 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002886 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2887 continue;
2888 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 status, &exception));
2890 } while (exception.retry);
2891 return res;
2892}
2893
Trond Myklebust8487c472016-06-26 08:44:35 -04002894static int _nfs4_do_setattr(struct inode *inode,
2895 struct nfs_setattrargs *arg,
2896 struct nfs_setattrres *res,
2897 struct rpc_cred *cred,
2898 struct nfs4_state *state)
2899{
2900 struct nfs_server *server = NFS_SERVER(inode);
2901 struct rpc_message msg = {
2902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2903 .rpc_argp = arg,
2904 .rpc_resp = res,
2905 .rpc_cred = cred,
2906 };
2907 struct rpc_cred *delegation_cred = NULL;
2908 unsigned long timestamp = jiffies;
2909 fmode_t fmode;
2910 bool truncate;
2911 int status;
2912
2913 nfs_fattr_init(res->fattr);
2914
2915 /* Servers should only apply open mode checks for file size changes */
2916 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2917 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2918
2919 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2920 /* Use that stateid */
2921 } else if (truncate && state != NULL) {
2922 struct nfs_lockowner lockowner = {
2923 .l_owner = current->files,
2924 .l_pid = current->tgid,
2925 };
2926 if (!nfs4_valid_open_stateid(state))
2927 return -EBADF;
2928 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2929 &arg->stateid, &delegation_cred) == -EIO)
2930 return -EBADF;
2931 } else
2932 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2933 if (delegation_cred)
2934 msg.rpc_cred = delegation_cred;
2935
2936 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2937
2938 put_rpccred(delegation_cred);
2939 if (status == 0 && state != NULL)
2940 renew_lease(server, timestamp);
2941 trace_nfs4_setattr(inode, &arg->stateid, status);
2942 return status;
2943}
2944
2945static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2946 struct nfs_fattr *fattr, struct iattr *sattr,
2947 struct nfs4_state *state, struct nfs4_label *ilabel,
2948 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002950 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002952 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .iap = sattr,
2954 .server = server,
2955 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002956 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 };
2958 struct nfs_setattrres res = {
2959 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002960 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .server = server,
2962 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002963 struct nfs4_exception exception = {
2964 .state = state,
2965 .inode = inode,
2966 .stateid = &arg.stateid,
2967 };
2968 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
David Quigleyaa9c2662013-05-22 12:50:44 -04002970 arg.bitmask = nfs4_bitmask(server, ilabel);
2971 if (ilabel)
2972 arg.bitmask = nfs4_bitmask(server, olabel);
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002975 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002976 switch (err) {
2977 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002978 if (!(sattr->ia_valid & ATTR_SIZE)) {
2979 pr_warn_once("NFSv4: server %s is incorrectly "
2980 "applying open mode checks to "
2981 "a SETATTR that is not "
2982 "changing file size.\n",
2983 server->nfs_client->cl_hostname);
2984 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002985 if (state && !(state->state & FMODE_WRITE)) {
2986 err = -EBADF;
2987 if (sattr->ia_valid & ATTR_OPEN)
2988 err = -EACCES;
2989 goto out;
2990 }
2991 }
2992 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002994out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return err;
2996}
2997
Peng Tao500d7012015-09-22 11:35:22 +08002998static bool
2999nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3000{
3001 if (inode == NULL || !nfs_have_layout(inode))
3002 return false;
3003
3004 return pnfs_wait_on_layoutreturn(inode, task);
3005}
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007struct nfs4_closedata {
3008 struct inode *inode;
3009 struct nfs4_state *state;
3010 struct nfs_closeargs arg;
3011 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003012 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003013 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003014 bool roc;
3015 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016};
3017
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003018static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003019{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003020 struct nfs4_closedata *calldata = data;
3021 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003022 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003023
Fred Isamanf7e89172011-01-06 11:36:32 +00003024 if (calldata->roc)
3025 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003026 nfs4_put_open_state(calldata->state);
3027 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003028 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003029 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003030 kfree(calldata);
3031}
3032
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003033static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003035 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003038 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Chuck Levera3ca5652012-03-01 17:00:40 -05003040 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003041 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3042 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003043 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* hmm. we are done with the inode, and in the process of freeing
3045 * the state_owner. we keep this around to process errors
3046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 switch (task->tk_status) {
3048 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003049 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003050 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003051 pnfs_roc_set_barrier(state->inode,
3052 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003053 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003054 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003055 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003057 case -NFS4ERR_EXPIRED:
3058 nfs4_free_revoked_stateid(server,
3059 &calldata->arg.stateid,
3060 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003061 case -NFS4ERR_OLD_STATEID:
3062 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003063 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003064 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003065 rpc_restart_call_prepare(task);
3066 goto out_release;
3067 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003068 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003071 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003072 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003073 goto out_release;
3074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003076 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3077 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003078out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003079 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003080 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003081 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003084static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003086 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003087 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003088 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003089 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003090 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003091
Chuck Levera3ca5652012-03-01 17:00:40 -05003092 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003093 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003094 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003095
Trond Myklebust88069f72009-12-08 08:33:16 -05003096 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003097 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003098 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3099 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3100 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003101 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003102 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003103 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003104 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003105 if (state->n_rdonly == 0)
3106 call_close |= is_rdonly;
3107 else if (is_rdonly)
3108 calldata->arg.fmode |= FMODE_READ;
3109 if (state->n_wronly == 0)
3110 call_close |= is_wronly;
3111 else if (is_wronly)
3112 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003113 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3114 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003115 } else if (is_rdwr)
3116 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3117
Trond Myklebust5d422302013-03-14 16:57:48 -04003118 if (!nfs4_valid_open_stateid(state))
3119 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003120 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003121
3122 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003123 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003124 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003125 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003126
Peng Tao500d7012015-09-22 11:35:22 +08003127 if (nfs4_wait_on_layoutreturn(inode, task)) {
3128 nfs_release_seqid(calldata->arg.seqid);
3129 goto out_wait;
3130 }
3131
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003132 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003133 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003134 if (calldata->roc)
3135 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3136
Trond Myklebust6ae37332015-01-30 14:21:14 -05003137 calldata->arg.share_access =
3138 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3139 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003140
Trond Myklebust516a6af2005-10-27 22:12:41 -04003141 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003142 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003143 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003144 &calldata->arg.seq_args,
3145 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003146 task) != 0)
3147 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003148 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003149 return;
3150out_no_action:
3151 task->tk_action = NULL;
3152out_wait:
3153 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003157 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003158 .rpc_call_done = nfs4_close_done,
3159 .rpc_release = nfs4_free_closedata,
3160};
3161
Peng Taofe08c542014-07-03 13:05:01 +08003162static bool nfs4_roc(struct inode *inode)
3163{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003164 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003165 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003166 return pnfs_roc(inode);
3167}
3168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169/*
3170 * It is possible for data to be read/written from a mem-mapped file
3171 * after the sys_close call (which hits the vfs layer as a flush).
3172 * This means that we can't safely call nfsv4 close on a file until
3173 * the inode is cleared. This in turn means that we are not good
3174 * NFSv4 citizens - we do not indicate to the server to update the file's
3175 * share state even when we are done with one of the three share
3176 * stateid's in the inode.
3177 *
3178 * NOTE: Caller must be holding the sp->so_owner semaphore!
3179 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003180int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003182 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003183 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003185 struct nfs4_state_owner *sp = state->owner;
3186 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003187 struct rpc_message msg = {
3188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3189 .rpc_cred = state->owner->so_cred,
3190 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003191 struct rpc_task_setup task_setup_data = {
3192 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003193 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003194 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003195 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003196 .flags = RPC_TASK_ASYNC,
3197 };
Trond Myklebust95121352005-10-18 14:20:12 -07003198 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003200 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3201 &task_setup_data.rpc_client, &msg);
3202
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003203 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003205 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003206 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003207 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003209 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003211 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3212 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003213 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003214 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003215 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003216 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003217 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003218 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003219 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003220 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003221 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003222
Trond Myklebust1174dd12010-12-21 10:52:24 -05003223 msg.rpc_argp = &calldata->arg;
3224 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003225 task_setup_data.callback_data = calldata;
3226 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003227 if (IS_ERR(task))
3228 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003229 status = 0;
3230 if (wait)
3231 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003232 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003233 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003234out_free_calldata:
3235 kfree(calldata);
3236out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003237 nfs4_put_open_state(state);
3238 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003239 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Trond Myklebust2b484292010-09-17 10:56:51 -04003242static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003243nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3244 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003247 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3248
3249 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Trond Myklebust565277f2007-10-15 18:17:53 -04003251 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003252 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003253
3254 nfs4_label_release_security(label);
3255
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003256 if (IS_ERR(state))
3257 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003258 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
Trond Myklebust1185a552009-12-03 15:54:02 -05003261static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003262{
3263 if (ctx->state == NULL)
3264 return;
3265 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003266 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003267 else
Al Viro643168c2011-06-22 18:20:23 -04003268 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebustb944dba2013-11-04 15:20:20 -05003271#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3272#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003273#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3276{
Kinglong Mee8c612822015-08-26 21:12:58 +08003277 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003278 struct nfs4_server_caps_arg args = {
3279 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003280 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 struct nfs4_server_caps_res res = {};
3283 struct rpc_message msg = {
3284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003285 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 .rpc_resp = &res,
3287 };
3288 int status;
3289
Kinglong Mee8c612822015-08-26 21:12:58 +08003290 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3291 FATTR4_WORD0_FH_EXPIRE_TYPE |
3292 FATTR4_WORD0_LINK_SUPPORT |
3293 FATTR4_WORD0_SYMLINK_SUPPORT |
3294 FATTR4_WORD0_ACLSUPPORT;
3295 if (minorversion)
3296 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3297
Bryan Schumaker7c513052011-03-24 17:12:24 +00003298 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003300 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003301 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003302 case 0:
3303 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3304 res.attr_bitmask[2] = 0;
3305 break;
3306 case 1:
3307 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3308 break;
3309 case 2:
3310 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003313 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3314 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3315 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3316 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003317 NFS_CAP_CTIME|NFS_CAP_MTIME|
3318 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003319 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3320 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 server->caps |= NFS_CAP_ACLS;
3322 if (res.has_links != 0)
3323 server->caps |= NFS_CAP_HARDLINKS;
3324 if (res.has_symlinks != 0)
3325 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003326 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3327 server->caps |= NFS_CAP_FILEID;
3328 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3329 server->caps |= NFS_CAP_MODE;
3330 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3331 server->caps |= NFS_CAP_NLINK;
3332 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3333 server->caps |= NFS_CAP_OWNER;
3334 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3335 server->caps |= NFS_CAP_OWNER_GROUP;
3336 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3337 server->caps |= NFS_CAP_ATIME;
3338 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3339 server->caps |= NFS_CAP_CTIME;
3340 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3341 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003342#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3343 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3344 server->caps |= NFS_CAP_SECURITY_LABEL;
3345#endif
3346 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3347 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003348 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003349
Trond Myklebusta65318b2009-03-11 14:10:28 -04003350 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3351 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3352 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003353 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003354 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3355 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003357 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return status;
3361}
3362
Trond Myklebust55a97592006-06-09 09:34:19 -04003363int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
3365 struct nfs4_exception exception = { };
3366 int err;
3367 do {
3368 err = nfs4_handle_exception(server,
3369 _nfs4_server_capabilities(server, fhandle),
3370 &exception);
3371 } while (exception.retry);
3372 return err;
3373}
3374
3375static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3376 struct nfs_fsinfo *info)
3377{
David Quigleyaa9c2662013-05-22 12:50:44 -04003378 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003380 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 };
3382 struct nfs4_lookup_res res = {
3383 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003384 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 .fh = fhandle,
3386 };
3387 struct rpc_message msg = {
3388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3389 .rpc_argp = &args,
3390 .rpc_resp = &res,
3391 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003392
David Quigleyaa9c2662013-05-22 12:50:44 -04003393 bitmask[0] = nfs4_fattr_bitmap[0];
3394 bitmask[1] = nfs4_fattr_bitmap[1];
3395 /*
3396 * Process the label in the upcoming getfattr
3397 */
3398 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3399
Trond Myklebust0e574af2005-10-27 22:12:38 -04003400 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003401 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
3404static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3405 struct nfs_fsinfo *info)
3406{
3407 struct nfs4_exception exception = { };
3408 int err;
3409 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003410 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003411 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003412 switch (err) {
3413 case 0:
3414 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003415 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003416 default:
3417 err = nfs4_handle_exception(server, err, &exception);
3418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return err;
3422}
3423
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003424static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3425 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3426{
Trond Myklebustc2190662013-08-26 19:23:04 -04003427 struct rpc_auth_create_args auth_args = {
3428 .pseudoflavor = flavor,
3429 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003430 struct rpc_auth *auth;
3431 int ret;
3432
Trond Myklebustc2190662013-08-26 19:23:04 -04003433 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003434 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003435 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003436 goto out;
3437 }
3438 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003439out:
3440 return ret;
3441}
3442
Chuck Lever9a744ba2013-03-16 15:56:02 -04003443/*
3444 * Retry pseudoroot lookup with various security flavors. We do this when:
3445 *
3446 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3447 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3448 *
3449 * Returns zero on success, or a negative NFS4ERR value, or a
3450 * negative errno value.
3451 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003452static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003453 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003455 /* Per 3530bis 15.33.5 */
3456 static const rpc_authflavor_t flav_array[] = {
3457 RPC_AUTH_GSS_KRB5P,
3458 RPC_AUTH_GSS_KRB5I,
3459 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003460 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003461 RPC_AUTH_NULL,
3462 };
3463 int status = -EPERM;
3464 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003466 if (server->auth_info.flavor_len > 0) {
3467 /* try each flavor specified by user */
3468 for (i = 0; i < server->auth_info.flavor_len; i++) {
3469 status = nfs4_lookup_root_sec(server, fhandle, info,
3470 server->auth_info.flavors[i]);
3471 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3472 continue;
3473 break;
3474 }
3475 } else {
3476 /* no flavors specified by user, try default list */
3477 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3478 status = nfs4_lookup_root_sec(server, fhandle, info,
3479 flav_array[i]);
3480 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3481 continue;
3482 break;
3483 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003484 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003485
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003486 /*
3487 * -EACCESS could mean that the user doesn't have correct permissions
3488 * to access the mount. It could also mean that we tried to mount
3489 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3490 * existing mount programs don't handle -EACCES very well so it should
3491 * be mapped to -EPERM instead.
3492 */
3493 if (status == -EACCES)
3494 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003495 return status;
3496}
3497
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498/**
3499 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3500 * @server: initialized nfs_server handle
3501 * @fhandle: we fill in the pseudo-fs root file handle
3502 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003503 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003504 *
3505 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003506 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003507int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003508 struct nfs_fsinfo *info,
3509 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003510{
Andre Przywarac7757072015-04-23 17:17:40 +01003511 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003512
Andre Przywarac7757072015-04-23 17:17:40 +01003513 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003514 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003515
3516 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003517 status = server->nfs_client->cl_mvops->find_root_sec(server,
3518 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (status == 0)
3521 status = nfs4_server_capabilities(server, fhandle);
3522 if (status == 0)
3523 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003524
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003525 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Bryan Schumakerbae36242012-05-10 15:07:31 -04003528static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3529 struct nfs_fsinfo *info)
3530{
3531 int error;
3532 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003533 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003534
3535 error = nfs4_server_capabilities(server, mntfh);
3536 if (error < 0) {
3537 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3538 return error;
3539 }
3540
David Quigley14c43f72013-05-22 12:50:43 -04003541 label = nfs4_label_alloc(server, GFP_KERNEL);
3542 if (IS_ERR(label))
3543 return PTR_ERR(label);
3544
David Quigley1775fd32013-05-22 12:50:42 -04003545 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003546 if (error < 0) {
3547 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003548 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003549 }
3550
3551 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3552 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3553 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3554
David Quigley14c43f72013-05-22 12:50:43 -04003555err_free_label:
3556 nfs4_label_free(label);
3557
Bryan Schumakerbae36242012-05-10 15:07:31 -04003558 return error;
3559}
3560
Manoj Naik6b97fd32006-06-09 09:34:29 -04003561/*
3562 * Get locations and (maybe) other attributes of a referral.
3563 * Note that we'll actually follow the referral later when
3564 * we detect fsid mismatch in inode revalidation
3565 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003566static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3567 const struct qstr *name, struct nfs_fattr *fattr,
3568 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003569{
3570 int status = -ENOMEM;
3571 struct page *page = NULL;
3572 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003573
3574 page = alloc_page(GFP_KERNEL);
3575 if (page == NULL)
3576 goto out;
3577 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3578 if (locations == NULL)
3579 goto out;
3580
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003581 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003582 if (status != 0)
3583 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003584
3585 /*
3586 * If the fsid didn't change, this is a migration event, not a
3587 * referral. Cause us to drop into the exception handler, which
3588 * will kick off migration recovery.
3589 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003590 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003591 dprintk("%s: server did not return a different fsid for"
3592 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003593 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003594 goto out;
3595 }
Andy Adamson533eb462011-06-13 18:25:56 -04003596 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3597 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003598
Andy Adamson533eb462011-06-13 18:25:56 -04003599 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003600 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003601 memset(fhandle, 0, sizeof(struct nfs_fh));
3602out:
3603 if (page)
3604 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003605 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003606 return status;
3607}
3608
David Quigley1775fd32013-05-22 12:50:42 -04003609static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3610 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 struct nfs4_getattr_arg args = {
3613 .fh = fhandle,
3614 .bitmask = server->attr_bitmask,
3615 };
3616 struct nfs4_getattr_res res = {
3617 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003618 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 .server = server,
3620 };
3621 struct rpc_message msg = {
3622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3623 .rpc_argp = &args,
3624 .rpc_resp = &res,
3625 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003626
3627 args.bitmask = nfs4_bitmask(server, label);
3628
Trond Myklebust0e574af2005-10-27 22:12:38 -04003629 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003630 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
David Quigley1775fd32013-05-22 12:50:42 -04003633static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3634 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 struct nfs4_exception exception = { };
3637 int err;
3638 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003639 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3640 trace_nfs4_getattr(server, fhandle, fattr, err);
3641 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 &exception);
3643 } while (exception.retry);
3644 return err;
3645}
3646
3647/*
3648 * The file is not closed if it is opened due to the a request to change
3649 * the size of the file. The open call will not be needed once the
3650 * VFS layer lookup-intents are implemented.
3651 *
3652 * Close is called when the inode is destroyed.
3653 * If we haven't opened the file for O_WRONLY, we
3654 * need to in the size_change case to obtain a stateid.
3655 *
3656 * Got race?
3657 * Because OPEN is always done by name in nfsv4, it is
3658 * possible that we opened a different file by the same
3659 * name. We can recognize this race condition, but we
3660 * can't do anything about it besides returning an error.
3661 *
3662 * This will be fixed with VFS changes (lookup-intent).
3663 */
3664static int
3665nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3666 struct iattr *sattr)
3667{
David Howells2b0143b2015-03-17 22:25:59 +00003668 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003669 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003670 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003671 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 int status;
3673
Peng Tao88ac8152014-09-12 11:04:10 +08003674 if (pnfs_ld_layoutret_on_setattr(inode) &&
3675 sattr->ia_valid & ATTR_SIZE &&
3676 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003677 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003678
Trond Myklebust0e574af2005-10-27 22:12:38 -04003679 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Andy Adamson26699402012-05-30 16:12:24 -04003681 /* Deal with open(O_TRUNC) */
3682 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003683 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003684
3685 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003686 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003687 return 0;
3688
Trond Myklebustd5308382005-11-04 15:33:38 -05003689 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003690 if (sattr->ia_valid & ATTR_FILE) {
3691 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003692
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003693 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003694 if (ctx) {
3695 cred = ctx->cred;
3696 state = ctx->state;
3697 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003698 }
3699
David Quigley14c43f72013-05-22 12:50:43 -04003700 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3701 if (IS_ERR(label))
3702 return PTR_ERR(label);
3703
3704 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003705 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003706 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003707 nfs_setsecurity(inode, fattr, label);
3708 }
David Quigley14c43f72013-05-22 12:50:43 -04003709 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return status;
3711}
3712
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003713static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3714 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003715 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003716{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003717 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003718 int status;
3719 struct nfs4_lookup_arg args = {
3720 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003721 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003722 .name = name,
3723 };
3724 struct nfs4_lookup_res res = {
3725 .server = server,
3726 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003727 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003728 .fh = fhandle,
3729 };
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3732 .rpc_argp = &args,
3733 .rpc_resp = &res,
3734 };
3735
David Quigleyaa9c2662013-05-22 12:50:44 -04003736 args.bitmask = nfs4_bitmask(server, label);
3737
David Howells2b3de442006-08-22 20:06:09 -04003738 nfs_fattr_init(fattr);
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003741 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 dprintk("NFS reply lookup: %d\n", status);
3743 return status;
3744}
3745
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003746static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003747{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003748 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003749 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003750 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3751 fattr->nlink = 2;
3752}
3753
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003754static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003755 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003756 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003757{
3758 struct nfs4_exception exception = { };
3759 struct rpc_clnt *client = *clnt;
3760 int err;
3761 do {
David Quigley1775fd32013-05-22 12:50:42 -04003762 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003763 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003764 switch (err) {
3765 case -NFS4ERR_BADNAME:
3766 err = -ENOENT;
3767 goto out;
3768 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003769 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003770 if (err == -NFS4ERR_MOVED)
3771 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003772 goto out;
3773 case -NFS4ERR_WRONGSEC:
3774 err = -EPERM;
3775 if (client != *clnt)
3776 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003777 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003778 if (IS_ERR(client))
3779 return PTR_ERR(client);
3780
3781 exception.retry = 1;
3782 break;
3783 default:
3784 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3785 }
3786 } while (exception.retry);
3787
3788out:
3789 if (err == 0)
3790 *clnt = client;
3791 else if (client != *clnt)
3792 rpc_shutdown_client(client);
3793
3794 return err;
3795}
3796
Al Virobeffb8f2016-07-20 16:34:42 -04003797static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003798 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3799 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003801 int status;
3802 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003803
David Quigley1775fd32013-05-22 12:50:42 -04003804 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003805 if (client != NFS_CLIENT(dir)) {
3806 rpc_shutdown_client(client);
3807 nfs_fixup_secinfo_attributes(fattr);
3808 }
3809 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003812struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003813nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003814 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3815{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003816 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003817 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003818
David Quigley1775fd32013-05-22 12:50:42 -04003819 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003820 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003821 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003822 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003823}
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3826{
Trond Myklebust76b32992007-08-10 17:45:11 -04003827 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 struct nfs4_accessargs args = {
3829 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003830 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003832 struct nfs4_accessres res = {
3833 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003834 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3837 .rpc_argp = &args,
3838 .rpc_resp = &res,
3839 .rpc_cred = entry->cred,
3840 };
3841 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003842 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844 /*
3845 * Determine which access bits we want to ask for...
3846 */
3847 if (mode & MAY_READ)
3848 args.access |= NFS4_ACCESS_READ;
3849 if (S_ISDIR(inode->i_mode)) {
3850 if (mode & MAY_WRITE)
3851 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3852 if (mode & MAY_EXEC)
3853 args.access |= NFS4_ACCESS_LOOKUP;
3854 } else {
3855 if (mode & MAY_WRITE)
3856 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3857 if (mode & MAY_EXEC)
3858 args.access |= NFS4_ACCESS_EXECUTE;
3859 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003860
3861 res.fattr = nfs_alloc_fattr();
3862 if (res.fattr == NULL)
3863 return -ENOMEM;
3864
Bryan Schumaker7c513052011-03-24 17:12:24 +00003865 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003867 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003868 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003870 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return status;
3872}
3873
3874static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3875{
3876 struct nfs4_exception exception = { };
3877 int err;
3878 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003879 err = _nfs4_proc_access(inode, entry);
3880 trace_nfs4_access(inode, err);
3881 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 &exception);
3883 } while (exception.retry);
3884 return err;
3885}
3886
3887/*
3888 * TODO: For the time being, we don't try to get any attributes
3889 * along with any of the zero-copy operations READ, READDIR,
3890 * READLINK, WRITE.
3891 *
3892 * In the case of the first three, we want to put the GETATTR
3893 * after the read-type operation -- this is because it is hard
3894 * to predict the length of a GETATTR response in v4, and thus
3895 * align the READ data correctly. This means that the GETATTR
3896 * may end up partially falling into the page cache, and we should
3897 * shift it into the 'tail' of the xdr_buf before processing.
3898 * To do this efficiently, we need to know the total length
3899 * of data received, which doesn't seem to be available outside
3900 * of the RPC layer.
3901 *
3902 * In the case of WRITE, we also want to put the GETATTR after
3903 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003904 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 *
3906 * Both of these changes to the XDR layer would in fact be quite
3907 * minor, but I decided to leave them for a subsequent patch.
3908 */
3909static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3910 unsigned int pgbase, unsigned int pglen)
3911{
3912 struct nfs4_readlink args = {
3913 .fh = NFS_FH(inode),
3914 .pgbase = pgbase,
3915 .pglen = pglen,
3916 .pages = &page,
3917 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003918 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct rpc_message msg = {
3920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3921 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003922 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 };
3924
Bryan Schumaker7c513052011-03-24 17:12:24 +00003925 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 -07003926}
3927
3928static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3929 unsigned int pgbase, unsigned int pglen)
3930{
3931 struct nfs4_exception exception = { };
3932 int err;
3933 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003934 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3935 trace_nfs4_readlink(inode, err);
3936 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 &exception);
3938 } while (exception.retry);
3939 return err;
3940}
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003943 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945static int
3946nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003947 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
David Quigleyaa9c2662013-05-22 12:50:44 -04003949 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003950 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 int status = 0;
3953
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003954 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3955 if (IS_ERR(ctx))
3956 return PTR_ERR(ctx);
3957
David Quigleyaa9c2662013-05-22 12:50:44 -04003958 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3959
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003960 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003961 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 if (IS_ERR(state)) {
3963 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003967 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003968 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return status;
3970}
3971
Al Virobeffb8f2016-07-20 16:34:42 -04003972static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Trond Myklebust16e42952005-10-27 22:12:44 -04003974 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003975 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003977 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003979 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003980 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003981 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3984 .rpc_argp = &args,
3985 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003987 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003988
Bryan Schumaker7c513052011-03-24 17:12:24 +00003989 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003990 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003991 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 return status;
3993}
3994
Al Virobeffb8f2016-07-20 16:34:42 -04003995static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
3997 struct nfs4_exception exception = { };
3998 int err;
3999 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004000 err = _nfs4_proc_remove(dir, name);
4001 trace_nfs4_remove(dir, name, err);
4002 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 &exception);
4004 } while (exception.retry);
4005 return err;
4006}
4007
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004008static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004010 struct nfs_server *server = NFS_SERVER(dir);
4011 struct nfs_removeargs *args = msg->rpc_argp;
4012 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004014 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004016 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004017
4018 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004021static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4022{
Al Viro884be172016-04-28 23:56:31 -04004023 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004024 &data->args.seq_args,
4025 &data->res.seq_res,
4026 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004029static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004031 struct nfs_unlinkdata *data = task->tk_calldata;
4032 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004033
Trond Myklebust14516c32010-07-31 14:29:06 -04004034 if (!nfs4_sequence_done(task, &res->seq_res))
4035 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004036 if (nfs4_async_handle_error(task, res->server, NULL,
4037 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004038 return 0;
4039 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004040 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Jeff Laytond3d41522010-09-17 17:31:57 -04004043static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4044{
4045 struct nfs_server *server = NFS_SERVER(dir);
4046 struct nfs_renameargs *arg = msg->rpc_argp;
4047 struct nfs_renameres *res = msg->rpc_resp;
4048
4049 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004050 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004051 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004052}
4053
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004054static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4055{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004056 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4057 &data->args.seq_args,
4058 &data->res.seq_res,
4059 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004060}
4061
4062static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4063 struct inode *new_dir)
4064{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004065 struct nfs_renamedata *data = task->tk_calldata;
4066 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004067
4068 if (!nfs4_sequence_done(task, &res->seq_res))
4069 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004070 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004071 return 0;
4072
4073 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004074 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004075 return 1;
4076}
4077
Al Virobeffb8f2016-07-20 16:34:42 -04004078static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004080 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 struct nfs4_link_arg arg = {
4082 .fh = NFS_FH(inode),
4083 .dir_fh = NFS_FH(dir),
4084 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004085 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004087 struct nfs4_link_res res = {
4088 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004089 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 struct rpc_message msg = {
4092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4093 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004096 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Trond Myklebust136f2622010-04-16 16:22:49 -04004098 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004099 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004100 goto out;
4101
David Quigley14c43f72013-05-22 12:50:43 -04004102 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4103 if (IS_ERR(res.label)) {
4104 status = PTR_ERR(res.label);
4105 goto out;
4106 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004107 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004108
Bryan Schumaker7c513052011-03-24 17:12:24 +00004109 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004110 if (!status) {
4111 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004112 status = nfs_post_op_update_inode(inode, res.fattr);
4113 if (!status)
4114 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004115 }
David Quigley14c43f72013-05-22 12:50:43 -04004116
4117
4118 nfs4_label_free(res.label);
4119
Trond Myklebust136f2622010-04-16 16:22:49 -04004120out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004121 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return status;
4123}
4124
Al Virobeffb8f2016-07-20 16:34:42 -04004125static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
4127 struct nfs4_exception exception = { };
4128 int err;
4129 do {
4130 err = nfs4_handle_exception(NFS_SERVER(inode),
4131 _nfs4_proc_link(inode, dir, name),
4132 &exception);
4133 } while (exception.retry);
4134 return err;
4135}
4136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004137struct nfs4_createdata {
4138 struct rpc_message msg;
4139 struct nfs4_create_arg arg;
4140 struct nfs4_create_res res;
4141 struct nfs_fh fh;
4142 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004143 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004144};
4145
4146static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004147 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004148{
4149 struct nfs4_createdata *data;
4150
4151 data = kzalloc(sizeof(*data), GFP_KERNEL);
4152 if (data != NULL) {
4153 struct nfs_server *server = NFS_SERVER(dir);
4154
David Quigley14c43f72013-05-22 12:50:43 -04004155 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4156 if (IS_ERR(data->label))
4157 goto out_free;
4158
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4160 data->msg.rpc_argp = &data->arg;
4161 data->msg.rpc_resp = &data->res;
4162 data->arg.dir_fh = NFS_FH(dir);
4163 data->arg.server = server;
4164 data->arg.name = name;
4165 data->arg.attrs = sattr;
4166 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004167 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004168 data->res.server = server;
4169 data->res.fh = &data->fh;
4170 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004171 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004172 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004173 }
4174 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004175out_free:
4176 kfree(data);
4177 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004178}
4179
4180static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4181{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004182 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004183 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004184 if (status == 0) {
4185 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004186 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 }
4188 return status;
4189}
4190
4191static void nfs4_free_createdata(struct nfs4_createdata *data)
4192{
David Quigley14c43f72013-05-22 12:50:43 -04004193 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194 kfree(data);
4195}
4196
Chuck Lever4f390c12006-08-22 20:06:22 -04004197static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004198 struct page *page, unsigned int len, struct iattr *sattr,
4199 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 struct nfs4_createdata *data;
4202 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Chuck Lever94a6d752006-08-22 20:06:23 -04004204 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 status = -ENOMEM;
4208 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4209 if (data == NULL)
4210 goto out;
4211
4212 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4213 data->arg.u.symlink.pages = &page;
4214 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004215 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004217 status = nfs4_do_create(dir, dentry, data);
4218
4219 nfs4_free_createdata(data);
4220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return status;
4222}
4223
Chuck Lever4f390c12006-08-22 20:06:22 -04004224static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004225 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
4227 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004228 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004230
4231 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004234 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4235 trace_nfs4_symlink(dir, &dentry->d_name, err);
4236 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 &exception);
4238 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004239
4240 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return err;
4242}
4243
4244static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004245 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004247 struct nfs4_createdata *data;
4248 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004250 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4251 if (data == NULL)
4252 goto out;
4253
David Quigley1775fd32013-05-22 12:50:42 -04004254 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004255 status = nfs4_do_create(dir, dentry, data);
4256
4257 nfs4_free_createdata(data);
4258out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return status;
4260}
4261
4262static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4263 struct iattr *sattr)
4264{
4265 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004266 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004268
David Quigleyaa9c2662013-05-22 12:50:44 -04004269 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4270
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004271 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004273 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4274 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4275 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 &exception);
4277 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004278 nfs4_label_release_security(label);
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return err;
4281}
4282
4283static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004284 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
David Howells2b0143b2015-03-17 22:25:59 +00004286 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 struct nfs4_readdir_arg args = {
4288 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004289 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 .pgbase = 0,
4291 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004292 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004293 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 };
4295 struct nfs4_readdir_res res;
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4298 .rpc_argp = &args,
4299 .rpc_resp = &res,
4300 .rpc_cred = cred,
4301 };
4302 int status;
4303
Al Viro6de14722013-09-16 10:53:17 -04004304 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4305 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004306 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004307 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004309 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 -05004310 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004311 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004312 status += args.pgbase;
4313 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004314
4315 nfs_invalidate_atime(dir);
4316
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return status;
4319}
4320
4321static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004322 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
4324 struct nfs4_exception exception = { };
4325 int err;
4326 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004327 err = _nfs4_proc_readdir(dentry, cred, cookie,
4328 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004329 trace_nfs4_readdir(d_inode(dentry), err);
4330 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 &exception);
4332 } while (exception.retry);
4333 return err;
4334}
4335
4336static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004337 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 struct nfs4_createdata *data;
4340 int mode = sattr->ia_mode;
4341 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004343 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4344 if (data == NULL)
4345 goto out;
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 data->arg.ftype = NF4BLK;
4351 data->arg.u.device.specdata1 = MAJOR(rdev);
4352 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 }
4354 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004355 data->arg.ftype = NF4CHR;
4356 data->arg.u.device.specdata1 = MAJOR(rdev);
4357 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004358 } else if (!S_ISSOCK(mode)) {
4359 status = -EINVAL;
4360 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
David Quigley1775fd32013-05-22 12:50:42 -04004362
David Quigleyaa9c2662013-05-22 12:50:44 -04004363 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004364 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004365out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004366 nfs4_free_createdata(data);
4367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 return status;
4369}
4370
4371static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4372 struct iattr *sattr, dev_t rdev)
4373{
4374 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004375 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004377
David Quigleyaa9c2662013-05-22 12:50:44 -04004378 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4379
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004380 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004382 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4383 trace_nfs4_mknod(dir, &dentry->d_name, err);
4384 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 &exception);
4386 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004387
4388 nfs4_label_release_security(label);
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return err;
4391}
4392
4393static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4394 struct nfs_fsstat *fsstat)
4395{
4396 struct nfs4_statfs_arg args = {
4397 .fh = fhandle,
4398 .bitmask = server->attr_bitmask,
4399 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004400 struct nfs4_statfs_res res = {
4401 .fsstat = fsstat,
4402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 struct rpc_message msg = {
4404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4405 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004406 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 };
4408
Trond Myklebust0e574af2005-10-27 22:12:38 -04004409 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004410 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
4413static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4414{
4415 struct nfs4_exception exception = { };
4416 int err;
4417 do {
4418 err = nfs4_handle_exception(server,
4419 _nfs4_proc_statfs(server, fhandle, fsstat),
4420 &exception);
4421 } while (exception.retry);
4422 return err;
4423}
4424
4425static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4426 struct nfs_fsinfo *fsinfo)
4427{
4428 struct nfs4_fsinfo_arg args = {
4429 .fh = fhandle,
4430 .bitmask = server->attr_bitmask,
4431 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004432 struct nfs4_fsinfo_res res = {
4433 .fsinfo = fsinfo,
4434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 struct rpc_message msg = {
4436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4437 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004438 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 };
4440
Bryan Schumaker7c513052011-03-24 17:12:24 +00004441 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
4444static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4445{
4446 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004447 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 int err;
4449
4450 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004451 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004452 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004453 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004454 nfs4_set_lease_period(server->nfs_client,
4455 fsinfo->lease_time * HZ,
4456 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004457 break;
4458 }
4459 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 } while (exception.retry);
4461 return err;
4462}
4463
4464static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4465{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004466 int error;
4467
Trond Myklebust0e574af2005-10-27 22:12:38 -04004468 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004469 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004470 if (error == 0) {
4471 /* block layout checks this! */
4472 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004473 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004474 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004475
4476 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
4479static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4480 struct nfs_pathconf *pathconf)
4481{
4482 struct nfs4_pathconf_arg args = {
4483 .fh = fhandle,
4484 .bitmask = server->attr_bitmask,
4485 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004486 struct nfs4_pathconf_res res = {
4487 .pathconf = pathconf,
4488 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct rpc_message msg = {
4490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4491 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004492 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 };
4494
4495 /* None of the pathconf attributes are mandatory to implement */
4496 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4497 memset(pathconf, 0, sizeof(*pathconf));
4498 return 0;
4499 }
4500
Trond Myklebust0e574af2005-10-27 22:12:38 -04004501 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004502 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
4505static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4506 struct nfs_pathconf *pathconf)
4507{
4508 struct nfs4_exception exception = { };
4509 int err;
4510
4511 do {
4512 err = nfs4_handle_exception(server,
4513 _nfs4_proc_pathconf(server, fhandle, pathconf),
4514 &exception);
4515 } while (exception.retry);
4516 return err;
4517}
4518
Trond Myklebust5521abf2013-03-16 20:54:34 -04004519int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004520 const struct nfs_open_context *ctx,
4521 const struct nfs_lock_context *l_ctx,
4522 fmode_t fmode)
4523{
4524 const struct nfs_lockowner *lockowner = NULL;
4525
4526 if (l_ctx != NULL)
4527 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004528 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004529}
4530EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4531
Trond Myklebust5521abf2013-03-16 20:54:34 -04004532static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4533 const struct nfs_open_context *ctx,
4534 const struct nfs_lock_context *l_ctx,
4535 fmode_t fmode)
4536{
4537 nfs4_stateid current_stateid;
4538
Trond Myklebuste1253be2014-03-05 08:44:23 -05004539 /* If the current stateid represents a lost lock, then exit */
4540 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4541 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004542 return nfs4_stateid_match(stateid, &current_stateid);
4543}
4544
4545static bool nfs4_error_stateid_expired(int err)
4546{
4547 switch (err) {
4548 case -NFS4ERR_DELEG_REVOKED:
4549 case -NFS4ERR_ADMIN_REVOKED:
4550 case -NFS4ERR_BAD_STATEID:
4551 case -NFS4ERR_STALE_STATEID:
4552 case -NFS4ERR_OLD_STATEID:
4553 case -NFS4ERR_OPENMODE:
4554 case -NFS4ERR_EXPIRED:
4555 return true;
4556 }
4557 return false;
4558}
4559
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004560void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004561{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004562 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004563}
4564
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004565static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004567 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004569 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004570 if (task->tk_status < 0) {
4571 struct nfs4_exception exception = {
4572 .inode = hdr->inode,
4573 .state = hdr->args.context->state,
4574 .stateid = &hdr->args.stateid,
4575 };
4576 task->tk_status = nfs4_async_handle_exception(task,
4577 server, task->tk_status, &exception);
4578 if (exception.retry) {
4579 rpc_restart_call_prepare(task);
4580 return -EAGAIN;
4581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004583
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004584 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004586 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Trond Myklebust5521abf2013-03-16 20:54:34 -04004590static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004591 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004592{
4593
4594 if (!nfs4_error_stateid_expired(task->tk_status) ||
4595 nfs4_stateid_is_current(&args->stateid,
4596 args->context,
4597 args->lock_context,
4598 FMODE_READ))
4599 return false;
4600 rpc_restart_call_prepare(task);
4601 return true;
4602}
4603
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004604static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004605{
4606
4607 dprintk("--> %s\n", __func__);
4608
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004609 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004610 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004611 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004612 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004613 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4614 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004615}
4616
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004617static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4618 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004621 if (!hdr->pgio_done_cb)
4622 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004623 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004624 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625}
4626
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4628 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004629{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4631 &hdr->args.seq_args,
4632 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004633 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004634 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4636 hdr->args.lock_context,
4637 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004638 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004639 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004640 return -EIO;
4641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004644static int nfs4_write_done_cb(struct rpc_task *task,
4645 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004647 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004648
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004650 if (task->tk_status < 0) {
4651 struct nfs4_exception exception = {
4652 .inode = hdr->inode,
4653 .state = hdr->args.context->state,
4654 .stateid = &hdr->args.stateid,
4655 };
4656 task->tk_status = nfs4_async_handle_exception(task,
4657 NFS_SERVER(inode), task->tk_status,
4658 &exception);
4659 if (exception.retry) {
4660 rpc_restart_call_prepare(task);
4661 return -EAGAIN;
4662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004664 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004666 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004667 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
Trond Myklebust5521abf2013-03-16 20:54:34 -04004671static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004672 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004673{
4674
4675 if (!nfs4_error_stateid_expired(task->tk_status) ||
4676 nfs4_stateid_is_current(&args->stateid,
4677 args->context,
4678 args->lock_context,
4679 FMODE_WRITE))
4680 return false;
4681 rpc_restart_call_prepare(task);
4682 return true;
4683}
4684
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004686{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004688 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004690 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4692 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004693}
4694
Trond Myklebust5a37f852012-04-28 14:55:16 -04004695static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004697{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004698 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004700 return false;
4701 /* Otherwise, request attributes if and only if we don't hold
4702 * a delegation
4703 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004704 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004705}
Fred Isamana69aef12011-03-03 15:13:47 +00004706
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4708 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004710 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004711
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4713 hdr->args.bitmask = NULL;
4714 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004715 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004716 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004717
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004718 if (!hdr->pgio_done_cb)
4719 hdr->pgio_done_cb = nfs4_write_done_cb;
4720 hdr->res.server = server;
4721 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004723 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
Fred Isaman0b7c0152012-04-20 14:47:39 -04004727static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4728{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004729 nfs4_setup_sequence(NFS_SERVER(data->inode),
4730 &data->args.seq_args,
4731 &data->res.seq_res,
4732 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733}
4734
Fred Isaman0b7c0152012-04-20 14:47:39 -04004735static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004738
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004739 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004740 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4741 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004742 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004743 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
Fred Isaman0b7c0152012-04-20 14:47:39 -04004748static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004749{
4750 if (!nfs4_sequence_done(task, &data->res.seq_res))
4751 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004752 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004753}
4754
Fred Isaman0b7c0152012-04-20 14:47:39 -04004755static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004757 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004758
Fred Isaman0b7c0152012-04-20 14:47:39 -04004759 if (data->commit_done_cb == NULL)
4760 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004761 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004762 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004763 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004766struct nfs4_renewdata {
4767 struct nfs_client *client;
4768 unsigned long timestamp;
4769};
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771/*
4772 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4773 * standalone procedure for queueing an asynchronous RENEW.
4774 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004775static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004776{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004777 struct nfs4_renewdata *data = calldata;
4778 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004779
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004780 if (atomic_read(&clp->cl_count) > 1)
4781 nfs4_schedule_state_renewal(clp);
4782 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004783 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004784}
4785
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004786static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004788 struct nfs4_renewdata *data = calldata;
4789 struct nfs_client *clp = data->client;
4790 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004792 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004793 switch (task->tk_status) {
4794 case 0:
4795 break;
4796 case -NFS4ERR_LEASE_MOVED:
4797 nfs4_schedule_lease_moved_recovery(clp);
4798 break;
4799 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004800 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004801 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4802 return;
4803 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004804 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004805 return;
4806 }
4807 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004809 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004812static const struct rpc_call_ops nfs4_renew_ops = {
4813 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004814 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004815};
4816
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004817static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
4819 struct rpc_message msg = {
4820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4821 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004822 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004824 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004826 if (renew_flags == 0)
4827 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004828 if (!atomic_inc_not_zero(&clp->cl_count))
4829 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004830 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004831 if (data == NULL)
4832 return -ENOMEM;
4833 data->client = clp;
4834 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004835 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004836 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837}
4838
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004839static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
4841 struct rpc_message msg = {
4842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4843 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004844 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 };
4846 unsigned long now = jiffies;
4847 int status;
4848
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004849 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 if (status < 0)
4851 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004852 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 return 0;
4854}
4855
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004856static inline int nfs4_server_supports_acls(struct nfs_server *server)
4857{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004858 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004859}
4860
Trond Myklebust21f498c2012-08-24 10:59:25 -04004861/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4862 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004863 * the stack.
4864 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004865#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004866
Neil Hormane9e3d722011-03-04 19:26:03 -05004867static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004868 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004869{
4870 struct page *newpage, **spages;
4871 int rc = 0;
4872 size_t len;
4873 spages = pages;
4874
4875 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004876 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004877 newpage = alloc_page(GFP_KERNEL);
4878
4879 if (newpage == NULL)
4880 goto unwind;
4881 memcpy(page_address(newpage), buf, len);
4882 buf += len;
4883 buflen -= len;
4884 *pages++ = newpage;
4885 rc++;
4886 } while (buflen != 0);
4887
4888 return rc;
4889
4890unwind:
4891 for(; rc > 0; rc--)
4892 __free_page(spages[rc-1]);
4893 return -ENOMEM;
4894}
4895
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004896struct nfs4_cached_acl {
4897 int cached;
4898 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004899 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004900};
4901
4902static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004903{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004904 struct nfs_inode *nfsi = NFS_I(inode);
4905
4906 spin_lock(&inode->i_lock);
4907 kfree(nfsi->nfs4_acl);
4908 nfsi->nfs4_acl = acl;
4909 spin_unlock(&inode->i_lock);
4910}
4911
4912static void nfs4_zap_acl_attr(struct inode *inode)
4913{
4914 nfs4_set_cached_acl(inode, NULL);
4915}
4916
4917static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4918{
4919 struct nfs_inode *nfsi = NFS_I(inode);
4920 struct nfs4_cached_acl *acl;
4921 int ret = -ENOENT;
4922
4923 spin_lock(&inode->i_lock);
4924 acl = nfsi->nfs4_acl;
4925 if (acl == NULL)
4926 goto out;
4927 if (buf == NULL) /* user is just asking for length */
4928 goto out_len;
4929 if (acl->cached == 0)
4930 goto out;
4931 ret = -ERANGE; /* see getxattr(2) man page */
4932 if (acl->len > buflen)
4933 goto out;
4934 memcpy(buf, acl->data, acl->len);
4935out_len:
4936 ret = acl->len;
4937out:
4938 spin_unlock(&inode->i_lock);
4939 return ret;
4940}
4941
Sachin Prabhu5794d212012-04-17 14:36:40 +01004942static 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 +00004943{
4944 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004945 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004946
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004947 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004948 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004949 if (acl == NULL)
4950 goto out;
4951 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004952 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004953 } else {
4954 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4955 if (acl == NULL)
4956 goto out;
4957 acl->cached = 0;
4958 }
4959 acl->len = acl_len;
4960out:
4961 nfs4_set_cached_acl(inode, acl);
4962}
4963
Andy Adamsonbf118a32011-12-07 11:55:27 -05004964/*
4965 * The getxattr API returns the required buffer length when called with a
4966 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4967 * the required buf. On a NULL buf, we send a page of data to the server
4968 * guessing that the ACL request can be serviced by a page. If so, we cache
4969 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4970 * the cache. If not so, we throw away the page, and cache the required
4971 * length. The next getxattr call will then produce another round trip to
4972 * the server, this time with the input buf of the required size.
4973 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004974static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004975{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004976 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004977 struct nfs_getaclargs args = {
4978 .fh = NFS_FH(inode),
4979 .acl_pages = pages,
4980 .acl_len = buflen,
4981 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004982 struct nfs_getaclres res = {
4983 .acl_len = buflen,
4984 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004985 struct rpc_message msg = {
4986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4987 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004988 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004989 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004990 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4991 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004992
Andy Adamsonbf118a32011-12-07 11:55:27 -05004993 /* As long as we're doing a round trip to the server anyway,
4994 * let's be prepared for a page of acl data. */
4995 if (npages == 0)
4996 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004997 if (npages > ARRAY_SIZE(pages))
4998 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004999
Andy Adamsonbf118a32011-12-07 11:55:27 -05005000 for (i = 0; i < npages; i++) {
5001 pages[i] = alloc_page(GFP_KERNEL);
5002 if (!pages[i])
5003 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005004 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005005
5006 /* for decoding across pages */
5007 res.acl_scratch = alloc_page(GFP_KERNEL);
5008 if (!res.acl_scratch)
5009 goto out_free;
5010
Andy Adamsonbf118a32011-12-07 11:55:27 -05005011 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005012
Peng Taode040be2012-01-10 22:42:47 +08005013 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005014 __func__, buf, buflen, npages, args.acl_len);
5015 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5016 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005017 if (ret)
5018 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005019
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005020 /* Handle the case where the passed-in buffer is too short */
5021 if (res.acl_flags & NFS4_ACL_TRUNC) {
5022 /* Did the user only issue a request for the acl length? */
5023 if (buf == NULL)
5024 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005025 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005026 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005027 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005028 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005029 if (buf) {
5030 if (res.acl_len > buflen) {
5031 ret = -ERANGE;
5032 goto out_free;
5033 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005034 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005035 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005036out_ok:
5037 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005038out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005039 for (i = 0; i < npages; i++)
5040 if (pages[i])
5041 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005042 if (res.acl_scratch)
5043 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005044 return ret;
5045}
5046
Trond Myklebust16b42892006-08-24 12:27:15 -04005047static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5048{
5049 struct nfs4_exception exception = { };
5050 ssize_t ret;
5051 do {
5052 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005053 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005054 if (ret >= 0)
5055 break;
5056 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5057 } while (exception.retry);
5058 return ret;
5059}
5060
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005061static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5062{
5063 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005064 int ret;
5065
5066 if (!nfs4_server_supports_acls(server))
5067 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005068 ret = nfs_revalidate_inode(server, inode);
5069 if (ret < 0)
5070 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005071 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5072 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005073 ret = nfs4_read_cached_acl(inode, buf, buflen);
5074 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005075 /* -ENOENT is returned if there is no ACL or if there is an ACL
5076 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005077 return ret;
5078 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005079}
5080
Trond Myklebust16b42892006-08-24 12:27:15 -04005081static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005082{
5083 struct nfs_server *server = NFS_SERVER(inode);
5084 struct page *pages[NFS4ACL_MAXPAGES];
5085 struct nfs_setaclargs arg = {
5086 .fh = NFS_FH(inode),
5087 .acl_pages = pages,
5088 .acl_len = buflen,
5089 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005090 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005091 struct rpc_message msg = {
5092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5093 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005094 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005095 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005096 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005097 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005098
5099 if (!nfs4_server_supports_acls(server))
5100 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005101 if (npages > ARRAY_SIZE(pages))
5102 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005103 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005104 if (i < 0)
5105 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005106 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005107 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005108
5109 /*
5110 * Free each page after tx, so the only ref left is
5111 * held by the network stack
5112 */
5113 for (; i > 0; i--)
5114 put_page(pages[i-1]);
5115
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005116 /*
5117 * Acl update can result in inode attribute update.
5118 * so mark the attribute cache invalid.
5119 */
5120 spin_lock(&inode->i_lock);
5121 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5122 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005123 nfs_access_zap_cache(inode);
5124 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005125 return ret;
5126}
5127
Trond Myklebust16b42892006-08-24 12:27:15 -04005128static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5129{
5130 struct nfs4_exception exception = { };
5131 int err;
5132 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005133 err = __nfs4_proc_set_acl(inode, buf, buflen);
5134 trace_nfs4_set_acl(inode, err);
5135 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005136 &exception);
5137 } while (exception.retry);
5138 return err;
5139}
5140
David Quigleyaa9c2662013-05-22 12:50:44 -04005141#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5142static int _nfs4_get_security_label(struct inode *inode, void *buf,
5143 size_t buflen)
5144{
5145 struct nfs_server *server = NFS_SERVER(inode);
5146 struct nfs_fattr fattr;
5147 struct nfs4_label label = {0, 0, buflen, buf};
5148
5149 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005150 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005151 .fh = NFS_FH(inode),
5152 .bitmask = bitmask,
5153 };
5154 struct nfs4_getattr_res res = {
5155 .fattr = &fattr,
5156 .label = &label,
5157 .server = server,
5158 };
5159 struct rpc_message msg = {
5160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005161 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005162 .rpc_resp = &res,
5163 };
5164 int ret;
5165
5166 nfs_fattr_init(&fattr);
5167
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005168 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005169 if (ret)
5170 return ret;
5171 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5172 return -ENOENT;
5173 if (buflen < label.len)
5174 return -ERANGE;
5175 return 0;
5176}
5177
5178static int nfs4_get_security_label(struct inode *inode, void *buf,
5179 size_t buflen)
5180{
5181 struct nfs4_exception exception = { };
5182 int err;
5183
5184 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5185 return -EOPNOTSUPP;
5186
5187 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005188 err = _nfs4_get_security_label(inode, buf, buflen);
5189 trace_nfs4_get_security_label(inode, err);
5190 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005191 &exception);
5192 } while (exception.retry);
5193 return err;
5194}
5195
5196static int _nfs4_do_set_security_label(struct inode *inode,
5197 struct nfs4_label *ilabel,
5198 struct nfs_fattr *fattr,
5199 struct nfs4_label *olabel)
5200{
5201
5202 struct iattr sattr = {0};
5203 struct nfs_server *server = NFS_SERVER(inode);
5204 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005205 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005206 .fh = NFS_FH(inode),
5207 .iap = &sattr,
5208 .server = server,
5209 .bitmask = bitmask,
5210 .label = ilabel,
5211 };
5212 struct nfs_setattrres res = {
5213 .fattr = fattr,
5214 .label = olabel,
5215 .server = server,
5216 };
5217 struct rpc_message msg = {
5218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005219 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005220 .rpc_resp = &res,
5221 };
5222 int status;
5223
Jeff Layton12207f62013-11-01 10:49:32 -04005224 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005225
Jeff Layton12207f62013-11-01 10:49:32 -04005226 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005227 if (status)
5228 dprintk("%s failed: %d\n", __func__, status);
5229
5230 return status;
5231}
5232
5233static int nfs4_do_set_security_label(struct inode *inode,
5234 struct nfs4_label *ilabel,
5235 struct nfs_fattr *fattr,
5236 struct nfs4_label *olabel)
5237{
5238 struct nfs4_exception exception = { };
5239 int err;
5240
5241 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005242 err = _nfs4_do_set_security_label(inode, ilabel,
5243 fattr, olabel);
5244 trace_nfs4_set_security_label(inode, err);
5245 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005246 &exception);
5247 } while (exception.retry);
5248 return err;
5249}
5250
5251static int
Al Viro59301222016-05-27 10:19:30 -04005252nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005253{
5254 struct nfs4_label ilabel, *olabel = NULL;
5255 struct nfs_fattr fattr;
5256 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005257 int status;
5258
5259 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5260 return -EOPNOTSUPP;
5261
5262 nfs_fattr_init(&fattr);
5263
5264 ilabel.pi = 0;
5265 ilabel.lfs = 0;
5266 ilabel.label = (char *)buf;
5267 ilabel.len = buflen;
5268
5269 cred = rpc_lookup_cred();
5270 if (IS_ERR(cred))
5271 return PTR_ERR(cred);
5272
5273 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5274 if (IS_ERR(olabel)) {
5275 status = -PTR_ERR(olabel);
5276 goto out;
5277 }
5278
5279 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5280 if (status == 0)
5281 nfs_setsecurity(inode, &fattr, olabel);
5282
5283 nfs4_label_free(olabel);
5284out:
5285 put_rpccred(cred);
5286 return status;
5287}
5288#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5289
5290
Chuck Leverf0920752012-05-21 22:45:41 -04005291static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5292 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005293{
5294 __be32 verf[2];
5295
Chuck Lever2c820d92012-05-21 22:45:33 -04005296 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5297 /* An impossible timestamp guarantees this value
5298 * will never match a generated boot time. */
5299 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005300 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005301 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005302 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005303 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5304 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005305 }
Chuck Levercd937102012-03-02 17:14:31 -05005306 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5307}
5308
Jeff Laytona3192682015-06-09 19:43:59 -04005309static int
5310nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005311{
Jeff Laytona3192682015-06-09 19:43:59 -04005312 size_t len;
5313 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005314
Trond Myklebustceb3a162015-01-03 15:16:04 -05005315 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005316 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005317
Jeff Laytona3192682015-06-09 19:43:59 -04005318 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005319 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005320 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5321 1 +
5322 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5323 1;
5324 rcu_read_unlock();
5325
5326 if (len > NFS4_OPAQUE_LIMIT + 1)
5327 return -EINVAL;
5328
5329 /*
5330 * Since this string is allocated at mount time, and held until the
5331 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5332 * about a memory-reclaim deadlock.
5333 */
5334 str = kmalloc(len, GFP_KERNEL);
5335 if (!str)
5336 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005337
Chuck Levere984a552012-09-14 17:24:21 -04005338 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005339 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005340 clp->cl_ipaddr,
5341 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5342 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005343 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005344
Jeff Laytona3192682015-06-09 19:43:59 -04005345 clp->cl_owner_id = str;
5346 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005347}
5348
Jeff Layton873e3852015-06-09 19:44:00 -04005349static int
5350nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005351{
Jeff Layton873e3852015-06-09 19:44:00 -04005352 size_t len;
5353 char *str;
5354
5355 len = 10 + 10 + 1 + 10 + 1 +
5356 strlen(nfs4_client_id_uniquifier) + 1 +
5357 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5358
5359 if (len > NFS4_OPAQUE_LIMIT + 1)
5360 return -EINVAL;
5361
5362 /*
5363 * Since this string is allocated at mount time, and held until the
5364 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5365 * about a memory-reclaim deadlock.
5366 */
5367 str = kmalloc(len, GFP_KERNEL);
5368 if (!str)
5369 return -ENOMEM;
5370
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005371 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005372 clp->rpc_ops->version, clp->cl_minorversion,
5373 nfs4_client_id_uniquifier,
5374 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005375 clp->cl_owner_id = str;
5376 return 0;
5377}
5378
5379static int
5380nfs4_init_uniform_client_string(struct nfs_client *clp)
5381{
Jeff Layton873e3852015-06-09 19:44:00 -04005382 size_t len;
5383 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005384
5385 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005386 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005387
5388 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005389 return nfs4_init_uniquifier_client_string(clp);
5390
5391 len = 10 + 10 + 1 + 10 + 1 +
5392 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5393
5394 if (len > NFS4_OPAQUE_LIMIT + 1)
5395 return -EINVAL;
5396
5397 /*
5398 * Since this string is allocated at mount time, and held until the
5399 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5400 * about a memory-reclaim deadlock.
5401 */
5402 str = kmalloc(len, GFP_KERNEL);
5403 if (!str)
5404 return -ENOMEM;
5405
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005406 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005407 clp->rpc_ops->version, clp->cl_minorversion,
5408 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005409 clp->cl_owner_id = str;
5410 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005411}
5412
Chuck Lever706cb8d2014-03-12 12:51:47 -04005413/*
5414 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5415 * services. Advertise one based on the address family of the
5416 * clientaddr.
5417 */
5418static unsigned int
5419nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5420{
5421 if (strchr(clp->cl_ipaddr, ':') != NULL)
5422 return scnprintf(buf, len, "tcp6");
5423 else
5424 return scnprintf(buf, len, "tcp");
5425}
5426
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005427static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5428{
5429 struct nfs4_setclientid *sc = calldata;
5430
5431 if (task->tk_status == 0)
5432 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5433}
5434
5435static const struct rpc_call_ops nfs4_setclientid_ops = {
5436 .rpc_call_done = nfs4_setclientid_done,
5437};
5438
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005439/**
5440 * nfs4_proc_setclientid - Negotiate client ID
5441 * @clp: state data structure
5442 * @program: RPC program for NFSv4 callback service
5443 * @port: IP port number for NFS4 callback service
5444 * @cred: RPC credential to use for this call
5445 * @res: where to place the result
5446 *
5447 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5448 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005449int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5450 unsigned short port, struct rpc_cred *cred,
5451 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452{
5453 nfs4_verifier sc_verifier;
5454 struct nfs4_setclientid setclientid = {
5455 .sc_verifier = &sc_verifier,
5456 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005457 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 };
5459 struct rpc_message msg = {
5460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5461 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005462 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005463 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005465 struct rpc_task *task;
5466 struct rpc_task_setup task_setup_data = {
5467 .rpc_client = clp->cl_rpcclient,
5468 .rpc_message = &msg,
5469 .callback_ops = &nfs4_setclientid_ops,
5470 .callback_data = &setclientid,
5471 .flags = RPC_TASK_TIMEOUT,
5472 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005473 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Chuck Leverde734832012-07-11 16:30:50 -04005475 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005476 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005477
5478 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5479 status = nfs4_init_uniform_client_string(clp);
5480 else
Jeff Laytona3192682015-06-09 19:43:59 -04005481 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005482
5483 if (status)
5484 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005485
Chuck Leverde734832012-07-11 16:30:50 -04005486 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005487 setclientid.sc_netid_len =
5488 nfs4_init_callback_netid(clp,
5489 setclientid.sc_netid,
5490 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005491 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005492 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 clp->cl_ipaddr, port >> 8, port & 255);
5494
Jeff Layton3a6bb732015-06-09 19:43:57 -04005495 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005496 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005497 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005498 task = rpc_run_task(&task_setup_data);
5499 if (IS_ERR(task)) {
5500 status = PTR_ERR(task);
5501 goto out;
5502 }
5503 status = task->tk_status;
5504 if (setclientid.sc_cred) {
5505 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5506 put_rpccred(setclientid.sc_cred);
5507 }
5508 rpc_put_task(task);
5509out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005510 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005511 dprintk("NFS reply setclientid: %d\n", status);
5512 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513}
5514
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005515/**
5516 * nfs4_proc_setclientid_confirm - Confirm client ID
5517 * @clp: state data structure
5518 * @res: result of a previous SETCLIENTID
5519 * @cred: RPC credential to use for this call
5520 *
5521 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5522 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005523int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005524 struct nfs4_setclientid_res *arg,
5525 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 struct rpc_message msg = {
5528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005529 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005530 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 int status;
5533
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005534 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5535 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5536 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005537 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005538 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005539 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 return status;
5541}
5542
Trond Myklebustfe650402006-01-03 09:55:18 +01005543struct nfs4_delegreturndata {
5544 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005545 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005546 struct nfs_fh fh;
5547 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005548 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005549 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005550 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005551 struct inode *inode;
5552 bool roc;
5553 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005554};
5555
Trond Myklebustfe650402006-01-03 09:55:18 +01005556static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5557{
5558 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005559
Trond Myklebust14516c32010-07-31 14:29:06 -04005560 if (!nfs4_sequence_done(task, &data->res.seq_res))
5561 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005562
Trond Myklebustca8acf82013-08-13 10:36:56 -04005563 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005564 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005565 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005566 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005567 case -NFS4ERR_ADMIN_REVOKED:
5568 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005569 case -NFS4ERR_EXPIRED:
5570 nfs4_free_revoked_stateid(data->res.server,
5571 data->args.stateid,
5572 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005573 case -NFS4ERR_BAD_STATEID:
5574 case -NFS4ERR_OLD_STATEID:
5575 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005576 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005577 if (data->roc)
5578 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005579 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005580 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005581 if (nfs4_async_handle_error(task, data->res.server,
5582 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005583 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005584 return;
5585 }
5586 }
5587 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005588}
5589
5590static void nfs4_delegreturn_release(void *calldata)
5591{
Peng Tao039b7562014-07-03 13:05:02 +08005592 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005593 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005594
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005595 if (inode) {
5596 if (data->roc)
5597 pnfs_roc_release(inode);
5598 nfs_iput_and_deactive(inode);
5599 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005600 kfree(calldata);
5601}
5602
Andy Adamson938e1012009-04-01 09:22:28 -04005603static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5604{
5605 struct nfs4_delegreturndata *d_data;
5606
5607 d_data = (struct nfs4_delegreturndata *)data;
5608
Peng Tao500d7012015-09-22 11:35:22 +08005609 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5610 return;
5611
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005612 if (d_data->roc)
5613 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005614
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005615 nfs4_setup_sequence(d_data->res.server,
5616 &d_data->args.seq_args,
5617 &d_data->res.seq_res,
5618 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005619}
Andy Adamson938e1012009-04-01 09:22:28 -04005620
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005621static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005622 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005623 .rpc_call_done = nfs4_delegreturn_done,
5624 .rpc_release = nfs4_delegreturn_release,
5625};
5626
Trond Myklebuste6f81072008-01-24 18:14:34 -05005627static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005628{
5629 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005630 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005631 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005632 struct rpc_message msg = {
5633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5634 .rpc_cred = cred,
5635 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005636 struct rpc_task_setup task_setup_data = {
5637 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005638 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005639 .callback_ops = &nfs4_delegreturn_ops,
5640 .flags = RPC_TASK_ASYNC,
5641 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005642 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005643
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005644 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005645 if (data == NULL)
5646 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005647 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005648
5649 nfs4_state_protect(server->nfs_client,
5650 NFS_SP4_MACH_CRED_CLEANUP,
5651 &task_setup_data.rpc_client, &msg);
5652
Trond Myklebustfe650402006-01-03 09:55:18 +01005653 data->args.fhandle = &data->fh;
5654 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005655 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005656 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005657 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005658 data->res.fattr = &data->fattr;
5659 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005660 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005661 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005662 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005663 data->inode = nfs_igrab_and_active(inode);
5664 if (data->inode)
5665 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005666
Trond Myklebustc970aa82007-07-14 15:39:59 -04005667 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005668 msg.rpc_argp = &data->args;
5669 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005670 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005671 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005672 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005673 if (!issync)
5674 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005675 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005676 if (status != 0)
5677 goto out;
5678 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005679 if (status == 0)
5680 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5681 else
5682 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005683out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005684 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005685 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686}
5687
Trond Myklebuste6f81072008-01-24 18:14:34 -05005688int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
5690 struct nfs_server *server = NFS_SERVER(inode);
5691 struct nfs4_exception exception = { };
5692 int err;
5693 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005694 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005695 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 switch (err) {
5697 case -NFS4ERR_STALE_STATEID:
5698 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 case 0:
5700 return 0;
5701 }
5702 err = nfs4_handle_exception(server, err, &exception);
5703 } while (exception.retry);
5704 return err;
5705}
5706
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5708{
5709 struct inode *inode = state->inode;
5710 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005711 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005712 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005714 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005716 struct nfs_lockt_res res = {
5717 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 };
5719 struct rpc_message msg = {
5720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5721 .rpc_argp = &arg,
5722 .rpc_resp = &res,
5723 .rpc_cred = state->owner->so_cred,
5724 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 struct nfs4_lock_state *lsp;
5726 int status;
5727
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005728 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005729 status = nfs4_set_lock_state(state, request);
5730 if (status != 0)
5731 goto out;
5732 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005733 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005734 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005735 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005736 switch (status) {
5737 case 0:
5738 request->fl_type = F_UNLCK;
5739 break;
5740 case -NFS4ERR_DENIED:
5741 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005743 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005744 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005745out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 return status;
5747}
5748
5749static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5750{
5751 struct nfs4_exception exception = { };
5752 int err;
5753
5754 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005755 err = _nfs4_proc_getlk(state, cmd, request);
5756 trace_nfs4_get_lock(request, state, cmd, err);
5757 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 &exception);
5759 } while (exception.retry);
5760 return err;
5761}
5762
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005763struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005764 struct nfs_locku_args arg;
5765 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005766 struct nfs4_lock_state *lsp;
5767 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005768 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005769 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005770 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005771};
5772
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005773static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5774 struct nfs_open_context *ctx,
5775 struct nfs4_lock_state *lsp,
5776 struct nfs_seqid *seqid)
5777{
5778 struct nfs4_unlockdata *p;
5779 struct inode *inode = lsp->ls_state->inode;
5780
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005781 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005782 if (p == NULL)
5783 return NULL;
5784 p->arg.fh = NFS_FH(inode);
5785 p->arg.fl = &p->fl;
5786 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005787 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005788 p->lsp = lsp;
5789 atomic_inc(&lsp->ls_count);
5790 /* Ensure we don't close file until we're done freeing locks! */
5791 p->ctx = get_nfs_open_context(ctx);
5792 memcpy(&p->fl, fl, sizeof(p->fl));
5793 p->server = NFS_SERVER(inode);
5794 return p;
5795}
5796
Trond Myklebust06f814a2006-01-03 09:55:07 +01005797static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005798{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005799 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005800 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005801 nfs4_put_lock_state(calldata->lsp);
5802 put_nfs_open_context(calldata->ctx);
5803 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005804}
5805
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005806static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005807{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005808 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005809
Trond Myklebust14516c32010-07-31 14:29:06 -04005810 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5811 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005812 switch (task->tk_status) {
5813 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005814 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005815 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005816 if (nfs4_update_lock_stateid(calldata->lsp,
5817 &calldata->res.stateid))
5818 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005819 case -NFS4ERR_ADMIN_REVOKED:
5820 case -NFS4ERR_EXPIRED:
5821 nfs4_free_revoked_stateid(calldata->server,
5822 &calldata->arg.stateid,
5823 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005824 case -NFS4ERR_BAD_STATEID:
5825 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005827 if (!nfs4_stateid_match(&calldata->arg.stateid,
5828 &calldata->lsp->ls_stateid))
5829 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005830 break;
5831 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005832 if (nfs4_async_handle_error(task, calldata->server,
5833 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005834 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005835 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005836 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005837}
5838
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005839static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005840{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005841 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005843 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005844 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005845 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005846 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005847 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005848 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005849 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005850 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005851 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005852 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005853 &calldata->res.seq_res,
5854 task) != 0)
5855 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005856 return;
5857out_no_action:
5858 task->tk_action = NULL;
5859out_wait:
5860 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861}
5862
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005863static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005864 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005865 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005866 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005867};
5868
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005869static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5870 struct nfs_open_context *ctx,
5871 struct nfs4_lock_state *lsp,
5872 struct nfs_seqid *seqid)
5873{
5874 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005875 struct rpc_message msg = {
5876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5877 .rpc_cred = ctx->cred,
5878 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005879 struct rpc_task_setup task_setup_data = {
5880 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005881 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005882 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005883 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005884 .flags = RPC_TASK_ASYNC,
5885 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005886
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005887 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5888 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5889
Frank Filz137d6ac2007-07-09 15:32:29 -07005890 /* Ensure this is an unlock - when canceling a lock, the
5891 * canceled lock is passed in, and it won't be an unlock.
5892 */
5893 fl->fl_type = F_UNLCK;
5894
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5896 if (data == NULL) {
5897 nfs_free_seqid(seqid);
5898 return ERR_PTR(-ENOMEM);
5899 }
5900
Chuck Levera9c92d62013-08-09 12:48:18 -04005901 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005902 msg.rpc_argp = &data->arg;
5903 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005904 task_setup_data.callback_data = data;
5905 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005906}
5907
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5909{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005910 struct inode *inode = state->inode;
5911 struct nfs4_state_owner *sp = state->owner;
5912 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005913 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005914 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005915 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005916 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005917 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005918 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Trond Myklebust9b073572006-06-29 16:38:34 -04005920 status = nfs4_set_lock_state(state, request);
5921 /* Unlock _before_ we do the RPC call */
5922 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005923 /* Exclude nfs_delegation_claim_locks() */
5924 mutex_lock(&sp->so_delegreturn_mutex);
5925 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005926 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005927 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005928 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005929 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005930 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005931 }
5932 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005933 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005934 if (status != 0)
5935 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005936 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005937 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005938 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5939 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005940 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5941 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005942 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005943 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005944 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005945 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005946 status = PTR_ERR(task);
5947 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005948 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005950 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005951out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005952 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005953 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005954 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955}
5956
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005957struct nfs4_lockdata {
5958 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005959 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005960 struct nfs4_lock_state *lsp;
5961 struct nfs_open_context *ctx;
5962 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005963 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005964 int rpc_status;
5965 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005966 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005967};
5968
5969static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005970 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5971 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005972{
5973 struct nfs4_lockdata *p;
5974 struct inode *inode = lsp->ls_state->inode;
5975 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005976 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005978 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979 if (p == NULL)
5980 return NULL;
5981
5982 p->arg.fh = NFS_FH(inode);
5983 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005984 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005985 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005986 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005987 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5988 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005989 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005990 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005991 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005992 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005993 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005994 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005996 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005997 atomic_inc(&lsp->ls_count);
5998 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005999 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000 memcpy(&p->fl, fl, sizeof(p->fl));
6001 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006002out_free_seqid:
6003 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004out_free:
6005 kfree(p);
6006 return NULL;
6007}
6008
6009static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6010{
6011 struct nfs4_lockdata *data = calldata;
6012 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
Harvey Harrison3110ff82008-05-02 13:42:44 -07006014 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006015 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006016 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006017 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006018 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006019 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006020 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006021 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006022 nfs4_stateid_copy(&data->arg.open_stateid,
6023 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006024 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006025 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006026 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006027 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006028 nfs4_stateid_copy(&data->arg.lock_stateid,
6029 &data->lsp->ls_stateid);
6030 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006031 if (!nfs4_valid_open_stateid(state)) {
6032 data->rpc_status = -EBADF;
6033 task->tk_action = NULL;
6034 goto out_release_open_seqid;
6035 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006036 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006037 if (nfs4_setup_sequence(data->server,
6038 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006039 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006040 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006041 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006042out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006043 nfs_release_seqid(data->arg.open_seqid);
6044out_release_lock_seqid:
6045 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006046out_wait:
6047 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006048 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006049}
6050
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6052{
6053 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006054 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006055
Harvey Harrison3110ff82008-05-02 13:42:44 -07006056 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006057
Trond Myklebust14516c32010-07-31 14:29:06 -04006058 if (!nfs4_sequence_done(task, &data->res.seq_res))
6059 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006060
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006061 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006062 switch (task->tk_status) {
6063 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006064 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006065 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006066 if (data->arg.new_lock) {
6067 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006068 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006069 rpc_restart_call_prepare(task);
6070 break;
6071 }
6072 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006073 if (data->arg.new_lock_owner != 0) {
6074 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6075 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6076 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6077 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6078 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006079 break;
6080 case -NFS4ERR_BAD_STATEID:
6081 case -NFS4ERR_OLD_STATEID:
6082 case -NFS4ERR_STALE_STATEID:
6083 case -NFS4ERR_EXPIRED:
6084 if (data->arg.new_lock_owner != 0) {
6085 if (!nfs4_stateid_match(&data->arg.open_stateid,
6086 &lsp->ls_state->open_stateid))
6087 rpc_restart_call_prepare(task);
6088 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6089 &lsp->ls_stateid))
6090 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006092 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006093}
6094
6095static void nfs4_lock_release(void *calldata)
6096{
6097 struct nfs4_lockdata *data = calldata;
6098
Harvey Harrison3110ff82008-05-02 13:42:44 -07006099 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006100 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101 if (data->cancelled != 0) {
6102 struct rpc_task *task;
6103 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6104 data->arg.lock_seqid);
6105 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006106 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006107 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006108 } else
6109 nfs_free_seqid(data->arg.lock_seqid);
6110 nfs4_put_lock_state(data->lsp);
6111 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006112 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006114 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006115}
6116
6117static const struct rpc_call_ops nfs4_lock_ops = {
6118 .rpc_call_prepare = nfs4_lock_prepare,
6119 .rpc_call_done = nfs4_lock_done,
6120 .rpc_release = nfs4_lock_release,
6121};
6122
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006123static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6124{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006125 switch (error) {
6126 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006127 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006128 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006129 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006130 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006131 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006132 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006133 break;
6134 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006135 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006136 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006137 };
6138}
6139
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006140static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141{
6142 struct nfs4_lockdata *data;
6143 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006144 struct rpc_message msg = {
6145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6146 .rpc_cred = state->owner->so_cred,
6147 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006148 struct rpc_task_setup task_setup_data = {
6149 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006150 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006151 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006152 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006153 .flags = RPC_TASK_ASYNC,
6154 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006155 int ret;
6156
Harvey Harrison3110ff82008-05-02 13:42:44 -07006157 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006158 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006159 fl->fl_u.nfs4_fl.owner,
6160 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161 if (data == NULL)
6162 return -ENOMEM;
6163 if (IS_SETLKW(cmd))
6164 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006165 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006166 msg.rpc_argp = &data->arg;
6167 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006168 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006169 if (recovery_type > NFS_LOCK_NEW) {
6170 if (recovery_type == NFS_LOCK_RECLAIM)
6171 data->arg.reclaim = NFS_LOCK_RECLAIM;
6172 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006173 } else
6174 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006175 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006176 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006177 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006178 ret = nfs4_wait_for_completion_rpc_task(task);
6179 if (ret == 0) {
6180 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006181 if (ret)
6182 nfs4_handle_setlk_error(data->server, data->lsp,
6183 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184 } else
6185 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006186 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006187 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006188 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006189 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190}
6191
6192static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6193{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006194 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006195 struct nfs4_exception exception = {
6196 .inode = state->inode,
6197 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006198 int err;
6199
6200 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006201 /* Cache the lock if possible... */
6202 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6203 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006204 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006205 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006206 break;
6207 nfs4_handle_exception(server, err, &exception);
6208 } while (exception.retry);
6209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
6212static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6213{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006214 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006215 struct nfs4_exception exception = {
6216 .inode = state->inode,
6217 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006218 int err;
6219
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006220 err = nfs4_set_lock_state(state, request);
6221 if (err != 0)
6222 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006223 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006224 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6225 return 0;
6226 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006227 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006228 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6229 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006230 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006231 switch (err) {
6232 default:
6233 goto out;
6234 case -NFS4ERR_GRACE:
6235 case -NFS4ERR_DELAY:
6236 nfs4_handle_exception(server, err, &exception);
6237 err = 0;
6238 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006239 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006240out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242}
6243
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006244#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006245static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6246{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006247 struct nfs4_lock_state *lsp;
6248 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006249
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006250 status = nfs4_set_lock_state(state, request);
6251 if (status != 0)
6252 return status;
6253 lsp = request->fl_u.nfs4_fl.owner;
6254 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6255 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6256 return 0;
6257 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006258 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006259}
6260#endif
6261
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6263{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006264 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006265 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006266 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006267 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
Trond Myklebust01c3b862006-06-29 16:38:39 -04006269 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006270 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006271 if (status < 0)
6272 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006273 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006274 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006275 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006276 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006277 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006278 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006279 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006280 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006281 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006282 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006283 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006284 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006285 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006286 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006287out:
6288 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 return status;
6290}
6291
6292static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6293{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006294 struct nfs4_exception exception = {
6295 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006296 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 int err;
6299
6300 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006301 err = _nfs4_proc_setlk(state, cmd, request);
6302 if (err == -NFS4ERR_DENIED)
6303 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006305 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 } while (exception.retry);
6307 return err;
6308}
6309
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006310#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6311#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6312
6313static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006314nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6315 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006316{
6317 int status = -ERESTARTSYS;
6318 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6319
6320 while(!signalled()) {
6321 status = nfs4_proc_setlk(state, cmd, request);
6322 if ((status != -EAGAIN) || IS_SETLK(cmd))
6323 break;
6324 freezable_schedule_timeout_interruptible(timeout);
6325 timeout *= 2;
6326 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6327 status = -ERESTARTSYS;
6328 }
6329 return status;
6330}
6331
Jeff Laytona1d617d82016-09-17 18:17:39 -04006332#ifdef CONFIG_NFS_V4_1
6333struct nfs4_lock_waiter {
6334 struct task_struct *task;
6335 struct inode *inode;
6336 struct nfs_lowner *owner;
6337 bool notified;
6338};
6339
6340static int
6341nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6342{
6343 int ret;
6344 struct cb_notify_lock_args *cbnl = key;
6345 struct nfs4_lock_waiter *waiter = wait->private;
6346 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6347 *wowner = waiter->owner;
6348
6349 /* Only wake if the callback was for the same owner */
6350 if (lowner->clientid != wowner->clientid ||
6351 lowner->id != wowner->id ||
6352 lowner->s_dev != wowner->s_dev)
6353 return 0;
6354
6355 /* Make sure it's for the right inode */
6356 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6357 return 0;
6358
6359 waiter->notified = true;
6360
6361 /* override "private" so we can use default_wake_function */
6362 wait->private = waiter->task;
6363 ret = autoremove_wake_function(wait, mode, flags, key);
6364 wait->private = waiter;
6365 return ret;
6366}
6367
6368static int
6369nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6370{
6371 int status = -ERESTARTSYS;
6372 unsigned long flags;
6373 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6374 struct nfs_server *server = NFS_SERVER(state->inode);
6375 struct nfs_client *clp = server->nfs_client;
6376 wait_queue_head_t *q = &clp->cl_lock_waitq;
6377 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6378 .id = lsp->ls_seqid.owner_id,
6379 .s_dev = server->s_dev };
6380 struct nfs4_lock_waiter waiter = { .task = current,
6381 .inode = state->inode,
6382 .owner = &owner,
6383 .notified = false };
6384 wait_queue_t wait;
6385
6386 /* Don't bother with waitqueue if we don't expect a callback */
6387 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6388 return nfs4_retry_setlk_simple(state, cmd, request);
6389
6390 init_wait(&wait);
6391 wait.private = &waiter;
6392 wait.func = nfs4_wake_lock_waiter;
6393 add_wait_queue(q, &wait);
6394
6395 while(!signalled()) {
6396 status = nfs4_proc_setlk(state, cmd, request);
6397 if ((status != -EAGAIN) || IS_SETLK(cmd))
6398 break;
6399
6400 status = -ERESTARTSYS;
6401 spin_lock_irqsave(&q->lock, flags);
6402 if (waiter.notified) {
6403 spin_unlock_irqrestore(&q->lock, flags);
6404 continue;
6405 }
6406 set_current_state(TASK_INTERRUPTIBLE);
6407 spin_unlock_irqrestore(&q->lock, flags);
6408
6409 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6410 }
6411
6412 finish_wait(q, &wait);
6413 return status;
6414}
6415#else /* !CONFIG_NFS_V4_1 */
6416static inline int
6417nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6418{
6419 return nfs4_retry_setlk_simple(state, cmd, request);
6420}
6421#endif
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423static int
6424nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6425{
6426 struct nfs_open_context *ctx;
6427 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 int status;
6429
6430 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006431 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 state = ctx->state;
6433
6434 if (request->fl_start < 0 || request->fl_end < 0)
6435 return -EINVAL;
6436
Trond Myklebustd9531262009-07-21 19:22:38 -04006437 if (IS_GETLK(cmd)) {
6438 if (state != NULL)
6439 return nfs4_proc_getlk(state, F_GETLK, request);
6440 return 0;
6441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
6443 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6444 return -EINVAL;
6445
Trond Myklebustd9531262009-07-21 19:22:38 -04006446 if (request->fl_type == F_UNLCK) {
6447 if (state != NULL)
6448 return nfs4_proc_unlck(state, cmd, request);
6449 return 0;
6450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Trond Myklebustd9531262009-07-21 19:22:38 -04006452 if (state == NULL)
6453 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006454
6455 if ((request->fl_flags & FL_POSIX) &&
6456 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6457 return -ENOLCK;
6458
Trond Myklebust55725512012-04-18 12:48:35 -04006459 /*
6460 * Don't rely on the VFS having checked the file open mode,
6461 * since it won't do this for flock() locks.
6462 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006463 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006464 case F_RDLCK:
6465 if (!(filp->f_mode & FMODE_READ))
6466 return -EBADF;
6467 break;
6468 case F_WRLCK:
6469 if (!(filp->f_mode & FMODE_WRITE))
6470 return -EBADF;
6471 }
6472
Jeff Layton1ea67db2016-09-17 18:17:37 -04006473 status = nfs4_set_lock_state(state, request);
6474 if (status != 0)
6475 return status;
6476
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006477 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478}
6479
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006480int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006481{
6482 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006483 int err;
6484
6485 err = nfs4_set_lock_state(state, fl);
6486 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006487 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006488 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006489 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006490}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006491
Trond Myklebustcf470c32012-03-07 13:49:12 -05006492struct nfs_release_lockowner_data {
6493 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006494 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006495 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006496 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006497 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006498};
6499
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006500static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6501{
6502 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006503 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006504 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6505 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006506 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006507 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006508}
6509
6510static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6511{
6512 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006513 struct nfs_server *server = data->server;
6514
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006515 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006516
6517 switch (task->tk_status) {
6518 case 0:
6519 renew_lease(server, data->timestamp);
6520 break;
6521 case -NFS4ERR_STALE_CLIENTID:
6522 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006523 nfs4_schedule_lease_recovery(server->nfs_client);
6524 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006525 case -NFS4ERR_LEASE_MOVED:
6526 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006527 if (nfs4_async_handle_error(task, server,
6528 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006529 rpc_restart_call_prepare(task);
6530 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006531}
6532
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006533static void nfs4_release_lockowner_release(void *calldata)
6534{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006535 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006536 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006537 kfree(calldata);
6538}
6539
Trond Myklebust17280172012-03-11 13:11:00 -04006540static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006541 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6542 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006543 .rpc_release = nfs4_release_lockowner_release,
6544};
6545
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006546static void
6547nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006548{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006549 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006550 struct rpc_message msg = {
6551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6552 };
6553
6554 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006555 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006556
Trond Myklebustcf470c32012-03-07 13:49:12 -05006557 data = kmalloc(sizeof(*data), GFP_NOFS);
6558 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006559 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006560 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006561 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006562 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6563 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6564 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006565
Trond Myklebustcf470c32012-03-07 13:49:12 -05006566 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006567 msg.rpc_resp = &data->res;
6568 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006569 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006570}
6571
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006572#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6573
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006574static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006575 struct dentry *unused, struct inode *inode,
6576 const char *key, const void *buf,
6577 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006578{
Al Viro59301222016-05-27 10:19:30 -04006579 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006580}
6581
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006582static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006583 struct dentry *unused, struct inode *inode,
6584 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006585{
Al Virob2968212016-04-10 20:48:24 -04006586 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006587}
6588
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006589static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006590{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006591 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006592}
6593
David Quigleyc9bccef2013-05-22 12:50:45 -04006594#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006595
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006596static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006597 struct dentry *unused, struct inode *inode,
6598 const char *key, const void *buf,
6599 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006600{
6601 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006602 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006603
6604 return -EOPNOTSUPP;
6605}
6606
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006607static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006608 struct dentry *unused, struct inode *inode,
6609 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006610{
6611 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006612 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006613 return -EOPNOTSUPP;
6614}
6615
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006616static ssize_t
6617nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006618{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006619 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006620
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006621 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6622 len = security_inode_listsecurity(inode, list, list_len);
6623 if (list_len && len > list_len)
6624 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006625 }
6626 return len;
6627}
6628
6629static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6630 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006631 .get = nfs4_xattr_get_nfs4_label,
6632 .set = nfs4_xattr_set_nfs4_label,
6633};
David Quigleyc9bccef2013-05-22 12:50:45 -04006634
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006635#else
6636
6637static ssize_t
6638nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6639{
6640 return 0;
6641}
6642
6643#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006644
Andy Adamson533eb462011-06-13 18:25:56 -04006645/*
6646 * nfs_fhget will use either the mounted_on_fileid or the fileid
6647 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006648static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6649{
Andy Adamson533eb462011-06-13 18:25:56 -04006650 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6651 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6652 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006653 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006654 return;
6655
6656 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006657 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006658 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6659 fattr->nlink = 2;
6660}
6661
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006662static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6663 const struct qstr *name,
6664 struct nfs4_fs_locations *fs_locations,
6665 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006666{
6667 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006668 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006669 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006670 };
6671 struct nfs4_fs_locations_arg args = {
6672 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006673 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006674 .page = page,
6675 .bitmask = bitmask,
6676 };
Benny Halevy22958462009-04-01 09:22:02 -04006677 struct nfs4_fs_locations_res res = {
6678 .fs_locations = fs_locations,
6679 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006680 struct rpc_message msg = {
6681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6682 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006683 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006684 };
6685 int status;
6686
Harvey Harrison3110ff82008-05-02 13:42:44 -07006687 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006688
6689 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6690 * is not supported */
6691 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6692 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6693 else
6694 bitmask[0] |= FATTR4_WORD0_FILEID;
6695
Trond Myklebustc228fd32007-01-13 02:28:11 -05006696 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006697 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006698 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006699 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006700 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006701 return status;
6702}
6703
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006704int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6705 const struct qstr *name,
6706 struct nfs4_fs_locations *fs_locations,
6707 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006708{
6709 struct nfs4_exception exception = { };
6710 int err;
6711 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006712 err = _nfs4_proc_fs_locations(client, dir, name,
6713 fs_locations, page);
6714 trace_nfs4_get_fs_locations(dir, name, err);
6715 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006716 &exception);
6717 } while (exception.retry);
6718 return err;
6719}
6720
Chuck Leverb03d7352013-10-17 14:12:50 -04006721/*
6722 * This operation also signals the server that this client is
6723 * performing migration recovery. The server can stop returning
6724 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6725 * appended to this compound to identify the client ID which is
6726 * performing recovery.
6727 */
6728static int _nfs40_proc_get_locations(struct inode *inode,
6729 struct nfs4_fs_locations *locations,
6730 struct page *page, struct rpc_cred *cred)
6731{
6732 struct nfs_server *server = NFS_SERVER(inode);
6733 struct rpc_clnt *clnt = server->client;
6734 u32 bitmask[2] = {
6735 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6736 };
6737 struct nfs4_fs_locations_arg args = {
6738 .clientid = server->nfs_client->cl_clientid,
6739 .fh = NFS_FH(inode),
6740 .page = page,
6741 .bitmask = bitmask,
6742 .migration = 1, /* skip LOOKUP */
6743 .renew = 1, /* append RENEW */
6744 };
6745 struct nfs4_fs_locations_res res = {
6746 .fs_locations = locations,
6747 .migration = 1,
6748 .renew = 1,
6749 };
6750 struct rpc_message msg = {
6751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6752 .rpc_argp = &args,
6753 .rpc_resp = &res,
6754 .rpc_cred = cred,
6755 };
6756 unsigned long now = jiffies;
6757 int status;
6758
6759 nfs_fattr_init(&locations->fattr);
6760 locations->server = server;
6761 locations->nlocations = 0;
6762
6763 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6764 nfs4_set_sequence_privileged(&args.seq_args);
6765 status = nfs4_call_sync_sequence(clnt, server, &msg,
6766 &args.seq_args, &res.seq_res);
6767 if (status)
6768 return status;
6769
6770 renew_lease(server, now);
6771 return 0;
6772}
6773
6774#ifdef CONFIG_NFS_V4_1
6775
6776/*
6777 * This operation also signals the server that this client is
6778 * performing migration recovery. The server can stop asserting
6779 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6780 * performing this operation is identified in the SEQUENCE
6781 * operation in this compound.
6782 *
6783 * When the client supports GETATTR(fs_locations_info), it can
6784 * be plumbed in here.
6785 */
6786static int _nfs41_proc_get_locations(struct inode *inode,
6787 struct nfs4_fs_locations *locations,
6788 struct page *page, struct rpc_cred *cred)
6789{
6790 struct nfs_server *server = NFS_SERVER(inode);
6791 struct rpc_clnt *clnt = server->client;
6792 u32 bitmask[2] = {
6793 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6794 };
6795 struct nfs4_fs_locations_arg args = {
6796 .fh = NFS_FH(inode),
6797 .page = page,
6798 .bitmask = bitmask,
6799 .migration = 1, /* skip LOOKUP */
6800 };
6801 struct nfs4_fs_locations_res res = {
6802 .fs_locations = locations,
6803 .migration = 1,
6804 };
6805 struct rpc_message msg = {
6806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6807 .rpc_argp = &args,
6808 .rpc_resp = &res,
6809 .rpc_cred = cred,
6810 };
6811 int status;
6812
6813 nfs_fattr_init(&locations->fattr);
6814 locations->server = server;
6815 locations->nlocations = 0;
6816
6817 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6818 nfs4_set_sequence_privileged(&args.seq_args);
6819 status = nfs4_call_sync_sequence(clnt, server, &msg,
6820 &args.seq_args, &res.seq_res);
6821 if (status == NFS4_OK &&
6822 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6823 status = -NFS4ERR_LEASE_MOVED;
6824 return status;
6825}
6826
6827#endif /* CONFIG_NFS_V4_1 */
6828
6829/**
6830 * nfs4_proc_get_locations - discover locations for a migrated FSID
6831 * @inode: inode on FSID that is migrating
6832 * @locations: result of query
6833 * @page: buffer
6834 * @cred: credential to use for this operation
6835 *
6836 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6837 * operation failed, or a negative errno if a local error occurred.
6838 *
6839 * On success, "locations" is filled in, but if the server has
6840 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6841 * asserted.
6842 *
6843 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6844 * from this client that require migration recovery.
6845 */
6846int nfs4_proc_get_locations(struct inode *inode,
6847 struct nfs4_fs_locations *locations,
6848 struct page *page, struct rpc_cred *cred)
6849{
6850 struct nfs_server *server = NFS_SERVER(inode);
6851 struct nfs_client *clp = server->nfs_client;
6852 const struct nfs4_mig_recovery_ops *ops =
6853 clp->cl_mvops->mig_recovery_ops;
6854 struct nfs4_exception exception = { };
6855 int status;
6856
6857 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6858 (unsigned long long)server->fsid.major,
6859 (unsigned long long)server->fsid.minor,
6860 clp->cl_hostname);
6861 nfs_display_fhandle(NFS_FH(inode), __func__);
6862
6863 do {
6864 status = ops->get_locations(inode, locations, page, cred);
6865 if (status != -NFS4ERR_DELAY)
6866 break;
6867 nfs4_handle_exception(server, status, &exception);
6868 } while (exception.retry);
6869 return status;
6870}
6871
Chuck Lever44c99932013-10-17 14:13:30 -04006872/*
6873 * This operation also signals the server that this client is
6874 * performing "lease moved" recovery. The server can stop
6875 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6876 * is appended to this compound to identify the client ID which is
6877 * performing recovery.
6878 */
6879static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6880{
6881 struct nfs_server *server = NFS_SERVER(inode);
6882 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6883 struct rpc_clnt *clnt = server->client;
6884 struct nfs4_fsid_present_arg args = {
6885 .fh = NFS_FH(inode),
6886 .clientid = clp->cl_clientid,
6887 .renew = 1, /* append RENEW */
6888 };
6889 struct nfs4_fsid_present_res res = {
6890 .renew = 1,
6891 };
6892 struct rpc_message msg = {
6893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6894 .rpc_argp = &args,
6895 .rpc_resp = &res,
6896 .rpc_cred = cred,
6897 };
6898 unsigned long now = jiffies;
6899 int status;
6900
6901 res.fh = nfs_alloc_fhandle();
6902 if (res.fh == NULL)
6903 return -ENOMEM;
6904
6905 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6906 nfs4_set_sequence_privileged(&args.seq_args);
6907 status = nfs4_call_sync_sequence(clnt, server, &msg,
6908 &args.seq_args, &res.seq_res);
6909 nfs_free_fhandle(res.fh);
6910 if (status)
6911 return status;
6912
6913 do_renew_lease(clp, now);
6914 return 0;
6915}
6916
6917#ifdef CONFIG_NFS_V4_1
6918
6919/*
6920 * This operation also signals the server that this client is
6921 * performing "lease moved" recovery. The server can stop asserting
6922 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6923 * this operation is identified in the SEQUENCE operation in this
6924 * compound.
6925 */
6926static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6927{
6928 struct nfs_server *server = NFS_SERVER(inode);
6929 struct rpc_clnt *clnt = server->client;
6930 struct nfs4_fsid_present_arg args = {
6931 .fh = NFS_FH(inode),
6932 };
6933 struct nfs4_fsid_present_res res = {
6934 };
6935 struct rpc_message msg = {
6936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6937 .rpc_argp = &args,
6938 .rpc_resp = &res,
6939 .rpc_cred = cred,
6940 };
6941 int status;
6942
6943 res.fh = nfs_alloc_fhandle();
6944 if (res.fh == NULL)
6945 return -ENOMEM;
6946
6947 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6948 nfs4_set_sequence_privileged(&args.seq_args);
6949 status = nfs4_call_sync_sequence(clnt, server, &msg,
6950 &args.seq_args, &res.seq_res);
6951 nfs_free_fhandle(res.fh);
6952 if (status == NFS4_OK &&
6953 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6954 status = -NFS4ERR_LEASE_MOVED;
6955 return status;
6956}
6957
6958#endif /* CONFIG_NFS_V4_1 */
6959
6960/**
6961 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6962 * @inode: inode on FSID to check
6963 * @cred: credential to use for this operation
6964 *
6965 * Server indicates whether the FSID is present, moved, or not
6966 * recognized. This operation is necessary to clear a LEASE_MOVED
6967 * condition for this client ID.
6968 *
6969 * Returns NFS4_OK if the FSID is present on this server,
6970 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6971 * NFS4ERR code if some error occurred on the server, or a
6972 * negative errno if a local failure occurred.
6973 */
6974int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6975{
6976 struct nfs_server *server = NFS_SERVER(inode);
6977 struct nfs_client *clp = server->nfs_client;
6978 const struct nfs4_mig_recovery_ops *ops =
6979 clp->cl_mvops->mig_recovery_ops;
6980 struct nfs4_exception exception = { };
6981 int status;
6982
6983 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6984 (unsigned long long)server->fsid.major,
6985 (unsigned long long)server->fsid.minor,
6986 clp->cl_hostname);
6987 nfs_display_fhandle(NFS_FH(inode), __func__);
6988
6989 do {
6990 status = ops->fsid_present(inode, cred);
6991 if (status != -NFS4ERR_DELAY)
6992 break;
6993 nfs4_handle_exception(server, status, &exception);
6994 } while (exception.retry);
6995 return status;
6996}
6997
Andy Adamson5ec16a82013-08-08 10:57:55 -04006998/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006999 * If 'use_integrity' is true and the state managment nfs_client
7000 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7001 * and the machine credential as per RFC3530bis and RFC5661 Security
7002 * Considerations sections. Otherwise, just use the user cred with the
7003 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007004 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007005static 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 +00007006{
7007 int status;
7008 struct nfs4_secinfo_arg args = {
7009 .dir_fh = NFS_FH(dir),
7010 .name = name,
7011 };
7012 struct nfs4_secinfo_res res = {
7013 .flavors = flavors,
7014 };
7015 struct rpc_message msg = {
7016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7017 .rpc_argp = &args,
7018 .rpc_resp = &res,
7019 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007020 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007021 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007022
7023 if (use_integrity) {
7024 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007025 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7026 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007027 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007028
7029 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007030
7031 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7032 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7033
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007034 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7035 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007036 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007037
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007038 if (cred)
7039 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007040
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007041 return status;
7042}
7043
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007044int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7045 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007046{
7047 struct nfs4_exception exception = { };
7048 int err;
7049 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007050 err = -NFS4ERR_WRONGSEC;
7051
7052 /* try to use integrity protection with machine cred */
7053 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7054 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7055
7056 /*
7057 * if unable to use integrity protection, or SECINFO with
7058 * integrity protection returns NFS4ERR_WRONGSEC (which is
7059 * disallowed by spec, but exists in deployed servers) use
7060 * the current filesystem's rpc_client and the user cred.
7061 */
7062 if (err == -NFS4ERR_WRONGSEC)
7063 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7064
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007065 trace_nfs4_secinfo(dir, name, err);
7066 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007067 &exception);
7068 } while (exception.retry);
7069 return err;
7070}
7071
Andy Adamson557134a2009-04-01 09:21:53 -04007072#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007073/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007074 * Check the exchange flags returned by the server for invalid flags, having
7075 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7076 * DS flags set.
7077 */
7078static int nfs4_check_cl_exchange_flags(u32 flags)
7079{
7080 if (flags & ~EXCHGID4_FLAG_MASK_R)
7081 goto out_inval;
7082 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7083 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7084 goto out_inval;
7085 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7086 goto out_inval;
7087 return NFS_OK;
7088out_inval:
7089 return -NFS4ERR_INVAL;
7090}
7091
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007092static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007093nfs41_same_server_scope(struct nfs41_server_scope *a,
7094 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007095{
7096 if (a->server_scope_sz == b->server_scope_sz &&
7097 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7098 return true;
7099
7100 return false;
7101}
7102
Andy Adamson02a95de2016-02-05 16:08:37 -05007103static void
7104nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7105{
7106}
7107
7108static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7109 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7110};
7111
Andy Adamson357f54d2010-12-14 10:11:57 -05007112/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007113 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007114 *
7115 * The 4.1 client currently uses the same TCP connection for the
7116 * fore and backchannel.
7117 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007118static
7119int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7120 struct rpc_xprt *xprt,
7121 struct nfs_client *clp,
7122 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007123{
7124 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007125 struct nfs41_bind_conn_to_session_args args = {
7126 .client = clp,
7127 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7128 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007129 struct nfs41_bind_conn_to_session_res res;
7130 struct rpc_message msg = {
7131 .rpc_proc =
7132 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007133 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007134 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007135 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007136 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007137 struct rpc_task_setup task_setup_data = {
7138 .rpc_client = clnt,
7139 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007140 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007141 .rpc_message = &msg,
7142 .flags = RPC_TASK_TIMEOUT,
7143 };
7144 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007145
7146 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007147
Trond Myklebust71a097c2015-02-18 09:27:18 -08007148 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7149 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7150 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007151
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007152 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7153 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7154 args.dir = NFS4_CDFC4_FORE;
7155
7156 task = rpc_run_task(&task_setup_data);
7157 if (!IS_ERR(task)) {
7158 status = task->tk_status;
7159 rpc_put_task(task);
7160 } else
7161 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007162 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007163 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007164 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007165 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7166 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7167 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007168 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007169 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007170 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007171 dprintk("NFS: %s: Unexpected direction from server\n",
7172 __func__);
7173 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007174 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007175 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007176 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7178 __func__);
7179 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007180 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007181 }
7182 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007183out:
7184 dprintk("<-- %s status= %d\n", __func__, status);
7185 return status;
7186}
7187
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007188struct rpc_bind_conn_calldata {
7189 struct nfs_client *clp;
7190 struct rpc_cred *cred;
7191};
7192
7193static int
7194nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7195 struct rpc_xprt *xprt,
7196 void *calldata)
7197{
7198 struct rpc_bind_conn_calldata *p = calldata;
7199
7200 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7201}
7202
7203int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7204{
7205 struct rpc_bind_conn_calldata data = {
7206 .clp = clp,
7207 .cred = cred,
7208 };
7209 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7210 nfs4_proc_bind_conn_to_session_callback, &data);
7211}
7212
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007213/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007214 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7215 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007216 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007217static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7218 .how = SP4_MACH_CRED,
7219 .enforce.u.words = {
7220 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7221 1 << (OP_EXCHANGE_ID - 32) |
7222 1 << (OP_CREATE_SESSION - 32) |
7223 1 << (OP_DESTROY_SESSION - 32) |
7224 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007225 },
7226 .allow.u.words = {
7227 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007228 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007229 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007230 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007231 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007232 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007233 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007234 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007235 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007236 1 << (OP_FREE_STATEID - 32) |
7237 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007238 }
7239};
7240
7241/*
7242 * Select the state protection mode for client `clp' given the server results
7243 * from exchange_id in `sp'.
7244 *
7245 * Returns 0 on success, negative errno otherwise.
7246 */
7247static int nfs4_sp4_select_mode(struct nfs_client *clp,
7248 struct nfs41_state_protection *sp)
7249{
7250 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7251 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7252 1 << (OP_EXCHANGE_ID - 32) |
7253 1 << (OP_CREATE_SESSION - 32) |
7254 1 << (OP_DESTROY_SESSION - 32) |
7255 1 << (OP_DESTROY_CLIENTID - 32)
7256 };
7257 unsigned int i;
7258
7259 if (sp->how == SP4_MACH_CRED) {
7260 /* Print state protect result */
7261 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7262 for (i = 0; i <= LAST_NFS4_OP; i++) {
7263 if (test_bit(i, sp->enforce.u.longs))
7264 dfprintk(MOUNT, " enforce op %d\n", i);
7265 if (test_bit(i, sp->allow.u.longs))
7266 dfprintk(MOUNT, " allow op %d\n", i);
7267 }
7268
7269 /* make sure nothing is on enforce list that isn't supported */
7270 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7271 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7272 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7273 return -EINVAL;
7274 }
7275 }
7276
7277 /*
7278 * Minimal mode - state operations are allowed to use machine
7279 * credential. Note this already happens by default, so the
7280 * client doesn't have to do anything more than the negotiation.
7281 *
7282 * NOTE: we don't care if EXCHANGE_ID is in the list -
7283 * we're already using the machine cred for exchange_id
7284 * and will never use a different cred.
7285 */
7286 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7287 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7288 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7289 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7290 dfprintk(MOUNT, "sp4_mach_cred:\n");
7291 dfprintk(MOUNT, " minimal mode enabled\n");
7292 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7293 } else {
7294 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7295 return -EINVAL;
7296 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007297
7298 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007299 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7300 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007301 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7302 dfprintk(MOUNT, " cleanup mode enabled\n");
7303 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7304 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007305
Andrew Elble99ade3c2015-12-02 09:39:51 -05007306 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7307 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7308 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7309 &clp->cl_sp4_flags);
7310 }
7311
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007312 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7313 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7314 dfprintk(MOUNT, " secinfo mode enabled\n");
7315 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7316 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007317
7318 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7319 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7320 dfprintk(MOUNT, " stateid mode enabled\n");
7321 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7322 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007323
7324 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7325 dfprintk(MOUNT, " write mode enabled\n");
7326 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7327 }
7328
7329 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7330 dfprintk(MOUNT, " commit mode enabled\n");
7331 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7332 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007333 }
7334
7335 return 0;
7336}
7337
Andy Adamson8d89bd72016-09-09 09:22:18 -04007338struct nfs41_exchange_id_data {
7339 struct nfs41_exchange_id_res res;
7340 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007341 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007342 int rpc_status;
7343};
7344
7345static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7346{
7347 struct nfs41_exchange_id_data *cdata =
7348 (struct nfs41_exchange_id_data *)data;
7349 struct nfs_client *clp = cdata->args.client;
7350 int status = task->tk_status;
7351
7352 trace_nfs4_exchange_id(clp, status);
7353
7354 if (status == 0)
7355 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007356
7357 if (cdata->xprt && status == 0) {
7358 status = nfs4_detect_session_trunking(clp, &cdata->res,
7359 cdata->xprt);
7360 goto out;
7361 }
7362
Andy Adamson8d89bd72016-09-09 09:22:18 -04007363 if (status == 0)
7364 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7365
7366 if (status == 0) {
7367 clp->cl_clientid = cdata->res.clientid;
7368 clp->cl_exchange_flags = cdata->res.flags;
7369 /* Client ID is not confirmed */
7370 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7371 clear_bit(NFS4_SESSION_ESTABLISHED,
7372 &clp->cl_session->session_state);
7373 clp->cl_seqid = cdata->res.seqid;
7374 }
7375
7376 kfree(clp->cl_serverowner);
7377 clp->cl_serverowner = cdata->res.server_owner;
7378 cdata->res.server_owner = NULL;
7379
7380 /* use the most recent implementation id */
7381 kfree(clp->cl_implid);
7382 clp->cl_implid = cdata->res.impl_id;
7383 cdata->res.impl_id = NULL;
7384
7385 if (clp->cl_serverscope != NULL &&
7386 !nfs41_same_server_scope(clp->cl_serverscope,
7387 cdata->res.server_scope)) {
7388 dprintk("%s: server_scope mismatch detected\n",
7389 __func__);
7390 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7391 kfree(clp->cl_serverscope);
7392 clp->cl_serverscope = NULL;
7393 }
7394
7395 if (clp->cl_serverscope == NULL) {
7396 clp->cl_serverscope = cdata->res.server_scope;
7397 cdata->res.server_scope = NULL;
7398 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007399 /* Save the EXCHANGE_ID verifier session trunk tests */
7400 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7401 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007403out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007404 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007405 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007406}
7407
7408static void nfs4_exchange_id_release(void *data)
7409{
7410 struct nfs41_exchange_id_data *cdata =
7411 (struct nfs41_exchange_id_data *)data;
7412
7413 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007414 if (cdata->xprt) {
7415 xprt_put(cdata->xprt);
7416 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7417 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007418 kfree(cdata->res.impl_id);
7419 kfree(cdata->res.server_scope);
7420 kfree(cdata->res.server_owner);
7421 kfree(cdata);
7422}
7423
7424static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7425 .rpc_call_done = nfs4_exchange_id_done,
7426 .rpc_release = nfs4_exchange_id_release,
7427};
7428
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007429/*
7430 * _nfs4_proc_exchange_id()
7431 *
7432 * Wrapper for EXCHANGE_ID operation.
7433 */
7434static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007435 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007436{
7437 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007438 struct rpc_message msg = {
7439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007440 .rpc_cred = cred,
7441 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442 struct rpc_task_setup task_setup_data = {
7443 .rpc_client = clp->cl_rpcclient,
7444 .callback_ops = &nfs4_exchange_id_call_ops,
7445 .rpc_message = &msg,
7446 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7447 };
7448 struct nfs41_exchange_id_data *calldata;
7449 struct rpc_task *task;
7450 int status = -EIO;
7451
7452 if (!atomic_inc_not_zero(&clp->cl_count))
7453 goto out;
7454
7455 status = -ENOMEM;
7456 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7457 if (!calldata)
7458 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007459
Andy Adamsonad0849a2016-09-09 09:22:28 -04007460 if (!xprt)
7461 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007462
7463 status = nfs4_init_uniform_client_string(clp);
7464 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007465 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007466
7467 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007468 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007469 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007470
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7472 GFP_NOFS);
7473 status = -ENOMEM;
7474 if (unlikely(calldata->res.server_owner == NULL))
7475 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007476
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007478 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007480 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007481
Andy Adamson8d89bd72016-09-09 09:22:18 -04007482 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7483 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007484 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007485
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007486 switch (sp4_how) {
7487 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007489 break;
7490
7491 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007493 break;
7494
7495 default:
7496 /* unsupported! */
7497 WARN_ON_ONCE(1);
7498 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007499 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007500 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007501 if (xprt) {
7502 calldata->xprt = xprt;
7503 task_setup_data.rpc_xprt = xprt;
7504 task_setup_data.flags =
7505 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7506 calldata->args.verifier = &clp->cl_confirm;
7507 } else {
7508 calldata->args.verifier = &verifier;
7509 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 calldata->args.client = clp;
7511#ifdef CONFIG_NFS_V4_1_MIGRATION
7512 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7513 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7514 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7515#else
7516 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7517 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7518#endif
7519 msg.rpc_argp = &calldata->args;
7520 msg.rpc_resp = &calldata->res;
7521 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007522
Andy Adamson8d89bd72016-09-09 09:22:18 -04007523 task = rpc_run_task(&task_setup_data);
7524 if (IS_ERR(task)) {
7525 status = PTR_ERR(task);
7526 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007527 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007528
Andy Adamsonad0849a2016-09-09 09:22:28 -04007529 if (!xprt) {
7530 status = rpc_wait_for_completion_task(task);
7531 if (!status)
7532 status = calldata->rpc_status;
7533 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007535
Andy Adamson8d89bd72016-09-09 09:22:18 -04007536 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007537out:
Chuck Lever177313f2012-05-21 22:44:58 -04007538 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007539 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007540 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007541 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007542 clp->cl_implid->date.seconds,
7543 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007544 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007545 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007546
7547out_impl_id:
7548 kfree(calldata->res.impl_id);
7549out_server_scope:
7550 kfree(calldata->res.server_scope);
7551out_server_owner:
7552 kfree(calldata->res.server_owner);
7553out_calldata:
7554 kfree(calldata);
7555 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007556}
7557
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007558/*
7559 * nfs4_proc_exchange_id()
7560 *
7561 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7562 *
7563 * Since the clientid has expired, all compounds using sessions
7564 * associated with the stale clientid will be returning
7565 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7566 * be in some phase of session reset.
7567 *
7568 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7569 */
7570int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7571{
7572 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7573 int status;
7574
7575 /* try SP4_MACH_CRED if krb5i/p */
7576 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7577 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007578 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007579 if (!status)
7580 return 0;
7581 }
7582
7583 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007584 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007585}
7586
Andy Adamson04fa2c62016-09-09 09:22:29 -04007587/**
7588 * nfs4_test_session_trunk
7589 *
7590 * This is an add_xprt_test() test function called from
7591 * rpc_clnt_setup_test_and_add_xprt.
7592 *
7593 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7594 * and is dereferrenced in nfs4_exchange_id_release
7595 *
7596 * Upon success, add the new transport to the rpc_clnt
7597 *
7598 * @clnt: struct rpc_clnt to get new transport
7599 * @xprt: the rpc_xprt to test
7600 * @data: call data for _nfs4_proc_exchange_id.
7601 */
7602int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7603 void *data)
7604{
7605 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7606 u32 sp4_how;
7607
7608 dprintk("--> %s try %s\n", __func__,
7609 xprt->address_strings[RPC_DISPLAY_ADDR]);
7610
7611 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7612
7613 /* Test connection for session trunking. Async exchange_id call */
7614 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7615}
7616EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7617
Trond Myklebust66245532012-05-25 17:18:09 -04007618static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7619 struct rpc_cred *cred)
7620{
7621 struct rpc_message msg = {
7622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7623 .rpc_argp = clp,
7624 .rpc_cred = cred,
7625 };
7626 int status;
7627
7628 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007629 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007630 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007631 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007632 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7633 return status;
7634}
7635
7636static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7637 struct rpc_cred *cred)
7638{
7639 unsigned int loop;
7640 int ret;
7641
7642 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7643 ret = _nfs4_proc_destroy_clientid(clp, cred);
7644 switch (ret) {
7645 case -NFS4ERR_DELAY:
7646 case -NFS4ERR_CLIENTID_BUSY:
7647 ssleep(1);
7648 break;
7649 default:
7650 return ret;
7651 }
7652 }
7653 return 0;
7654}
7655
7656int nfs4_destroy_clientid(struct nfs_client *clp)
7657{
7658 struct rpc_cred *cred;
7659 int ret = 0;
7660
7661 if (clp->cl_mvops->minor_version < 1)
7662 goto out;
7663 if (clp->cl_exchange_flags == 0)
7664 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007665 if (clp->cl_preserve_clid)
7666 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007667 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007668 ret = nfs4_proc_destroy_clientid(clp, cred);
7669 if (cred)
7670 put_rpccred(cred);
7671 switch (ret) {
7672 case 0:
7673 case -NFS4ERR_STALE_CLIENTID:
7674 clp->cl_exchange_flags = 0;
7675 }
7676out:
7677 return ret;
7678}
7679
Andy Adamson2050f0c2009-04-01 09:22:30 -04007680struct nfs4_get_lease_time_data {
7681 struct nfs4_get_lease_time_args *args;
7682 struct nfs4_get_lease_time_res *res;
7683 struct nfs_client *clp;
7684};
7685
7686static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7687 void *calldata)
7688{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007689 struct nfs4_get_lease_time_data *data =
7690 (struct nfs4_get_lease_time_data *)calldata;
7691
7692 dprintk("--> %s\n", __func__);
7693 /* just setup sequence, do not trigger session recovery
7694 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007695 nfs41_setup_sequence(data->clp->cl_session,
7696 &data->args->la_seq_args,
7697 &data->res->lr_seq_res,
7698 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007699 dprintk("<-- %s\n", __func__);
7700}
7701
7702/*
7703 * Called from nfs4_state_manager thread for session setup, so don't recover
7704 * from sequence operation or clientid errors.
7705 */
7706static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7707{
7708 struct nfs4_get_lease_time_data *data =
7709 (struct nfs4_get_lease_time_data *)calldata;
7710
7711 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007712 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7713 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007714 switch (task->tk_status) {
7715 case -NFS4ERR_DELAY:
7716 case -NFS4ERR_GRACE:
7717 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7718 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7719 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007720 /* fall through */
7721 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007722 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007723 return;
7724 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007725 dprintk("<-- %s\n", __func__);
7726}
7727
Trond Myklebust17280172012-03-11 13:11:00 -04007728static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007729 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7730 .rpc_call_done = nfs4_get_lease_time_done,
7731};
7732
7733int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7734{
7735 struct rpc_task *task;
7736 struct nfs4_get_lease_time_args args;
7737 struct nfs4_get_lease_time_res res = {
7738 .lr_fsinfo = fsinfo,
7739 };
7740 struct nfs4_get_lease_time_data data = {
7741 .args = &args,
7742 .res = &res,
7743 .clp = clp,
7744 };
7745 struct rpc_message msg = {
7746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7747 .rpc_argp = &args,
7748 .rpc_resp = &res,
7749 };
7750 struct rpc_task_setup task_setup = {
7751 .rpc_client = clp->cl_rpcclient,
7752 .rpc_message = &msg,
7753 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007754 .callback_data = &data,
7755 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007756 };
7757 int status;
7758
Chuck Levera9c92d62013-08-09 12:48:18 -04007759 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007760 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007761 dprintk("--> %s\n", __func__);
7762 task = rpc_run_task(&task_setup);
7763
7764 if (IS_ERR(task))
7765 status = PTR_ERR(task);
7766 else {
7767 status = task->tk_status;
7768 rpc_put_task(task);
7769 }
7770 dprintk("<-- %s return %d\n", __func__, status);
7771
7772 return status;
7773}
7774
Andy Adamsonfc931582009-04-01 09:22:31 -04007775/*
7776 * Initialize the values to be used by the client in CREATE_SESSION
7777 * If nfs4_init_session set the fore channel request and response sizes,
7778 * use them.
7779 *
7780 * Set the back channel max_resp_sz_cached to zero to force the client to
7781 * always set csa_cachethis to FALSE because the current implementation
7782 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7783 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007784static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7785 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007786{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007787 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007788 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007789
Andy Adamson18aad3d2013-06-26 12:21:49 -04007790 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7791 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7792
Andy Adamsonfc931582009-04-01 09:22:31 -04007793 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007794 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7795 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007796 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007797 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007798
7799 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007800 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007801 __func__,
7802 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007803 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007804
7805 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007806 args->bc_attrs.max_rqst_sz = max_bc_payload;
7807 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007808 args->bc_attrs.max_resp_sz_cached = 0;
7809 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007810 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007811
7812 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7813 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7814 __func__,
7815 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7816 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7817 args->bc_attrs.max_reqs);
7818}
7819
Trond Myklebust79969dd2015-02-18 11:30:18 -08007820static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7821 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007822{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007823 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007824 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007825
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007826 if (rcvd->max_resp_sz > sent->max_resp_sz)
7827 return -EINVAL;
7828 /*
7829 * Our requested max_ops is the minimum we need; we're not
7830 * prepared to break up compounds into smaller pieces than that.
7831 * So, no point even trying to continue if the server won't
7832 * cooperate:
7833 */
7834 if (rcvd->max_ops < sent->max_ops)
7835 return -EINVAL;
7836 if (rcvd->max_reqs == 0)
7837 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007838 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7839 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007840 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007841}
7842
Trond Myklebust79969dd2015-02-18 11:30:18 -08007843static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7844 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007845{
7846 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007847 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007848
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007849 if (!(res->flags & SESSION4_BACK_CHAN))
7850 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007851 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7852 return -EINVAL;
7853 if (rcvd->max_resp_sz < sent->max_resp_sz)
7854 return -EINVAL;
7855 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7856 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007857 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007858 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007859 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007860 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007861out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862 return 0;
7863}
Andy Adamson8d353012009-04-01 09:22:32 -04007864
Andy Adamson8d353012009-04-01 09:22:32 -04007865static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007866 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007867{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007868 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007869
Trond Myklebust79969dd2015-02-18 11:30:18 -08007870 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007871 if (ret)
7872 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007873 return nfs4_verify_back_channel_attrs(args, res);
7874}
7875
7876static void nfs4_update_session(struct nfs4_session *session,
7877 struct nfs41_create_session_res *res)
7878{
7879 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007880 /* Mark client id and session as being confirmed */
7881 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7882 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007883 session->flags = res->flags;
7884 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007885 if (res->flags & SESSION4_BACK_CHAN)
7886 memcpy(&session->bc_attrs, &res->bc_attrs,
7887 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007888}
7889
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007890static int _nfs4_proc_create_session(struct nfs_client *clp,
7891 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007892{
7893 struct nfs4_session *session = clp->cl_session;
7894 struct nfs41_create_session_args args = {
7895 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007896 .clientid = clp->cl_clientid,
7897 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007898 .cb_program = NFS4_CALLBACK,
7899 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007900 struct nfs41_create_session_res res;
7901
Andy Adamsonfc931582009-04-01 09:22:31 -04007902 struct rpc_message msg = {
7903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7904 .rpc_argp = &args,
7905 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007906 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007907 };
7908 int status;
7909
Chuck Lever6b26cc82016-05-02 14:40:40 -04007910 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007911 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007912
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007913 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007914 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007915
Trond Myklebustb519d402016-09-11 14:50:01 -04007916 switch (status) {
7917 case -NFS4ERR_STALE_CLIENTID:
7918 case -NFS4ERR_DELAY:
7919 case -ETIMEDOUT:
7920 case -EACCES:
7921 case -EAGAIN:
7922 goto out;
7923 };
7924
7925 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007926 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007927 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007928 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007930 if (status)
7931 goto out;
7932 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007933 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007934out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007935 return status;
7936}
7937
7938/*
7939 * Issues a CREATE_SESSION operation to the server.
7940 * It is the responsibility of the caller to verify the session is
7941 * expired before calling this routine.
7942 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007943int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007944{
7945 int status;
7946 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007947 struct nfs4_session *session = clp->cl_session;
7948
7949 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7950
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007951 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007952 if (status)
7953 goto out;
7954
Andy Adamsonaacd5532011-11-09 13:58:21 -05007955 /* Init or reset the session slot tables */
7956 status = nfs4_setup_session_slot_tables(session);
7957 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007958 if (status)
7959 goto out;
7960
7961 ptr = (unsigned *)&session->sess_id.data[0];
7962 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7963 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007964out:
7965 dprintk("<-- %s\n", __func__);
7966 return status;
7967}
7968
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007969/*
7970 * Issue the over-the-wire RPC DESTROY_SESSION.
7971 * The caller must serialize access to this routine.
7972 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007973int nfs4_proc_destroy_session(struct nfs4_session *session,
7974 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007975{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007976 struct rpc_message msg = {
7977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7978 .rpc_argp = session,
7979 .rpc_cred = cred,
7980 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007981 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007982
7983 dprintk("--> nfs4_proc_destroy_session\n");
7984
7985 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007986 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7987 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007988
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007989 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007990 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007991
7992 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007993 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007994 "Session has been destroyed regardless...\n", status);
7995
7996 dprintk("<-- nfs4_proc_destroy_session\n");
7997 return status;
7998}
7999
Trond Myklebust7b38c362012-05-23 13:23:31 -04008000/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008001 * Renew the cl_session lease.
8002 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008003struct nfs4_sequence_data {
8004 struct nfs_client *clp;
8005 struct nfs4_sequence_args args;
8006 struct nfs4_sequence_res res;
8007};
8008
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008009static void nfs41_sequence_release(void *data)
8010{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008011 struct nfs4_sequence_data *calldata = data;
8012 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008013
Alexandros Batsakis71358402010-02-05 03:45:05 -08008014 if (atomic_read(&clp->cl_count) > 1)
8015 nfs4_schedule_state_renewal(clp);
8016 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008017 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008018}
8019
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008020static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8021{
8022 switch(task->tk_status) {
8023 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008024 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8025 return -EAGAIN;
8026 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008027 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008028 }
8029 return 0;
8030}
8031
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008032static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008033{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008034 struct nfs4_sequence_data *calldata = data;
8035 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008036
Trond Myklebust14516c32010-07-31 14:29:06 -04008037 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8038 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008040 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008041 if (task->tk_status < 0) {
8042 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008043 if (atomic_read(&clp->cl_count) == 1)
8044 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008045
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008046 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8047 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008048 return;
8049 }
8050 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008051 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008052out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008053 dprintk("<-- %s\n", __func__);
8054}
8055
8056static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8057{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008058 struct nfs4_sequence_data *calldata = data;
8059 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008060 struct nfs4_sequence_args *args;
8061 struct nfs4_sequence_res *res;
8062
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063 args = task->tk_msg.rpc_argp;
8064 res = task->tk_msg.rpc_resp;
8065
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008066 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008067}
8068
8069static const struct rpc_call_ops nfs41_sequence_ops = {
8070 .rpc_call_done = nfs41_sequence_call_done,
8071 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008072 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008073};
8074
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008075static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8076 struct rpc_cred *cred,
8077 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008079 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080 struct rpc_message msg = {
8081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8082 .rpc_cred = cred,
8083 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008084 struct rpc_task_setup task_setup_data = {
8085 .rpc_client = clp->cl_rpcclient,
8086 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008087 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008088 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008089 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008090
Alexandros Batsakis71358402010-02-05 03:45:05 -08008091 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008093 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008094 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008095 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008096 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008097 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008098 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008099 if (is_privileged)
8100 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008101 msg.rpc_argp = &calldata->args;
8102 msg.rpc_resp = &calldata->res;
8103 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008104 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106 return rpc_run_task(&task_setup_data);
8107}
8108
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008109static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008110{
8111 struct rpc_task *task;
8112 int ret = 0;
8113
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008114 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008115 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008116 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008117 if (IS_ERR(task))
8118 ret = PTR_ERR(task);
8119 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008120 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008121 dprintk("<-- %s status=%d\n", __func__, ret);
8122 return ret;
8123}
8124
8125static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8126{
8127 struct rpc_task *task;
8128 int ret;
8129
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008130 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008131 if (IS_ERR(task)) {
8132 ret = PTR_ERR(task);
8133 goto out;
8134 }
8135 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008136 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008137 ret = task->tk_status;
8138 rpc_put_task(task);
8139out:
8140 dprintk("<-- %s status=%d\n", __func__, ret);
8141 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008142}
8143
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008144struct nfs4_reclaim_complete_data {
8145 struct nfs_client *clp;
8146 struct nfs41_reclaim_complete_args arg;
8147 struct nfs41_reclaim_complete_res res;
8148};
8149
8150static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8151{
8152 struct nfs4_reclaim_complete_data *calldata = data;
8153
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008154 nfs41_setup_sequence(calldata->clp->cl_session,
8155 &calldata->arg.seq_args,
8156 &calldata->res.seq_res,
8157 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008158}
8159
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008160static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8161{
8162 switch(task->tk_status) {
8163 case 0:
8164 case -NFS4ERR_COMPLETE_ALREADY:
8165 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8166 break;
8167 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008168 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008169 /* fall through */
8170 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008171 return -EAGAIN;
8172 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008173 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008174 }
8175 return 0;
8176}
8177
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008178static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8179{
8180 struct nfs4_reclaim_complete_data *calldata = data;
8181 struct nfs_client *clp = calldata->clp;
8182 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8183
8184 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008185 if (!nfs41_sequence_done(task, res))
8186 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008187
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008188 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008189 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8190 rpc_restart_call_prepare(task);
8191 return;
8192 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008193 dprintk("<-- %s\n", __func__);
8194}
8195
8196static void nfs4_free_reclaim_complete_data(void *data)
8197{
8198 struct nfs4_reclaim_complete_data *calldata = data;
8199
8200 kfree(calldata);
8201}
8202
8203static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8204 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8205 .rpc_call_done = nfs4_reclaim_complete_done,
8206 .rpc_release = nfs4_free_reclaim_complete_data,
8207};
8208
8209/*
8210 * Issue a global reclaim complete.
8211 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008212static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8213 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008214{
8215 struct nfs4_reclaim_complete_data *calldata;
8216 struct rpc_task *task;
8217 struct rpc_message msg = {
8218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008219 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008220 };
8221 struct rpc_task_setup task_setup_data = {
8222 .rpc_client = clp->cl_rpcclient,
8223 .rpc_message = &msg,
8224 .callback_ops = &nfs4_reclaim_complete_call_ops,
8225 .flags = RPC_TASK_ASYNC,
8226 };
8227 int status = -ENOMEM;
8228
8229 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008230 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008231 if (calldata == NULL)
8232 goto out;
8233 calldata->clp = clp;
8234 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008235
Chuck Levera9c92d62013-08-09 12:48:18 -04008236 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008237 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238 msg.rpc_argp = &calldata->arg;
8239 msg.rpc_resp = &calldata->res;
8240 task_setup_data.callback_data = calldata;
8241 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008242 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008243 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008244 goto out;
8245 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008246 status = nfs4_wait_for_completion_rpc_task(task);
8247 if (status == 0)
8248 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008249 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008250 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008251out:
8252 dprintk("<-- %s status=%d\n", __func__, status);
8253 return status;
8254}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008255
8256static void
8257nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8258{
8259 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008260 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008261 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008262
8263 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008264 nfs41_setup_sequence(session, &lgp->args.seq_args,
8265 &lgp->res.seq_res, task);
8266 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008267}
8268
8269static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8270{
8271 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008272
8273 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008274 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008275 dprintk("<-- %s\n", __func__);
8276}
8277
8278static int
8279nfs4_layoutget_handle_exception(struct rpc_task *task,
8280 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8281{
Trond Myklebustee314c22012-10-01 17:25:48 -07008282 struct inode *inode = lgp->args.inode;
8283 struct nfs_server *server = NFS_SERVER(inode);
8284 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008285 int nfs4err = task->tk_status;
8286 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008287 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008288
Boaz Harroshed7e5422014-01-22 20:34:54 +02008289 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008290
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008291 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008292 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008293 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008294
8295 /*
8296 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8297 * on the file. set tk_status to -ENODATA to tell upper layer to
8298 * retry go inband.
8299 */
8300 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008301 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008302 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008303 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008304 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8305 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8306 */
8307 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008308 status = -EOVERFLOW;
8309 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008310 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008311 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008312 * (or clients) writing to the same RAID stripe except when
8313 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008314 *
8315 * Treat it like we would RECALLCONFLICT -- we retry for a little
8316 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008317 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008318 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008319 if (lgp->args.minlength == 0) {
8320 status = -EOVERFLOW;
8321 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008322 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008323 status = -EBUSY;
8324 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008325 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008326 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008327 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008328 case -NFS4ERR_DELEG_REVOKED:
8329 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008330 case -NFS4ERR_EXPIRED:
8331 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008332 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008333 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008334 lo = NFS_I(inode)->layout;
8335 /* If the open stateid was bad, then recover it. */
8336 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8337 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008338 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008339 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008340 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008341 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008342 break;
8343 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008344
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008345 /*
8346 * Mark the bad layout state as invalid, then retry
8347 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008348 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008349 spin_unlock(&inode->i_lock);
8350 pnfs_free_lseg_list(&head);
8351 status = -EAGAIN;
8352 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008353 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008354
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008355 err = nfs4_handle_exception(server, nfs4err, exception);
8356 if (!status) {
8357 if (exception->retry)
8358 status = -EAGAIN;
8359 else
8360 status = err;
8361 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008362out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008363 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008364 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008365}
8366
Idan Kedar85541162012-08-02 11:47:10 +03008367static size_t max_response_pages(struct nfs_server *server)
8368{
8369 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8370 return nfs_page_array_len(0, max_resp_sz);
8371}
8372
8373static void nfs4_free_pages(struct page **pages, size_t size)
8374{
8375 int i;
8376
8377 if (!pages)
8378 return;
8379
8380 for (i = 0; i < size; i++) {
8381 if (!pages[i])
8382 break;
8383 __free_page(pages[i]);
8384 }
8385 kfree(pages);
8386}
8387
8388static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8389{
8390 struct page **pages;
8391 int i;
8392
8393 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8394 if (!pages) {
8395 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8396 return NULL;
8397 }
8398
8399 for (i = 0; i < size; i++) {
8400 pages[i] = alloc_page(gfp_flags);
8401 if (!pages[i]) {
8402 dprintk("%s: failed to allocate page\n", __func__);
8403 nfs4_free_pages(pages, size);
8404 return NULL;
8405 }
8406 }
8407
8408 return pages;
8409}
8410
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008411static void nfs4_layoutget_release(void *calldata)
8412{
8413 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008414 struct inode *inode = lgp->args.inode;
8415 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008416 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008417
8418 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008419 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008420 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008421 put_nfs_open_context(lgp->args.ctx);
8422 kfree(calldata);
8423 dprintk("<-- %s\n", __func__);
8424}
8425
8426static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8427 .rpc_call_prepare = nfs4_layoutget_prepare,
8428 .rpc_call_done = nfs4_layoutget_done,
8429 .rpc_release = nfs4_layoutget_release,
8430};
8431
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008432struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008433nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008434{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008435 struct inode *inode = lgp->args.inode;
8436 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008437 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008438 struct rpc_task *task;
8439 struct rpc_message msg = {
8440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8441 .rpc_argp = &lgp->args,
8442 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008443 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008444 };
8445 struct rpc_task_setup task_setup_data = {
8446 .rpc_client = server->client,
8447 .rpc_message = &msg,
8448 .callback_ops = &nfs4_layoutget_call_ops,
8449 .callback_data = lgp,
8450 .flags = RPC_TASK_ASYNC,
8451 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008452 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008453 struct nfs4_exception exception = {
8454 .inode = inode,
8455 .timeout = *timeout,
8456 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457 int status = 0;
8458
8459 dprintk("--> %s\n", __func__);
8460
Peng Tao4bd5a982014-11-17 11:05:17 +08008461 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8462 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8463
Idan Kedar85541162012-08-02 11:47:10 +03008464 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8465 if (!lgp->args.layout.pages) {
8466 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008467 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008468 }
8469 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8470
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008471 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008473 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008474
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008475 task = rpc_run_task(&task_setup_data);
8476 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008477 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008479 if (status == 0) {
8480 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8481 *timeout = exception.timeout;
8482 }
8483
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008484 trace_nfs4_layoutget(lgp->args.ctx,
8485 &lgp->args.range,
8486 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008487 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008488 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008489
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008490 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8491 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008492 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008493 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008494 rpc_put_task(task);
8495 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008496 if (status)
8497 return ERR_PTR(status);
8498 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008499}
8500
Benny Halevycbe82602011-05-22 19:52:37 +03008501static void
8502nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8503{
8504 struct nfs4_layoutreturn *lrp = calldata;
8505
8506 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008507 nfs41_setup_sequence(lrp->clp->cl_session,
8508 &lrp->args.seq_args,
8509 &lrp->res.seq_res,
8510 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008511}
8512
8513static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8514{
8515 struct nfs4_layoutreturn *lrp = calldata;
8516 struct nfs_server *server;
8517
8518 dprintk("--> %s\n", __func__);
8519
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008520 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008521 return;
8522
8523 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008524 switch (task->tk_status) {
8525 default:
8526 task->tk_status = 0;
8527 case 0:
8528 break;
8529 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008530 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008531 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008532 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008533 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008534 return;
8535 }
Benny Halevycbe82602011-05-22 19:52:37 +03008536 dprintk("<-- %s\n", __func__);
8537}
8538
8539static void nfs4_layoutreturn_release(void *calldata)
8540{
8541 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008542 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008543 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008544
8545 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008546 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008547 if (lrp->res.lrs_present) {
8548 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8549 &lrp->args.range,
8550 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008551 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008552 } else
8553 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008554 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008555 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008556 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008557 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008558 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008559 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008560 kfree(calldata);
8561 dprintk("<-- %s\n", __func__);
8562}
8563
8564static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8565 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8566 .rpc_call_done = nfs4_layoutreturn_done,
8567 .rpc_release = nfs4_layoutreturn_release,
8568};
8569
Peng Tao6c166052014-11-17 09:30:40 +08008570int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008571{
8572 struct rpc_task *task;
8573 struct rpc_message msg = {
8574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8575 .rpc_argp = &lrp->args,
8576 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008577 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008578 };
8579 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008580 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008581 .rpc_message = &msg,
8582 .callback_ops = &nfs4_layoutreturn_call_ops,
8583 .callback_data = lrp,
8584 };
Peng Tao6c166052014-11-17 09:30:40 +08008585 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008586
Andrew Elble99ade3c2015-12-02 09:39:51 -05008587 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8588 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8589 &task_setup_data.rpc_client, &msg);
8590
Benny Halevycbe82602011-05-22 19:52:37 +03008591 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008592 if (!sync) {
8593 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8594 if (!lrp->inode) {
8595 nfs4_layoutreturn_release(lrp);
8596 return -EAGAIN;
8597 }
8598 task_setup_data.flags |= RPC_TASK_ASYNC;
8599 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008600 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008601 task = rpc_run_task(&task_setup_data);
8602 if (IS_ERR(task))
8603 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008604 if (sync)
8605 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008606 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008607 dprintk("<-- %s status=%d\n", __func__, status);
8608 rpc_put_task(task);
8609 return status;
8610}
8611
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008612static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008613_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8614 struct pnfs_device *pdev,
8615 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008616{
8617 struct nfs4_getdeviceinfo_args args = {
8618 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008619 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8620 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008621 };
8622 struct nfs4_getdeviceinfo_res res = {
8623 .pdev = pdev,
8624 };
8625 struct rpc_message msg = {
8626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8627 .rpc_argp = &args,
8628 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008629 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008630 };
8631 int status;
8632
8633 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008634 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008635 if (res.notification & ~args.notify_types)
8636 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008637 if (res.notification != args.notify_types)
8638 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008639
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008640 dprintk("<-- %s status=%d\n", __func__, status);
8641
8642 return status;
8643}
8644
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008645int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8646 struct pnfs_device *pdev,
8647 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008648{
8649 struct nfs4_exception exception = { };
8650 int err;
8651
8652 do {
8653 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008654 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655 &exception);
8656 } while (exception.retry);
8657 return err;
8658}
8659EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8660
Andy Adamson863a3c62011-03-23 13:27:54 +00008661static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8662{
8663 struct nfs4_layoutcommit_data *data = calldata;
8664 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008665 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008666
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008667 nfs41_setup_sequence(session,
8668 &data->args.seq_args,
8669 &data->res.seq_res,
8670 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008671}
8672
8673static void
8674nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8675{
8676 struct nfs4_layoutcommit_data *data = calldata;
8677 struct nfs_server *server = NFS_SERVER(data->args.inode);
8678
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008679 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008680 return;
8681
8682 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008683 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8684 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8685 case -NFS4ERR_BADLAYOUT: /* no layout */
8686 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008687 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008688 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008689 break;
8690 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008691 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008692 rpc_restart_call_prepare(task);
8693 return;
8694 }
8695 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008696}
8697
8698static void nfs4_layoutcommit_release(void *calldata)
8699{
8700 struct nfs4_layoutcommit_data *data = calldata;
8701
Andy Adamsondb29c082011-07-30 20:52:38 -04008702 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008703 nfs_post_op_update_inode_force_wcc(data->args.inode,
8704 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008705 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008706 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008707 kfree(data);
8708}
8709
8710static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8711 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8712 .rpc_call_done = nfs4_layoutcommit_done,
8713 .rpc_release = nfs4_layoutcommit_release,
8714};
8715
8716int
Andy Adamsonef311532011-03-12 02:58:10 -05008717nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008718{
8719 struct rpc_message msg = {
8720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8721 .rpc_argp = &data->args,
8722 .rpc_resp = &data->res,
8723 .rpc_cred = data->cred,
8724 };
8725 struct rpc_task_setup task_setup_data = {
8726 .task = &data->task,
8727 .rpc_client = NFS_CLIENT(data->args.inode),
8728 .rpc_message = &msg,
8729 .callback_ops = &nfs4_layoutcommit_ops,
8730 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008731 };
8732 struct rpc_task *task;
8733 int status = 0;
8734
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008735 dprintk("NFS: initiating layoutcommit call. sync %d "
8736 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008737 data->args.lastbytewritten,
8738 data->args.inode->i_ino);
8739
Trond Myklebust472e2592015-02-05 16:50:30 -05008740 if (!sync) {
8741 data->inode = nfs_igrab_and_active(data->args.inode);
8742 if (data->inode == NULL) {
8743 nfs4_layoutcommit_release(data);
8744 return -EAGAIN;
8745 }
8746 task_setup_data.flags = RPC_TASK_ASYNC;
8747 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008748 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008749 task = rpc_run_task(&task_setup_data);
8750 if (IS_ERR(task))
8751 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008752 if (sync)
8753 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008754 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008755 dprintk("%s: status %d\n", __func__, status);
8756 rpc_put_task(task);
8757 return status;
8758}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008759
Andy Adamson97431202013-08-08 10:57:56 -04008760/**
8761 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8762 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8763 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008764static int
8765_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008766 struct nfs_fsinfo *info,
8767 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008768{
8769 struct nfs41_secinfo_no_name_args args = {
8770 .style = SECINFO_STYLE_CURRENT_FH,
8771 };
8772 struct nfs4_secinfo_res res = {
8773 .flavors = flavors,
8774 };
8775 struct rpc_message msg = {
8776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8777 .rpc_argp = &args,
8778 .rpc_resp = &res,
8779 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008780 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008781 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008782 int status;
8783
8784 if (use_integrity) {
8785 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008786 cred = nfs4_get_clid_cred(server->nfs_client);
8787 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008788 }
8789
8790 dprintk("--> %s\n", __func__);
8791 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8792 &res.seq_res, 0);
8793 dprintk("<-- %s status=%d\n", __func__, status);
8794
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008795 if (cred)
8796 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008797
8798 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008799}
8800
8801static int
8802nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8803 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8804{
8805 struct nfs4_exception exception = { };
8806 int err;
8807 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008808 /* first try using integrity protection */
8809 err = -NFS4ERR_WRONGSEC;
8810
8811 /* try to use integrity protection with machine cred */
8812 if (_nfs4_is_integrity_protected(server->nfs_client))
8813 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8814 flavors, true);
8815
8816 /*
8817 * if unable to use integrity protection, or SECINFO with
8818 * integrity protection returns NFS4ERR_WRONGSEC (which is
8819 * disallowed by spec, but exists in deployed servers) use
8820 * the current filesystem's rpc_client and the user cred.
8821 */
8822 if (err == -NFS4ERR_WRONGSEC)
8823 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8824 flavors, false);
8825
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008826 switch (err) {
8827 case 0:
8828 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008829 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008830 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008831 default:
8832 err = nfs4_handle_exception(server, err, &exception);
8833 }
8834 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008835out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008836 return err;
8837}
8838
8839static int
8840nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8841 struct nfs_fsinfo *info)
8842{
8843 int err;
8844 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008845 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008846 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008847 struct nfs4_secinfo4 *secinfo;
8848 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008849
8850 page = alloc_page(GFP_KERNEL);
8851 if (!page) {
8852 err = -ENOMEM;
8853 goto out;
8854 }
8855
8856 flavors = page_address(page);
8857 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8858
8859 /*
8860 * Fall back on "guess and check" method if
8861 * the server doesn't support SECINFO_NO_NAME
8862 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008863 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008864 err = nfs4_find_root_sec(server, fhandle, info);
8865 goto out_freepage;
8866 }
8867 if (err)
8868 goto out_freepage;
8869
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008870 for (i = 0; i < flavors->num_flavors; i++) {
8871 secinfo = &flavors->flavors[i];
8872
8873 switch (secinfo->flavor) {
8874 case RPC_AUTH_NULL:
8875 case RPC_AUTH_UNIX:
8876 case RPC_AUTH_GSS:
8877 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8878 &secinfo->flavor_info);
8879 break;
8880 default:
8881 flavor = RPC_AUTH_MAXFLAVOR;
8882 break;
8883 }
8884
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008885 if (!nfs_auth_info_match(&server->auth_info, flavor))
8886 flavor = RPC_AUTH_MAXFLAVOR;
8887
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008888 if (flavor != RPC_AUTH_MAXFLAVOR) {
8889 err = nfs4_lookup_root_sec(server, fhandle,
8890 info, flavor);
8891 if (!err)
8892 break;
8893 }
8894 }
8895
8896 if (flavor == RPC_AUTH_MAXFLAVOR)
8897 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008898
8899out_freepage:
8900 put_page(page);
8901 if (err == -EACCES)
8902 return -EPERM;
8903out:
8904 return err;
8905}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008906
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008907static int _nfs41_test_stateid(struct nfs_server *server,
8908 nfs4_stateid *stateid,
8909 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008910{
8911 int status;
8912 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008913 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008914 };
8915 struct nfs41_test_stateid_res res;
8916 struct rpc_message msg = {
8917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8918 .rpc_argp = &args,
8919 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008920 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008921 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008922 struct rpc_clnt *rpc_client = server->client;
8923
8924 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8925 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008926
Chuck Lever38527b12012-07-11 16:30:23 -04008927 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008928 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008929 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008930 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008931 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008932 if (status != NFS_OK) {
8933 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008934 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008935 }
8936 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008937 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008938}
8939
Trond Myklebust43912bb2016-09-22 13:38:56 -04008940static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8941 int err, struct nfs4_exception *exception)
8942{
8943 exception->retry = 0;
8944 switch(err) {
8945 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008946 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008947 nfs4_handle_exception(server, err, exception);
8948 break;
8949 case -NFS4ERR_BADSESSION:
8950 case -NFS4ERR_BADSLOT:
8951 case -NFS4ERR_BAD_HIGH_SLOT:
8952 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8953 case -NFS4ERR_DEADSESSION:
8954 nfs4_do_handle_exception(server, err, exception);
8955 }
8956}
8957
Chuck Lever38527b12012-07-11 16:30:23 -04008958/**
8959 * nfs41_test_stateid - perform a TEST_STATEID operation
8960 *
8961 * @server: server / transport on which to perform the operation
8962 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008963 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008964 *
8965 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8966 * Otherwise a negative NFS4ERR value is returned if the operation
8967 * failed or the state ID is not currently valid.
8968 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008969static int nfs41_test_stateid(struct nfs_server *server,
8970 nfs4_stateid *stateid,
8971 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008972{
8973 struct nfs4_exception exception = { };
8974 int err;
8975 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008976 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008977 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008978 } while (exception.retry);
8979 return err;
8980}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008981
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008982struct nfs_free_stateid_data {
8983 struct nfs_server *server;
8984 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008985 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008986};
8987
8988static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8989{
8990 struct nfs_free_stateid_data *data = calldata;
8991 nfs41_setup_sequence(nfs4_get_session(data->server),
8992 &data->args.seq_args,
8993 &data->res.seq_res,
8994 task);
8995}
8996
8997static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8998{
8999 struct nfs_free_stateid_data *data = calldata;
9000
9001 nfs41_sequence_done(task, &data->res.seq_res);
9002
9003 switch (task->tk_status) {
9004 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009005 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009006 rpc_restart_call_prepare(task);
9007 }
9008}
9009
9010static void nfs41_free_stateid_release(void *calldata)
9011{
9012 kfree(calldata);
9013}
9014
Trond Myklebust17f26b12013-08-21 15:48:42 -04009015static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009016 .rpc_call_prepare = nfs41_free_stateid_prepare,
9017 .rpc_call_done = nfs41_free_stateid_done,
9018 .rpc_release = nfs41_free_stateid_release,
9019};
9020
9021static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009022 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009023 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009024 bool privileged)
9025{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009026 struct rpc_message msg = {
9027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009028 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009029 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009030 struct rpc_task_setup task_setup = {
9031 .rpc_client = server->client,
9032 .rpc_message = &msg,
9033 .callback_ops = &nfs41_free_stateid_ops,
9034 .flags = RPC_TASK_ASYNC,
9035 };
9036 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009037
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009038 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9039 &task_setup.rpc_client, &msg);
9040
Chuck Lever38527b12012-07-11 16:30:23 -04009041 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009042 data = kmalloc(sizeof(*data), GFP_NOFS);
9043 if (!data)
9044 return ERR_PTR(-ENOMEM);
9045 data->server = server;
9046 nfs4_stateid_copy(&data->args.stateid, stateid);
9047
9048 task_setup.callback_data = data;
9049
9050 msg.rpc_argp = &data->args;
9051 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009052 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053 if (privileged)
9054 nfs4_set_sequence_privileged(&data->args.seq_args);
9055
9056 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009057}
9058
Chuck Lever38527b12012-07-11 16:30:23 -04009059/**
9060 * nfs41_free_stateid - perform a FREE_STATEID operation
9061 *
9062 * @server: server / transport on which to perform the operation
9063 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009064 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009065 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009066 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009067 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009068 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009069static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009070 const nfs4_stateid *stateid,
9071 struct rpc_cred *cred,
9072 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009073{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009074 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009075
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009076 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009077 if (IS_ERR(task))
9078 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009079 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009080 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009081}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009082
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009083static void
9084nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009085{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009086 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009087
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009088 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009089 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009090}
9091
Trond Myklebust36281ca2012-03-04 18:13:56 -05009092static bool nfs41_match_stateid(const nfs4_stateid *s1,
9093 const nfs4_stateid *s2)
9094{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009095 if (s1->type != s2->type)
9096 return false;
9097
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009098 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009099 return false;
9100
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009101 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009102 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009103 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009104 return true;
9105
9106 return false;
9107}
9108
Andy Adamson557134a2009-04-01 09:21:53 -04009109#endif /* CONFIG_NFS_V4_1 */
9110
Trond Myklebust36281ca2012-03-04 18:13:56 -05009111static bool nfs4_match_stateid(const nfs4_stateid *s1,
9112 const nfs4_stateid *s2)
9113{
Trond Myklebustf597c532012-03-04 18:13:56 -05009114 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009115}
9116
9117
Trond Myklebust17280172012-03-11 13:11:00 -04009118static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009119 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009120 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121 .recover_open = nfs4_open_reclaim,
9122 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009123 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009124 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125};
9126
Andy Adamson591d71c2009-04-01 09:22:47 -04009127#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009128static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009129 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9130 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9131 .recover_open = nfs4_open_reclaim,
9132 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009133 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009134 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009135 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009136};
9137#endif /* CONFIG_NFS_V4_1 */
9138
Trond Myklebust17280172012-03-11 13:11:00 -04009139static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009140 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009141 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009142 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009144 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145};
9146
Andy Adamson591d71c2009-04-01 09:22:47 -04009147#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009148static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009149 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9150 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009151 .recover_open = nfs41_open_expired,
9152 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009153 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009154};
9155#endif /* CONFIG_NFS_V4_1 */
9156
Trond Myklebust17280172012-03-11 13:11:00 -04009157static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009158 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009159 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009160 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009161};
9162
9163#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009164static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009165 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009166 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009167 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009168};
9169#endif
9170
Chuck Leverec011fe2013-10-17 14:12:39 -04009171static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009172 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009173 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009174};
9175
9176#if defined(CONFIG_NFS_V4_1)
9177static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009178 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009179 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009180};
9181#endif /* CONFIG_NFS_V4_1 */
9182
Trond Myklebust97dc1352010-06-16 09:52:26 -04009183static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9184 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009185 .init_caps = NFS_CAP_READDIRPLUS
9186 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009187 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009188 .init_client = nfs40_init_client,
9189 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009190 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009191 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009192 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009193 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009194 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009195 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009196 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9197 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9198 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009199 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009200};
9201
9202#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009203static struct nfs_seqid *
9204nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9205{
9206 return NULL;
9207}
9208
Trond Myklebust97dc1352010-06-16 09:52:26 -04009209static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9210 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009211 .init_caps = NFS_CAP_READDIRPLUS
9212 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009213 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009214 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009215 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009216 .init_client = nfs41_init_client,
9217 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009218 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009219 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009220 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009221 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009222 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009223 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009224 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009225 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9226 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9227 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009228 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009229};
9230#endif
9231
Steve Dickson42c2c422013-05-22 12:50:38 -04009232#if defined(CONFIG_NFS_V4_2)
9233static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9234 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009235 .init_caps = NFS_CAP_READDIRPLUS
9236 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009237 | NFS_CAP_POSIX_LOCK
9238 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009239 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009240 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009241 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009242 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009243 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009244 | NFS_CAP_LAYOUTSTATS
9245 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009246 .init_client = nfs41_init_client,
9247 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009248 .match_stateid = nfs41_match_stateid,
9249 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009250 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009251 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009252 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009253 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009254 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009255 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9256 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9257 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009258 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009259};
9260#endif
9261
Trond Myklebust97dc1352010-06-16 09:52:26 -04009262const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9263 [0] = &nfs_v4_0_minor_ops,
9264#if defined(CONFIG_NFS_V4_1)
9265 [1] = &nfs_v4_1_minor_ops,
9266#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009267#if defined(CONFIG_NFS_V4_2)
9268 [2] = &nfs_v4_2_minor_ops,
9269#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009270};
9271
Trond Myklebust13997822016-07-24 17:10:52 -04009272static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009273{
9274 ssize_t error, error2;
9275
9276 error = generic_listxattr(dentry, list, size);
9277 if (error < 0)
9278 return error;
9279 if (list) {
9280 list += error;
9281 size -= error;
9282 }
9283
9284 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9285 if (error2 < 0)
9286 return error2;
9287 return error + error2;
9288}
9289
Trond Myklebust17f26b12013-08-21 15:48:42 -04009290static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009291 .create = nfs_create,
9292 .lookup = nfs_lookup,
9293 .atomic_open = nfs_atomic_open,
9294 .link = nfs_link,
9295 .unlink = nfs_unlink,
9296 .symlink = nfs_symlink,
9297 .mkdir = nfs_mkdir,
9298 .rmdir = nfs_rmdir,
9299 .mknod = nfs_mknod,
9300 .rename = nfs_rename,
9301 .permission = nfs_permission,
9302 .getattr = nfs_getattr,
9303 .setattr = nfs_setattr,
9304 .getxattr = generic_getxattr,
9305 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009306 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009307 .removexattr = generic_removexattr,
9308};
9309
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009310static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009311 .permission = nfs_permission,
9312 .getattr = nfs_getattr,
9313 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009314 .getxattr = generic_getxattr,
9315 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009316 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009317 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009318};
9319
David Howells509de812006-08-22 20:06:11 -04009320const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 .version = 4, /* protocol version */
9322 .dentry_ops = &nfs4_dentry_operations,
9323 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009324 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009325 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009327 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009328 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 .getattr = nfs4_proc_getattr,
9330 .setattr = nfs4_proc_setattr,
9331 .lookup = nfs4_proc_lookup,
9332 .access = nfs4_proc_access,
9333 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334 .create = nfs4_proc_create,
9335 .remove = nfs4_proc_remove,
9336 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009337 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009339 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009340 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009341 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 .link = nfs4_proc_link,
9343 .symlink = nfs4_proc_symlink,
9344 .mkdir = nfs4_proc_mkdir,
9345 .rmdir = nfs4_proc_remove,
9346 .readdir = nfs4_proc_readdir,
9347 .mknod = nfs4_proc_mknod,
9348 .statfs = nfs4_proc_statfs,
9349 .fsinfo = nfs4_proc_fsinfo,
9350 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009351 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009353 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009355 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009357 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009359 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009360 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009362 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009363 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009364 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009365 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009366 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009367 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009368 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009369 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009370 .create_server = nfs4_create_server,
9371 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372};
9373
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009374static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009375 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009376 .list = nfs4_xattr_list_nfs4_acl,
9377 .get = nfs4_xattr_get_nfs4_acl,
9378 .set = nfs4_xattr_set_nfs4_acl,
9379};
9380
9381const struct xattr_handler *nfs4_xattr_handlers[] = {
9382 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009383#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9384 &nfs4_xattr_nfs4_label_handler,
9385#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009386 NULL
9387};
9388
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389/*
9390 * Local variables:
9391 * c-basic-offset: 8
9392 * End:
9393 */