blob: d1e60c915a07e51e863fd5e82c5079df70c56f26 [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;
400 switch(errorcode) {
401 case 0:
402 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500403 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400404 case -NFS4ERR_DELEG_REVOKED:
405 case -NFS4ERR_ADMIN_REVOKED:
406 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400407 if (inode) {
408 int err;
409
410 err = nfs_async_inode_return_delegation(inode,
411 stateid);
412 if (err == 0)
413 goto wait_on_recovery;
414 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
415 exception->retry = 1;
416 break;
417 }
418 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500419 if (state == NULL)
420 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400421 ret = nfs4_schedule_stateid_recovery(server, state);
422 if (ret < 0)
423 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500424 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400425 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400426 if (state != NULL) {
427 ret = nfs4_schedule_stateid_recovery(server, state);
428 if (ret < 0)
429 break;
430 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500433 nfs4_schedule_lease_recovery(clp);
434 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400435 case -NFS4ERR_MOVED:
436 ret = nfs4_schedule_migration_recovery(server);
437 if (ret < 0)
438 break;
439 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400440 case -NFS4ERR_LEASE_MOVED:
441 nfs4_schedule_lease_moved_recovery(clp);
442 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500443#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400444 case -NFS4ERR_BADSESSION:
445 case -NFS4ERR_BADSLOT:
446 case -NFS4ERR_BAD_HIGH_SLOT:
447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
448 case -NFS4ERR_DEADSESSION:
449 case -NFS4ERR_SEQ_FALSE_RETRY:
450 case -NFS4ERR_SEQ_MISORDERED:
451 dprintk("%s ERROR: %d Reset session\n", __func__,
452 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400453 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500457 if (exception->timeout > HZ) {
458 /* We have retried a decent amount, time to
459 * fail
460 */
461 ret = -EBUSY;
462 break;
463 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400465 nfs_inc_server_stats(server, NFSIOS_DELAY);
466 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400467 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400468 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 1;
470 return 0;
471
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400472 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_OLD_STATEID:
474 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800475 break;
476 case -NFS4ERR_BADOWNER:
477 /* The following works around a Linux server bug! */
478 case -NFS4ERR_BADNAME:
479 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
480 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
481 exception->retry = 1;
482 printk(KERN_WARNING "NFS: v4 server %s "
483 "does not accept raw "
484 "uid/gids. "
485 "Reenabling the idmapper.\n",
486 server->nfs_client->cl_hostname);
487 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500488 }
489 /* We failed to handle the error */
490 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500491wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400492 exception->recovering = 1;
493 return 0;
494}
495
496/* This is the error handling routine for processes that are allowed
497 * to sleep.
498 */
499int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
500{
501 struct nfs_client *clp = server->nfs_client;
502 int ret;
503
504 ret = nfs4_do_handle_exception(server, errorcode, exception);
505 if (exception->delay) {
506 ret = nfs4_delay(server->client, &exception->timeout);
507 goto out_retry;
508 }
509 if (exception->recovering) {
510 ret = nfs4_wait_clnt_recover(clp);
511 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
512 return -EIO;
513 goto out_retry;
514 }
515 return ret;
516out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500520}
521
Trond Myklebust037fc982015-09-20 15:51:00 -0400522static int
523nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
524 int errorcode, struct nfs4_exception *exception)
525{
526 struct nfs_client *clp = server->nfs_client;
527 int ret;
528
529 ret = nfs4_do_handle_exception(server, errorcode, exception);
530 if (exception->delay) {
531 rpc_delay(task, nfs4_update_delay(&exception->timeout));
532 goto out_retry;
533 }
534 if (exception->recovering) {
535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
538 goto out_retry;
539 }
540 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
541 ret = -EIO;
542 return ret;
543out_retry:
544 if (ret == 0)
545 exception->retry = 1;
546 return ret;
547}
548
549static int
550nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
551 struct nfs4_state *state, long *timeout)
552{
553 struct nfs4_exception exception = {
554 .state = state,
555 };
556
557 if (task->tk_status >= 0)
558 return 0;
559 if (timeout)
560 exception.timeout = *timeout;
561 task->tk_status = nfs4_async_handle_exception(task, server,
562 task->tk_status,
563 &exception);
564 if (exception.delay && timeout)
565 *timeout = exception.timeout;
566 if (exception.retry)
567 return -EAGAIN;
568 return 0;
569}
570
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400571/*
572 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
573 * or 'false' otherwise.
574 */
575static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
576{
577 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
578
579 if (flavor == RPC_AUTH_GSS_KRB5I ||
580 flavor == RPC_AUTH_GSS_KRB5P)
581 return true;
582
583 return false;
584}
Trond Myklebust65de8722008-12-23 15:21:44 -0500585
Trond Myklebust452e9352010-07-31 14:29:06 -0400586static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_lock(&clp->cl_lock);
589 if (time_before(clp->cl_last_renewal,timestamp))
590 clp->cl_last_renewal = timestamp;
591 spin_unlock(&clp->cl_lock);
592}
593
Trond Myklebust452e9352010-07-31 14:29:06 -0400594static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
595{
Trond Myklebustbe824162015-07-05 14:50:46 -0400596 struct nfs_client *clp = server->nfs_client;
597
598 if (!nfs4_has_session(clp))
599 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400600}
601
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400602struct nfs4_call_sync_data {
603 const struct nfs_server *seq_server;
604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
606};
607
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800608void nfs4_init_sequence(struct nfs4_sequence_args *args,
609 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400610{
611 args->sa_slot = NULL;
612 args->sa_cache_this = cache_reply;
613 args->sa_privileged = 0;
614
615 res->sr_slot = NULL;
616}
617
618static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
619{
620 args->sa_privileged = 1;
621}
622
Peng Taocb04ad22014-06-11 05:24:15 +0800623int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
624 struct nfs4_sequence_args *args,
625 struct nfs4_sequence_res *res,
626 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400627{
Chuck Lever3bd23842013-08-09 12:49:19 -0400628 struct nfs4_slot *slot;
629
630 /* slot already allocated? */
631 if (res->sr_slot != NULL)
632 goto out_start;
633
634 spin_lock(&tbl->slot_tbl_lock);
635 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
636 goto out_sleep;
637
638 slot = nfs4_alloc_slot(tbl);
639 if (IS_ERR(slot)) {
640 if (slot == ERR_PTR(-ENOMEM))
641 task->tk_timeout = HZ >> 2;
642 goto out_sleep;
643 }
644 spin_unlock(&tbl->slot_tbl_lock);
645
Trond Myklebust0a014a42016-09-22 13:38:51 -0400646 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400647 args->sa_slot = slot;
648 res->sr_slot = slot;
649
650out_start:
651 rpc_call_start(task);
652 return 0;
653
654out_sleep:
655 if (args->sa_privileged)
656 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
657 NULL, RPC_PRIORITY_PRIVILEGED);
658 else
659 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
660 spin_unlock(&tbl->slot_tbl_lock);
661 return -EAGAIN;
662}
Peng Taocb04ad22014-06-11 05:24:15 +0800663EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400664
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400665static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400666{
667 struct nfs4_slot *slot = res->sr_slot;
668 struct nfs4_slot_table *tbl;
669
Chuck Lever3bd23842013-08-09 12:49:19 -0400670 tbl = slot->table;
671 spin_lock(&tbl->slot_tbl_lock);
672 if (!nfs41_wake_and_assign_slot(tbl, slot))
673 nfs4_free_slot(tbl, slot);
674 spin_unlock(&tbl->slot_tbl_lock);
675
676 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400677}
678
679static int nfs40_sequence_done(struct rpc_task *task,
680 struct nfs4_sequence_res *res)
681{
682 if (res->sr_slot != NULL)
683 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400684 return 1;
685}
686
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400687#if defined(CONFIG_NFS_V4_1)
688
Trond Myklebustd185a332010-06-16 09:52:25 -0400689static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400690{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500691 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400692 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500693 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500694 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400695
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500698
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
703
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500704 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
707 */
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
710
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500712 send_new_highest_used_slotid = false;
713 goto out_unlock;
714 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500716
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400721 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500722 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400723 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400724 if (waitqueue_active(&tbl->slot_waitq))
725 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400726}
727
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400728static int nfs41_sequence_process(struct rpc_task *task,
729 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400730{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500731 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500732 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400733 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500734 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500735 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400736
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500737 if (slot == NULL)
738 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400739 /* don't increment the sequence number if the task wasn't sent */
740 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741 goto out;
742
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500743 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500744
Trond Myklebustac20d162012-12-15 15:36:07 -0500745 if (slot->interrupted) {
746 slot->interrupted = 0;
747 interrupted = true;
748 }
749
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400750 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500751 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400752 switch (res->sr_status) {
753 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400754 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400755 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500756 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500757 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500758 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400759 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
760 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500761 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400762 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 case 1:
764 /*
765 * sr_status remains 1 if an RPC level error occurred.
766 * The server may or may not have processed the sequence
767 * operation..
768 * Mark the slot as having hosted an interrupted RPC call.
769 */
770 slot->interrupted = 1;
771 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 case -NFS4ERR_DELAY:
773 /* The server detected a resend of the RPC call and
774 * returned NFS4ERR_DELAY as per Section 2.10.6.2
775 * of RFC5661.
776 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500777 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400778 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500779 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500780 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400781 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500782 case -NFS4ERR_BADSLOT:
783 /*
784 * The slot id we used was probably retired. Try again
785 * using a different slot id.
786 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500787 goto retry_nowait;
788 case -NFS4ERR_SEQ_MISORDERED:
789 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500790 * Was the last operation on this sequence interrupted?
791 * If so, retry after bumping the sequence number.
792 */
793 if (interrupted) {
794 ++slot->seq_nr;
795 goto retry_nowait;
796 }
797 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500798 * Could this slot have been previously retired?
799 * If so, then the server may be expecting seq_nr = 1!
800 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500801 if (slot->seq_nr != 1) {
802 slot->seq_nr = 1;
803 goto retry_nowait;
804 }
805 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500806 case -NFS4ERR_SEQ_FALSE_RETRY:
807 ++slot->seq_nr;
808 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400809 default:
810 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400811 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400812 }
813out:
814 /* The session may be reset by one of the error handlers. */
815 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500816out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500817 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500818retry_nowait:
819 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400820 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500821 task->tk_status = 0;
822 ret = 0;
823 }
824 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400825out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400826 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400827 goto out;
828 rpc_delay(task, NFS4_POLL_RETRY_MAX);
829 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400830}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400831
832int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
833{
834 if (!nfs41_sequence_process(task, res))
835 return 0;
836 if (res->sr_slot != NULL)
837 nfs41_sequence_free_slot(res);
838 return 1;
839
840}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500841EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400842
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400843static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
844{
845 if (res->sr_slot == NULL)
846 return 1;
847 if (res->sr_slot->table->session != NULL)
848 return nfs41_sequence_process(task, res);
849 return nfs40_sequence_done(task, res);
850}
851
852static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
853{
854 if (res->sr_slot != NULL) {
855 if (res->sr_slot->table->session != NULL)
856 nfs41_sequence_free_slot(res);
857 else
858 nfs40_sequence_free_slot(res);
859 }
860}
861
Peng Tao2c4b1312014-06-11 05:24:16 +0800862int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400863{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500864 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400865 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400866 if (!res->sr_slot->table->session)
867 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400868 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400869}
Peng Tao2c4b1312014-06-11 05:24:16 +0800870EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400871
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000872int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400873 struct nfs4_sequence_args *args,
874 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400875 struct rpc_task *task)
876{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400877 struct nfs4_slot *slot;
878 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400879
880 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400881 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400882 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400883 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400884
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400885 tbl = &session->fc_slot_table;
886
Trond Myklebust69d206b2012-11-22 13:21:02 -0500887 task->tk_timeout = 0;
888
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400890 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400891 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500892 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500893 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400894 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400895 }
896
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500897 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500898 if (IS_ERR(slot)) {
899 /* If out of memory, try again in 1/4 second */
900 if (slot == ERR_PTR(-ENOMEM))
901 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400902 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400903 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400904 }
905 spin_unlock(&tbl->slot_tbl_lock);
906
Trond Myklebust0a014a42016-09-22 13:38:51 -0400907 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500908 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400909
Chuck Levere8d92382013-08-09 12:47:51 -0400910 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400912
Benny Halevydfb4f3092010-09-24 09:17:01 -0400913 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500914 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400915 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 /*
917 * sr_status is only set in decode_sequence, and so will remain
918 * set to 1 if an rpc level failure occurs.
919 */
920 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400921 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400922out_success:
923 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400924 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400925out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400926 /* Privileged tasks are queued with top priority */
927 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400928 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
929 NULL, RPC_PRIORITY_PRIVILEGED);
930 else
931 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400932 spin_unlock(&tbl->slot_tbl_lock);
933 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400934}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000935EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400936
Chuck Lever5a580e02013-08-09 12:48:09 -0400937static int nfs4_setup_sequence(const struct nfs_server *server,
938 struct nfs4_sequence_args *args,
939 struct nfs4_sequence_res *res,
940 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400941{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400942 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400943 int ret = 0;
944
Chuck Lever3bd23842013-08-09 12:49:19 -0400945 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800946 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
947 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400948
Chuck Levere8d92382013-08-09 12:47:51 -0400949 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400950 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400951 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400952
Trond Myklebust9d12b212012-01-17 22:04:25 -0500953 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400954
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955 dprintk("<-- %s status=%d\n", __func__, ret);
956 return ret;
957}
958
Andy Adamsonce5039c2009-04-01 09:22:13 -0400959static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
960{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400961 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400962 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400963
Trond Myklebust035168ab2010-06-16 09:52:26 -0400964 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
965
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400966 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967}
968
Andy Adamson69ab40c2009-04-01 09:22:19 -0400969static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
970{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400971 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400972
Trond Myklebust14516c32010-07-31 14:29:06 -0400973 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400974}
975
Trond Myklebust17280172012-03-11 13:11:00 -0400976static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400979};
980
Chuck Lever3bd23842013-08-09 12:49:19 -0400981#else /* !CONFIG_NFS_V4_1 */
982
Chuck Lever5a580e02013-08-09 12:48:09 -0400983static int nfs4_setup_sequence(const struct nfs_server *server,
984 struct nfs4_sequence_args *args,
985 struct nfs4_sequence_res *res,
986 struct rpc_task *task)
987{
Peng Taocb04ad22014-06-11 05:24:15 +0800988 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
989 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400990}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400991
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400992static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
993{
994 return nfs40_sequence_done(task, res);
995}
996
997static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
998{
999 if (res->sr_slot != NULL)
1000 nfs40_sequence_free_slot(res);
1001}
1002
Peng Tao2c4b1312014-06-11 05:24:16 +08001003int nfs4_sequence_done(struct rpc_task *task,
1004 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001005{
Chuck Lever3bd23842013-08-09 12:49:19 -04001006 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001007}
Peng Tao2c4b1312014-06-11 05:24:16 +08001008EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001009
1010#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001011
Chuck Lever9915ea72013-08-09 12:48:27 -04001012static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1013{
1014 struct nfs4_call_sync_data *data = calldata;
1015 nfs4_setup_sequence(data->seq_server,
1016 data->seq_args, data->seq_res, task);
1017}
1018
1019static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1020{
1021 struct nfs4_call_sync_data *data = calldata;
1022 nfs4_sequence_done(task, data->seq_res);
1023}
1024
1025static const struct rpc_call_ops nfs40_call_sync_ops = {
1026 .rpc_call_prepare = nfs40_call_sync_prepare,
1027 .rpc_call_done = nfs40_call_sync_done,
1028};
1029
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001030static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001031 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001033 struct nfs4_sequence_args *args,
1034 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001035{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001036 int ret;
1037 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001038 struct nfs_client *clp = server->nfs_client;
1039 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 .seq_server = server,
1041 .seq_args = args,
1042 .seq_res = res,
1043 };
1044 struct rpc_task_setup task_setup = {
1045 .rpc_client = clnt,
1046 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001047 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 .callback_data = &data
1049 };
1050
1051 task = rpc_run_task(&task_setup);
1052 if (IS_ERR(task))
1053 ret = PTR_ERR(task);
1054 else {
1055 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001056 rpc_put_task(task);
1057 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001058 return ret;
1059}
1060
Bryan Schumaker7c513052011-03-24 17:12:24 +00001061int nfs4_call_sync(struct rpc_clnt *clnt,
1062 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001063 struct rpc_message *msg,
1064 struct nfs4_sequence_args *args,
1065 struct nfs4_sequence_res *res,
1066 int cache_reply)
1067{
Chuck Levera9c92d62013-08-09 12:48:18 -04001068 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001069 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1073{
1074 struct nfs_inode *nfsi = NFS_I(dir);
1075
1076 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001077 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001078 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001080 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001081 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001082 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 spin_unlock(&dir->i_lock);
1084}
1085
1086struct nfs4_opendata {
1087 struct kref kref;
1088 struct nfs_openargs o_arg;
1089 struct nfs_openres o_res;
1090 struct nfs_open_confirmargs c_arg;
1091 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001092 struct nfs4_string owner_name;
1093 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001094 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001096 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001098 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct nfs4_state_owner *owner;
1100 struct nfs4_state *state;
1101 struct iattr attrs;
1102 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001103 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001104 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001105 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001106 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int cancelled;
1108};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001109
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001110static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1111 int err, struct nfs4_exception *exception)
1112{
1113 if (err != -EINVAL)
1114 return false;
1115 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1116 return false;
1117 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1118 exception->retry = 1;
1119 return true;
1120}
1121
Trond Myklebust6ae37332015-01-30 14:21:14 -05001122static u32
1123nfs4_map_atomic_open_share(struct nfs_server *server,
1124 fmode_t fmode, int openflags)
1125{
1126 u32 res = 0;
1127
1128 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1129 case FMODE_READ:
1130 res = NFS4_SHARE_ACCESS_READ;
1131 break;
1132 case FMODE_WRITE:
1133 res = NFS4_SHARE_ACCESS_WRITE;
1134 break;
1135 case FMODE_READ|FMODE_WRITE:
1136 res = NFS4_SHARE_ACCESS_BOTH;
1137 }
1138 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1139 goto out;
1140 /* Want no delegation if we're using O_DIRECT */
1141 if (openflags & O_DIRECT)
1142 res |= NFS4_SHARE_WANT_NO_DELEG;
1143out:
1144 return res;
1145}
1146
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001147static enum open_claim_type4
1148nfs4_map_atomic_open_claim(struct nfs_server *server,
1149 enum open_claim_type4 claim)
1150{
1151 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1152 return claim;
1153 switch (claim) {
1154 default:
1155 return claim;
1156 case NFS4_OPEN_CLAIM_FH:
1157 return NFS4_OPEN_CLAIM_NULL;
1158 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1159 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1160 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1161 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1162 }
1163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166{
1167 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001168 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 p->o_res.seqid = p->o_arg.seqid;
1170 p->c_res.seqid = p->c_arg.seqid;
1171 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001172 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001174 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175}
1176
Al Viro82a2c1b2011-06-22 18:30:55 -04001177static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001179 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001180 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001181 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001182 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183{
Al Viro82a2c1b2011-06-22 18:30:55 -04001184 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001185 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001187 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188 struct nfs4_opendata *p;
1189
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001190 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001191 if (p == NULL)
1192 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001193
1194 p->f_label = nfs4_label_alloc(server, gfp_mask);
1195 if (IS_ERR(p->f_label))
1196 goto err_free_p;
1197
Kinglong Meea49c2692015-07-27 15:31:38 +08001198 p->a_label = nfs4_label_alloc(server, gfp_mask);
1199 if (IS_ERR(p->a_label))
1200 goto err_free_f;
1201
Trond Myklebust63f5f792015-01-23 19:19:25 -05001202 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1203 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001204 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001205 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001206 nfs_sb_active(dentry->d_sb);
1207 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 p->dir = parent;
1209 p->owner = sp;
1210 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001211 p->o_arg.open_flags = flags;
1212 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001213 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1214 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001215 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1216 * will return permission denied for all bits until close */
1217 if (!(flags & O_EXCL)) {
1218 /* ask server to check for all possible rights as results
1219 * are cached */
1220 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1221 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001222 }
David Howells7539bba2006-08-22 20:06:09 -04001223 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001224 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1225 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001226 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001228 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001229 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001230 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001231 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1232 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001233 case NFS4_OPEN_CLAIM_NULL:
1234 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1235 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1236 p->o_arg.fh = NFS_FH(dir);
1237 break;
1238 case NFS4_OPEN_CLAIM_PREVIOUS:
1239 case NFS4_OPEN_CLAIM_FH:
1240 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1241 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001242 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001243 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001244 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001245 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001246
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247 p->o_arg.u.attrs = &p->attrs;
1248 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001249
1250 verf[0] = jiffies;
1251 verf[1] = current->pid;
1252 memcpy(p->o_arg.u.verifier.data, verf,
1253 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001254 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001255 p->c_arg.fh = &p->o_res.fh;
1256 p->c_arg.stateid = &p->o_res.stateid;
1257 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001258 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001259 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001260 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001261
1262err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001263 nfs4_label_free(p->a_label);
1264err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001265 nfs4_label_free(p->f_label);
1266err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 kfree(p);
1268err:
1269 dput(parent);
1270 return NULL;
1271}
1272
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 struct nfs4_opendata *p = container_of(kref,
1276 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001277 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278
1279 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001280 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001281 if (p->state != NULL)
1282 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001284
Kinglong Meea49c2692015-07-27 15:31:38 +08001285 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001286 nfs4_label_free(p->f_label);
1287
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001288 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001289 dput(p->dentry);
1290 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001291 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001292 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 kfree(p);
1294}
1295
1296static void nfs4_opendata_put(struct nfs4_opendata *p)
1297{
1298 if (p != NULL)
1299 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001300}
1301
Trond Myklebust06f814a2006-01-03 09:55:07 +01001302static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1303{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001304 int ret;
1305
Trond Myklebust06f814a2006-01-03 09:55:07 +01001306 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001307 return ret;
1308}
1309
Trond Myklebust24311f82015-09-20 10:50:17 -04001310static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1311 fmode_t fmode)
1312{
1313 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1314 case FMODE_READ|FMODE_WRITE:
1315 return state->n_rdwr != 0;
1316 case FMODE_WRITE:
1317 return state->n_wronly != 0;
1318 case FMODE_READ:
1319 return state->n_rdonly != 0;
1320 }
1321 WARN_ON_ONCE(1);
1322 return false;
1323}
1324
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001325static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001326{
1327 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001328
Trond Myklebust536e43d2012-01-17 22:04:26 -05001329 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001330 goto out;
1331 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001332 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001333 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1334 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001335 break;
1336 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001337 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1338 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 break;
1340 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001341 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1342 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001345 return ret;
1346}
1347
Trond Myklebust2a606182015-08-19 22:30:00 -05001348static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1349 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001351 if (delegation == NULL)
1352 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001355 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1356 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001357 switch (claim) {
1358 case NFS4_OPEN_CLAIM_NULL:
1359 case NFS4_OPEN_CLAIM_FH:
1360 break;
1361 case NFS4_OPEN_CLAIM_PREVIOUS:
1362 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1363 break;
1364 default:
1365 return 0;
1366 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001367 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 return 1;
1369}
1370
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001372{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001373 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001374 case FMODE_WRITE:
1375 state->n_wronly++;
1376 break;
1377 case FMODE_READ:
1378 state->n_rdonly++;
1379 break;
1380 case FMODE_READ|FMODE_WRITE:
1381 state->n_rdwr++;
1382 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001384}
1385
Trond Myklebust4f14c192014-02-12 19:15:06 -05001386static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001387{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001388 struct nfs_client *clp = state->owner->so_server->nfs_client;
1389 bool need_recover = false;
1390
1391 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1392 need_recover = true;
1393 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1394 need_recover = true;
1395 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1396 need_recover = true;
1397 if (need_recover)
1398 nfs4_state_mark_reclaim_nograce(clp, state);
1399}
1400
Trond Myklebuste999e802014-02-10 18:20:47 -05001401static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1402 nfs4_stateid *stateid)
1403{
1404 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1405 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001406 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1407 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001408 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001409 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001410 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1411 return true;
1412 return false;
1413}
1414
Trond Myklebustf95549c2015-01-23 18:06:09 -05001415static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1416{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001417 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1418 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001419 if (state->n_wronly)
1420 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1421 if (state->n_rdonly)
1422 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1423 if (state->n_rdwr)
1424 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001425 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001426}
1427
Trond Myklebust226056c2014-02-11 10:41:07 -05001428static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001429 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001430 nfs4_stateid *stateid, fmode_t fmode)
1431{
1432 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1433 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1434 case FMODE_WRITE:
1435 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1436 break;
1437 case FMODE_READ:
1438 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1439 break;
1440 case 0:
1441 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1442 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1443 clear_bit(NFS_OPEN_STATE, &state->flags);
1444 }
1445 if (stateid == NULL)
1446 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001448 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1449 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1450 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001452 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001453 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001454 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001455 nfs4_stateid_copy(&state->stateid, stateid);
1456 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001457}
1458
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001459static void nfs_clear_open_stateid(struct nfs4_state *state,
1460 nfs4_stateid *arg_stateid,
1461 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001462{
1463 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001464 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001465 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001466 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1467 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001468}
1469
Trond Myklebust003707c2007-07-05 18:07:55 -04001470static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1471{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001472 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001473 case FMODE_READ:
1474 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1475 break;
1476 case FMODE_WRITE:
1477 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 break;
1479 case FMODE_READ|FMODE_WRITE:
1480 set_bit(NFS_O_RDWR_STATE, &state->flags);
1481 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001482 if (!nfs_need_update_open_stateid(state, stateid))
1483 return;
1484 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1485 nfs4_stateid_copy(&state->stateid, stateid);
1486 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001487}
1488
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001489static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001491 /*
1492 * Protect the call to nfs4_state_set_mode_locked and
1493 * serialise the stateid update
1494 */
Andrew Elble361cad32015-12-02 09:20:57 -05001495 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001496 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001497 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001498 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001499 set_bit(NFS_DELEGATED_STATE, &state->flags);
1500 }
1501 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001503 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001505 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001508static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001509{
1510 struct nfs_inode *nfsi = NFS_I(state->inode);
1511 struct nfs_delegation *deleg_cur;
1512 int ret = 0;
1513
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001514 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001515
1516 rcu_read_lock();
1517 deleg_cur = rcu_dereference(nfsi->delegation);
1518 if (deleg_cur == NULL)
1519 goto no_delegation;
1520
1521 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001522 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001523 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001524 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001525 goto no_delegation_unlock;
1526
1527 if (delegation == NULL)
1528 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001529 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001530 goto no_delegation_unlock;
1531
Trond Myklebustb7391f42008-12-23 15:21:52 -05001532 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001533 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001534 ret = 1;
1535no_delegation_unlock:
1536 spin_unlock(&deleg_cur->lock);
1537no_delegation:
1538 rcu_read_unlock();
1539
1540 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001542 ret = 1;
1543 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001544 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1545 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001546
1547 return ret;
1548}
1549
Trond Myklebust39071e62015-01-24 15:07:56 -05001550static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1551 const nfs4_stateid *stateid)
1552{
1553 struct nfs4_state *state = lsp->ls_state;
1554 bool ret = false;
1555
1556 spin_lock(&state->state_lock);
1557 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1558 goto out_noupdate;
1559 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1560 goto out_noupdate;
1561 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1562 ret = true;
1563out_noupdate:
1564 spin_unlock(&state->state_lock);
1565 return ret;
1566}
Trond Myklebust34310432008-12-23 15:21:38 -05001567
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001568static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001569{
1570 struct nfs_delegation *delegation;
1571
1572 rcu_read_lock();
1573 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001574 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 rcu_read_unlock();
1576 return;
1577 }
1578 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001579 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001580}
1581
Trond Myklebust6ee41262007-07-08 14:11:36 -04001582static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001583{
1584 struct nfs4_state *state = opendata->state;
1585 struct nfs_inode *nfsi = NFS_I(state->inode);
1586 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001587 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001588 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001589 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590 nfs4_stateid stateid;
1591 int ret = -EAGAIN;
1592
Trond Myklebustaac00a82007-07-05 19:02:21 -04001593 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001594 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001595 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001596 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001597 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001598 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001599 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001600 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001601 rcu_read_lock();
1602 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001603 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001604 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001605 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001606 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001607 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001608 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001609 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001610 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001611 if (!opendata->is_recover) {
1612 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1613 if (ret != 0)
1614 goto out;
1615 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001617
1618 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001619 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001620 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001622out:
1623 return ERR_PTR(ret);
1624out_return_state:
1625 atomic_inc(&state->count);
1626 return state;
1627}
1628
Andy Adamsone23008e2012-10-02 21:07:32 -04001629static void
1630nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1631{
1632 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1633 struct nfs_delegation *delegation;
1634 int delegation_flags = 0;
1635
1636 rcu_read_lock();
1637 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1638 if (delegation)
1639 delegation_flags = delegation->flags;
1640 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001641 switch (data->o_arg.claim) {
1642 default:
1643 break;
1644 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1645 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001646 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1647 "returning a delegation for "
1648 "OPEN(CLAIM_DELEGATE_CUR)\n",
1649 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001650 return;
1651 }
1652 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001653 nfs_inode_set_delegation(state->inode,
1654 data->owner->so_cred,
1655 &data->o_res);
1656 else
1657 nfs_inode_reclaim_delegation(state->inode,
1658 data->owner->so_cred,
1659 &data->o_res);
1660}
1661
1662/*
1663 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1664 * and update the nfs4_state.
1665 */
1666static struct nfs4_state *
1667_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1668{
1669 struct inode *inode = data->state->inode;
1670 struct nfs4_state *state = data->state;
1671 int ret;
1672
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001673 if (!data->rpc_done) {
1674 if (data->rpc_status) {
1675 ret = data->rpc_status;
1676 goto err;
1677 }
1678 /* cached opens have already been processed */
1679 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001680 }
1681
Andy Adamsone23008e2012-10-02 21:07:32 -04001682 ret = nfs_refresh_inode(inode, &data->f_attr);
1683 if (ret)
1684 goto err;
1685
1686 if (data->o_res.delegation_type != 0)
1687 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001688update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001689 update_open_stateid(state, &data->o_res.stateid, NULL,
1690 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001691 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001692
1693 return state;
1694err:
1695 return ERR_PTR(ret);
1696
1697}
1698
1699static struct nfs4_state *
1700_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701{
1702 struct inode *inode;
1703 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001704 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001707 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001708 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709 goto out;
1710 }
1711
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001712 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001713 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001714 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001715 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001716 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001717 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001718 goto err;
1719 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001720 state = nfs4_get_open_state(inode, data->owner);
1721 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001722 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 if (data->o_res.delegation_type != 0)
1724 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001725 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001726 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001727 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001728out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001729 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001730 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001731err_put_inode:
1732 iput(inode);
1733err:
1734 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001735}
1736
Andy Adamsone23008e2012-10-02 21:07:32 -04001737static struct nfs4_state *
1738nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1739{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001740 struct nfs4_state *ret;
1741
Andy Adamsone23008e2012-10-02 21:07:32 -04001742 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001743 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1744 else
1745 ret = _nfs4_opendata_to_nfs4_state(data);
1746 nfs4_sequence_free_slot(&data->o_res.seq_res);
1747 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001748}
1749
Trond Myklebust864472e2006-01-03 09:55:15 +01001750static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1751{
1752 struct nfs_inode *nfsi = NFS_I(state->inode);
1753 struct nfs_open_context *ctx;
1754
1755 spin_lock(&state->inode->i_lock);
1756 list_for_each_entry(ctx, &nfsi->open_files, list) {
1757 if (ctx->state != state)
1758 continue;
1759 get_nfs_open_context(ctx);
1760 spin_unlock(&state->inode->i_lock);
1761 return ctx;
1762 }
1763 spin_unlock(&state->inode->i_lock);
1764 return ERR_PTR(-ENOENT);
1765}
1766
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001767static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1768 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001769{
1770 struct nfs4_opendata *opendata;
1771
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001772 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001773 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001774 if (opendata == NULL)
1775 return ERR_PTR(-ENOMEM);
1776 opendata->state = state;
1777 atomic_inc(&state->count);
1778 return opendata;
1779}
1780
Trond Myklebust24311f82015-09-20 10:50:17 -04001781static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1782 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001783{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001784 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001785 int ret;
1786
Trond Myklebust24311f82015-09-20 10:50:17 -04001787 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001788 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001789 opendata->o_arg.open_flags = 0;
1790 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001791 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1792 NFS_SB(opendata->dentry->d_sb),
1793 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001794 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1795 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1796 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001797 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001798 if (ret != 0)
1799 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001800 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001801 if (IS_ERR(newstate))
1802 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001803 if (newstate != opendata->state)
1804 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001805 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001806 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001807}
1808
1809static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1810{
Trond Myklebust864472e2006-01-03 09:55:15 +01001811 int ret;
1812
Trond Myklebust4f14c192014-02-12 19:15:06 -05001813 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1814 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1815 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1816 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001817 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001818 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001819 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001820 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001821 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1822 if (ret != 0)
1823 return ret;
1824 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1825 if (ret != 0)
1826 return ret;
1827 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1828 if (ret != 0)
1829 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001830 /*
1831 * We may have performed cached opens for all three recoveries.
1832 * Check if we need to update the current stateid.
1833 */
1834 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001835 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001836 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001837 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001838 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001839 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001840 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001841 return 0;
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/*
1845 * OPEN_RECLAIM:
1846 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001848static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001850 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001851 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001852 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int status;
1854
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001855 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1856 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001857 if (IS_ERR(opendata))
1858 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001859 rcu_read_lock();
1860 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001861 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001862 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001863 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 opendata->o_arg.u.delegation_type = delegation_type;
1865 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001866 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return status;
1868}
1869
Trond Myklebust539cd032007-06-05 11:46:42 -04001870static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct nfs_server *server = NFS_SERVER(state->inode);
1873 struct nfs4_exception exception = { };
1874 int err;
1875 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001876 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001877 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001878 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1879 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001880 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001881 break;
1882 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 } while (exception.retry);
1884 return err;
1885}
1886
Trond Myklebust864472e2006-01-03 09:55:15 +01001887static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1888{
1889 struct nfs_open_context *ctx;
1890 int ret;
1891
1892 ctx = nfs4_state_find_open_context(state);
1893 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001894 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001895 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001896 put_nfs_open_context(ctx);
1897 return ret;
1898}
1899
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001900static 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 -07001901{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001902 switch (err) {
1903 default:
1904 printk(KERN_ERR "NFS: %s: unhandled error "
1905 "%d.\n", __func__, err);
1906 case 0:
1907 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001908 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001909 case -ESTALE:
1910 break;
1911 case -NFS4ERR_BADSESSION:
1912 case -NFS4ERR_BADSLOT:
1913 case -NFS4ERR_BAD_HIGH_SLOT:
1914 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1915 case -NFS4ERR_DEADSESSION:
1916 set_bit(NFS_DELEGATED_STATE, &state->flags);
1917 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1918 return -EAGAIN;
1919 case -NFS4ERR_STALE_CLIENTID:
1920 case -NFS4ERR_STALE_STATEID:
1921 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001922 /* Don't recall a delegation if it was lost */
1923 nfs4_schedule_lease_recovery(server->nfs_client);
1924 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001925 case -NFS4ERR_MOVED:
1926 nfs4_schedule_migration_recovery(server);
1927 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001928 case -NFS4ERR_LEASE_MOVED:
1929 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1930 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001931 case -NFS4ERR_DELEG_REVOKED:
1932 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001933 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001934 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001935 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001936 nfs_inode_find_state_and_recover(state->inode,
1937 stateid);
1938 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001939 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001940 case -NFS4ERR_DELAY:
1941 case -NFS4ERR_GRACE:
1942 set_bit(NFS_DELEGATED_STATE, &state->flags);
1943 ssleep(1);
1944 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001945 case -ENOMEM:
1946 case -NFS4ERR_DENIED:
1947 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1948 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return err;
1951}
1952
Trond Myklebust24311f82015-09-20 10:50:17 -04001953int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1954 struct nfs4_state *state, const nfs4_stateid *stateid,
1955 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001956{
1957 struct nfs_server *server = NFS_SERVER(state->inode);
1958 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001959 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001960
1961 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1962 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1963 if (IS_ERR(opendata))
1964 return PTR_ERR(opendata);
1965 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001966 write_seqlock(&state->seqlock);
1967 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1968 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001969 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1970 switch (type & (FMODE_READ|FMODE_WRITE)) {
1971 case FMODE_READ|FMODE_WRITE:
1972 case FMODE_WRITE:
1973 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1974 if (err)
1975 break;
1976 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1977 if (err)
1978 break;
1979 case FMODE_READ:
1980 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1981 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001982 nfs4_opendata_put(opendata);
1983 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1984}
1985
Chuck Leverbe05c862013-08-09 12:49:47 -04001986static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1987{
1988 struct nfs4_opendata *data = calldata;
1989
Peng Taocb04ad22014-06-11 05:24:15 +08001990 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1991 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001992}
1993
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001994static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1995{
1996 struct nfs4_opendata *data = calldata;
1997
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001998 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001999
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002000 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002001 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002002 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002003 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002004 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002005 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002006 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002007}
2008
2009static void nfs4_open_confirm_release(void *calldata)
2010{
2011 struct nfs4_opendata *data = calldata;
2012 struct nfs4_state *state = NULL;
2013
2014 /* If this request hasn't been cancelled, do nothing */
2015 if (data->cancelled == 0)
2016 goto out_free;
2017 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002018 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002019 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002020 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002021 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002022 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002023out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002024 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002025}
2026
2027static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002028 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002029 .rpc_call_done = nfs4_open_confirm_done,
2030 .rpc_release = nfs4_open_confirm_release,
2031};
2032
2033/*
2034 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2035 */
2036static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2037{
David Howells2b0143b2015-03-17 22:25:59 +00002038 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002039 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002040 struct rpc_message msg = {
2041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2042 .rpc_argp = &data->c_arg,
2043 .rpc_resp = &data->c_res,
2044 .rpc_cred = data->owner->so_cred,
2045 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002046 struct rpc_task_setup task_setup_data = {
2047 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002048 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002049 .callback_ops = &nfs4_open_confirm_ops,
2050 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002051 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002052 .flags = RPC_TASK_ASYNC,
2053 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int status;
2055
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002056 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002057 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002058 data->rpc_done = 0;
2059 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002060 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002061 if (data->is_recover)
2062 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002063 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002064 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002065 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002066 status = nfs4_wait_for_completion_rpc_task(task);
2067 if (status != 0) {
2068 data->cancelled = 1;
2069 smp_wmb();
2070 } else
2071 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002072 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return status;
2074}
2075
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002076static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002078 struct nfs4_opendata *data = calldata;
2079 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002080 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002081 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002082
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002083 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002084 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002085 /*
2086 * Check if we still need to send an OPEN call, or if we can use
2087 * a delegation instead.
2088 */
2089 if (data->state != NULL) {
2090 struct nfs_delegation *delegation;
2091
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002092 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002093 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002094 rcu_read_lock();
2095 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002096 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002097 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002098 rcu_read_unlock();
2099 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002100 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002101 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002102 switch (claim) {
2103 default:
2104 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002105 case NFS4_OPEN_CLAIM_PREVIOUS:
2106 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2107 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002108 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002109 case NFS4_OPEN_CLAIM_FH:
2110 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002111 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2112 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002114 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002115 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002116 &data->o_res.seq_res,
2117 task) != 0)
2118 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002119
2120 /* Set the create mode (note dependency on the session type) */
2121 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2122 if (data->o_arg.open_flags & O_EXCL) {
2123 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2124 if (nfs4_has_persistent_session(clp))
2125 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2126 else if (clp->cl_mvops->minor_version > 0)
2127 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2128 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002129 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002130unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002131 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002132 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002133out_no_action:
2134 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002135out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002136 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002137}
2138
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002139static void nfs4_open_done(struct rpc_task *task, void *calldata)
2140{
2141 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002144
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002145 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002146 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002147
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002148 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002149 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2150 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002151 case S_IFREG:
2152 break;
2153 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002154 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002155 break;
2156 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002158 break;
2159 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002161 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002162 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002164 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2165 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002166 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002167 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002168}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002169
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002170static void nfs4_open_release(void *calldata)
2171{
2172 struct nfs4_opendata *data = calldata;
2173 struct nfs4_state *state = NULL;
2174
2175 /* If this request hasn't been cancelled, do nothing */
2176 if (data->cancelled == 0)
2177 goto out_free;
2178 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002179 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002180 goto out_free;
2181 /* In case we need an open_confirm, no cleanup! */
2182 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2183 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002184 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002185 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002186 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002188 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189}
2190
2191static const struct rpc_call_ops nfs4_open_ops = {
2192 .rpc_call_prepare = nfs4_open_prepare,
2193 .rpc_call_done = nfs4_open_done,
2194 .rpc_release = nfs4_open_release,
2195};
2196
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002197static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198{
David Howells2b0143b2015-03-17 22:25:59 +00002199 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200 struct nfs_server *server = NFS_SERVER(dir);
2201 struct nfs_openargs *o_arg = &data->o_arg;
2202 struct nfs_openres *o_res = &data->o_res;
2203 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002204 struct rpc_message msg = {
2205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2206 .rpc_argp = o_arg,
2207 .rpc_resp = o_res,
2208 .rpc_cred = data->owner->so_cred,
2209 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002210 struct rpc_task_setup task_setup_data = {
2211 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002212 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002213 .callback_ops = &nfs4_open_ops,
2214 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002215 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002216 .flags = RPC_TASK_ASYNC,
2217 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218 int status;
2219
Chuck Levera9c92d62013-08-09 12:48:18 -04002220 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002221 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002222 data->rpc_done = 0;
2223 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002224 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002225 data->is_recover = 0;
2226 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002227 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002228 data->is_recover = 1;
2229 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002230 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002231 if (IS_ERR(task))
2232 return PTR_ERR(task);
2233 status = nfs4_wait_for_completion_rpc_task(task);
2234 if (status != 0) {
2235 data->cancelled = 1;
2236 smp_wmb();
2237 } else
2238 status = data->rpc_status;
2239 rpc_put_task(task);
2240
2241 return status;
2242}
2243
2244static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2245{
David Howells2b0143b2015-03-17 22:25:59 +00002246 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002247 struct nfs_openres *o_res = &data->o_res;
2248 int status;
2249
2250 status = nfs4_run_open_task(data, 1);
2251 if (status != 0 || !data->rpc_done)
2252 return status;
2253
Trond Myklebust6926afd2012-01-07 13:22:46 -05002254 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2255
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002256 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2257 status = _nfs4_proc_open_confirm(data);
2258 if (status != 0)
2259 return status;
2260 }
2261
2262 return status;
2263}
2264
Trond Myklebustf3792d62014-07-10 08:54:32 -04002265/*
2266 * Additional permission checks in order to distinguish between an
2267 * open for read, and an open for execute. This works around the
2268 * fact that NFSv4 OPEN treats read and execute permissions as being
2269 * the same.
2270 * Note that in the non-execute case, we want to turn off permission
2271 * checking if we just created a new file (POSIX open() semantics).
2272 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002273static int nfs4_opendata_access(struct rpc_cred *cred,
2274 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002275 struct nfs4_state *state, fmode_t fmode,
2276 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002277{
2278 struct nfs_access_entry cache;
2279 u32 mask;
2280
2281 /* access call failed or for some reason the server doesn't
2282 * support any access modes -- defer access call until later */
2283 if (opendata->o_res.access_supported == 0)
2284 return 0;
2285
2286 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002287 /*
2288 * Use openflags to check for exec, because fmode won't
2289 * always have FMODE_EXEC set when file open for exec.
2290 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002291 if (openflags & __FMODE_EXEC) {
2292 /* ONLY check for exec rights */
2293 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002294 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002295 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002296
2297 cache.cred = cred;
2298 cache.jiffies = jiffies;
2299 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2300 nfs_access_add_cache(state->inode, &cache);
2301
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002302 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002303 return 0;
2304
2305 /* even though OPEN succeeded, access is denied. Close the file */
2306 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002307 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002308}
2309
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002310/*
2311 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2312 */
2313static int _nfs4_proc_open(struct nfs4_opendata *data)
2314{
David Howells2b0143b2015-03-17 22:25:59 +00002315 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002316 struct nfs_server *server = NFS_SERVER(dir);
2317 struct nfs_openargs *o_arg = &data->o_arg;
2318 struct nfs_openres *o_res = &data->o_res;
2319 int status;
2320
2321 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002322 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002323 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002324 if (status != 0) {
2325 if (status == -NFS4ERR_BADNAME &&
2326 !(o_arg->open_flags & O_CREAT))
2327 return -ENOENT;
2328 return status;
2329 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002330
Trond Myklebust6926afd2012-01-07 13:22:46 -05002331 nfs_fattr_map_and_free_names(server, &data->f_attr);
2332
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002333 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002334 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002335 if (o_arg->open_flags & O_EXCL)
2336 data->file_created = 1;
2337 else if (o_res->cinfo.before != o_res->cinfo.after)
2338 data->file_created = 1;
2339 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002340 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2341 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002343 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002345 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002348 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
Andy Adamsond83217c2011-03-01 01:34:17 +00002352static int nfs4_recover_expired_lease(struct nfs_server *server)
2353{
2354 return nfs4_client_recover_expired_lease(server->nfs_client);
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357/*
2358 * OPEN_EXPIRED:
2359 * reclaim state on the server after a network partition.
2360 * Assumes caller holds the appropriate lock
2361 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002362static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002364 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002365 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002367 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002368 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002369 if (IS_ERR(opendata))
2370 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002371 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002372 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002373 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002374 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002375 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002378static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002379{
Trond Myklebust539cd032007-06-05 11:46:42 -04002380 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002381 struct nfs4_exception exception = { };
2382 int err;
2383
2384 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002385 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002386 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002387 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2388 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002389 switch (err) {
2390 default:
2391 goto out;
2392 case -NFS4ERR_GRACE:
2393 case -NFS4ERR_DELAY:
2394 nfs4_handle_exception(server, err, &exception);
2395 err = 0;
2396 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002397 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002398out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002399 return err;
2400}
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002405 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Trond Myklebust864472e2006-01-03 09:55:15 +01002407 ctx = nfs4_state_find_open_context(state);
2408 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002409 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002410 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002411 put_nfs_open_context(ctx);
2412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Trond Myklebust41020b62016-09-22 13:38:58 -04002415static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2416 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002417{
Trond Myklebust41020b62016-09-22 13:38:58 -04002418 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002419 write_seqlock(&state->seqlock);
2420 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2421 write_sequnlock(&state->seqlock);
2422 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2423}
2424
2425static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2426{
2427 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002428 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002429}
2430
2431static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2432{
2433 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2434 nfs40_clear_delegation_stateid(state);
2435 return nfs4_open_expired(sp, state);
2436}
2437
Trond Myklebust45870d62016-09-22 13:38:59 -04002438static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2439 nfs4_stateid *stateid,
2440 struct rpc_cred *cred)
2441{
2442 return -NFS4ERR_BAD_STATEID;
2443}
2444
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002445#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002446static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2447 nfs4_stateid *stateid,
2448 struct rpc_cred *cred)
2449{
2450 int status;
2451
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002452 switch (stateid->type) {
2453 default:
2454 break;
2455 case NFS4_INVALID_STATEID_TYPE:
2456 case NFS4_SPECIAL_STATEID_TYPE:
2457 return -NFS4ERR_BAD_STATEID;
2458 case NFS4_REVOKED_STATEID_TYPE:
2459 goto out_free;
2460 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002461
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002462 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002463 switch (status) {
2464 case -NFS4ERR_EXPIRED:
2465 case -NFS4ERR_ADMIN_REVOKED:
2466 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002467 break;
2468 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002469 return status;
2470 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002471out_free:
2472 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002473 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002474 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002475}
2476
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002477static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002478{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002479 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002480 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002481 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002482 struct rpc_cred *cred;
2483 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002484
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002485 /* Get the delegation credential for use by test/free_stateid */
2486 rcu_read_lock();
2487 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002488 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002489 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002490 return;
2491 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002492
2493 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002494 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2495 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002496 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002497 return;
2498 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002499
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002500 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2501 rcu_read_unlock();
2502 return;
2503 }
2504
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002505 cred = get_rpccred(delegation->cred);
2506 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002507 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002508 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002509 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002510 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002511
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002512 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002513}
2514
2515/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002516 * nfs41_check_expired_locks - possibly free a lock stateid
2517 *
2518 * @state: NFSv4 state for an inode
2519 *
2520 * Returns NFS_OK if recovery for this stateid is now finished.
2521 * Otherwise a negative NFS4ERR value is returned.
2522 */
2523static int nfs41_check_expired_locks(struct nfs4_state *state)
2524{
2525 int status, ret = NFS_OK;
2526 struct nfs4_lock_state *lsp;
2527 struct nfs_server *server = NFS_SERVER(state->inode);
2528
2529 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2530 goto out;
2531 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2532 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2533 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2534
2535 status = nfs41_test_and_free_expired_stateid(server,
2536 &lsp->ls_stateid,
2537 cred);
2538 trace_nfs4_test_lock_stateid(state, lsp, status);
2539 if (status == -NFS4ERR_EXPIRED ||
2540 status == -NFS4ERR_BAD_STATEID) {
2541 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2542 if (!recover_lost_locks)
2543 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2544 } else if (status != NFS_OK) {
2545 ret = status;
2546 break;
2547 }
2548 }
2549 };
2550out:
2551 return ret;
2552}
2553
2554/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002555 * nfs41_check_open_stateid - possibly free an open stateid
2556 *
2557 * @state: NFSv4 state for an inode
2558 *
2559 * Returns NFS_OK if recovery for this stateid is now finished.
2560 * Otherwise a negative NFS4ERR value is returned.
2561 */
2562static int nfs41_check_open_stateid(struct nfs4_state *state)
2563{
2564 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002565 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002566 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002567 int status;
2568
2569 /* If a state reset has been done, test_stateid is unneeded */
2570 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2571 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2572 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2573 return -NFS4ERR_BAD_STATEID;
2574
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002575 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002576 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002577 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002578 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2579 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2580 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002581 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002582 }
2583 return status;
2584}
2585
2586static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2587{
Chuck Levereb64cf92012-07-11 16:30:05 -04002588 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002589
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002590 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002591 status = nfs41_check_expired_locks(state);
2592 if (status != NFS_OK)
2593 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002594 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002595 if (status != NFS_OK)
2596 status = nfs4_open_expired(sp, state);
2597 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002598}
2599#endif
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002602 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2603 * fields corresponding to attributes that were used to store the verifier.
2604 * Make sure we clobber those fields in the later setattr call
2605 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002606static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2607 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002608{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002609 const u32 *attrset = opendata->o_res.attrset;
2610
2611 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002612 !(sattr->ia_valid & ATTR_ATIME_SET))
2613 sattr->ia_valid |= ATTR_ATIME;
2614
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002615 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002616 !(sattr->ia_valid & ATTR_MTIME_SET))
2617 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002618
2619 /* Except MODE, it seems harmless of setting twice. */
2620 if ((attrset[1] & FATTR4_WORD1_MODE))
2621 sattr->ia_valid &= ~ATTR_MODE;
2622
2623 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2624 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002625}
2626
Trond Myklebustc21443c2013-02-07 14:26:21 -05002627static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2628 fmode_t fmode,
2629 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002630 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002631{
2632 struct nfs4_state_owner *sp = opendata->owner;
2633 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002634 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002635 struct nfs4_state *state;
2636 unsigned int seq;
2637 int ret;
2638
2639 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2640
2641 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002642 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002643 goto out;
2644
2645 state = nfs4_opendata_to_nfs4_state(opendata);
2646 ret = PTR_ERR(state);
2647 if (IS_ERR(state))
2648 goto out;
2649 if (server->caps & NFS_CAP_POSIX_LOCK)
2650 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002651 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2652 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002653
Trond Myklebust275bb302013-05-29 13:11:28 -04002654 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002655 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002656 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002657 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002658 alias = d_exact_alias(dentry, state->inode);
2659 if (!alias)
2660 alias = d_splice_alias(igrab(state->inode), dentry);
2661 /* d_splice_alias() can't fail here - it's a non-directory */
2662 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002663 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002664 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002665 }
2666 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002667 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002668 }
2669
Trond Myklebustc21443c2013-02-07 14:26:21 -05002670 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2671 if (ret != 0)
2672 goto out;
2673
Trond Myklebust3efb9722013-05-29 13:17:04 -04002674 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002675 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002676 nfs_inode_attach_open_context(ctx);
2677 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2678 nfs4_schedule_stateid_recovery(server, state);
2679 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002680out:
2681 return ret;
2682}
2683
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002684/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002685 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 */
Andy Adamson82be4172012-05-23 05:02:35 -04002687static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002688 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002689 int flags,
2690 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002691 struct nfs4_label *label,
2692 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 struct nfs4_state_owner *sp;
2695 struct nfs4_state *state = NULL;
2696 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002697 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002698 struct dentry *dentry = ctx->dentry;
2699 struct rpc_cred *cred = ctx->cred;
2700 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2701 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002702 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002703 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002704 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002708 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2709 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2711 goto out_err;
2712 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002713 status = nfs4_recover_expired_lease(server);
2714 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002715 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002716 if (d_really_is_positive(dentry))
2717 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002718 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002719 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002720 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002721 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002722 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002723 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002724 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
David Quigley14c43f72013-05-22 12:50:43 -04002726 if (label) {
2727 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2728 if (IS_ERR(olabel)) {
2729 status = PTR_ERR(olabel);
2730 goto err_opendata_put;
2731 }
2732 }
2733
Trond Myklebuste911b812014-03-26 13:24:37 -07002734 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2735 if (!opendata->f_attr.mdsthreshold) {
2736 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2737 if (!opendata->f_attr.mdsthreshold)
2738 goto err_free_label;
2739 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002740 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002741 }
David Howells2b0143b2015-03-17 22:25:59 +00002742 if (d_really_is_positive(dentry))
2743 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002744
Trond Myklebust3efb9722013-05-29 13:17:04 -04002745 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002746 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002747 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002748 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002749
NeilBrownefcbc042015-07-30 13:00:56 +10002750 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002751 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002753 /*
2754 * send create attributes which was not set by open
2755 * with an extra setattr.
2756 */
2757 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2758 nfs_fattr_init(opendata->o_res.f_attr);
2759 status = nfs4_do_setattr(state->inode, cred,
2760 opendata->o_res.f_attr, sattr,
2761 state, label, olabel);
2762 if (status == 0) {
2763 nfs_setattr_update_inode(state->inode, sattr,
2764 opendata->o_res.f_attr);
2765 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2766 }
David Quigley1775fd32013-05-22 12:50:42 -04002767 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002768 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002769 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002770 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002771
Trond Myklebuste911b812014-03-26 13:24:37 -07002772 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002773 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002774 opendata->f_attr.mdsthreshold = NULL;
2775 }
Andy Adamson82be4172012-05-23 05:02:35 -04002776
David Quigley14c43f72013-05-22 12:50:43 -04002777 nfs4_label_free(olabel);
2778
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002779 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002782err_free_label:
2783 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002784err_opendata_put:
2785 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002786err_put_state_owner:
2787 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return status;
2790}
2791
2792
Andy Adamson82be4172012-05-23 05:02:35 -04002793static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002794 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002795 int flags,
2796 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002797 struct nfs4_label *label,
2798 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002800 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 struct nfs4_exception exception = { };
2802 struct nfs4_state *res;
2803 int status;
2804
2805 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002806 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002807 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002808 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (status == 0)
2810 break;
2811 /* NOTE: BAD_SEQID means the server and client disagree about the
2812 * book-keeping w.r.t. state-changing operations
2813 * (OPEN/CLOSE/LOCK/LOCKU...)
2814 * It is actually a sign of a bug on the client or on the server.
2815 *
2816 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002817 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 * have unhashed the old state_owner for us, and that we can
2819 * therefore safely retry using a new one. We should still warn
2820 * the user though...
2821 */
2822 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002823 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002824 " returned a bad sequence-id error!\n",
2825 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 exception.retry = 1;
2827 continue;
2828 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002829 /*
2830 * BAD_STATEID on OPEN means that the server cancelled our
2831 * state before it received the OPEN_CONFIRM.
2832 * Recover by retrying the request as per the discussion
2833 * on Page 181 of RFC3530.
2834 */
2835 if (status == -NFS4ERR_BAD_STATEID) {
2836 exception.retry = 1;
2837 continue;
2838 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002839 if (status == -EAGAIN) {
2840 /* We must have found a delegation */
2841 exception.retry = 1;
2842 continue;
2843 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002844 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2845 continue;
2846 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 status, &exception));
2848 } while (exception.retry);
2849 return res;
2850}
2851
Trond Myklebust8487c472016-06-26 08:44:35 -04002852static int _nfs4_do_setattr(struct inode *inode,
2853 struct nfs_setattrargs *arg,
2854 struct nfs_setattrres *res,
2855 struct rpc_cred *cred,
2856 struct nfs4_state *state)
2857{
2858 struct nfs_server *server = NFS_SERVER(inode);
2859 struct rpc_message msg = {
2860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2861 .rpc_argp = arg,
2862 .rpc_resp = res,
2863 .rpc_cred = cred,
2864 };
2865 struct rpc_cred *delegation_cred = NULL;
2866 unsigned long timestamp = jiffies;
2867 fmode_t fmode;
2868 bool truncate;
2869 int status;
2870
2871 nfs_fattr_init(res->fattr);
2872
2873 /* Servers should only apply open mode checks for file size changes */
2874 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2875 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2876
2877 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2878 /* Use that stateid */
2879 } else if (truncate && state != NULL) {
2880 struct nfs_lockowner lockowner = {
2881 .l_owner = current->files,
2882 .l_pid = current->tgid,
2883 };
2884 if (!nfs4_valid_open_stateid(state))
2885 return -EBADF;
2886 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2887 &arg->stateid, &delegation_cred) == -EIO)
2888 return -EBADF;
2889 } else
2890 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2891 if (delegation_cred)
2892 msg.rpc_cred = delegation_cred;
2893
2894 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2895
2896 put_rpccred(delegation_cred);
2897 if (status == 0 && state != NULL)
2898 renew_lease(server, timestamp);
2899 trace_nfs4_setattr(inode, &arg->stateid, status);
2900 return status;
2901}
2902
2903static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2904 struct nfs_fattr *fattr, struct iattr *sattr,
2905 struct nfs4_state *state, struct nfs4_label *ilabel,
2906 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002908 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002910 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 .iap = sattr,
2912 .server = server,
2913 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002914 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 };
2916 struct nfs_setattrres res = {
2917 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002918 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 .server = server,
2920 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002921 struct nfs4_exception exception = {
2922 .state = state,
2923 .inode = inode,
2924 .stateid = &arg.stateid,
2925 };
2926 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
David Quigleyaa9c2662013-05-22 12:50:44 -04002928 arg.bitmask = nfs4_bitmask(server, ilabel);
2929 if (ilabel)
2930 arg.bitmask = nfs4_bitmask(server, olabel);
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002933 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002934 switch (err) {
2935 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002936 if (!(sattr->ia_valid & ATTR_SIZE)) {
2937 pr_warn_once("NFSv4: server %s is incorrectly "
2938 "applying open mode checks to "
2939 "a SETATTR that is not "
2940 "changing file size.\n",
2941 server->nfs_client->cl_hostname);
2942 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002943 if (state && !(state->state & FMODE_WRITE)) {
2944 err = -EBADF;
2945 if (sattr->ia_valid & ATTR_OPEN)
2946 err = -EACCES;
2947 goto out;
2948 }
2949 }
2950 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002952out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return err;
2954}
2955
Peng Tao500d7012015-09-22 11:35:22 +08002956static bool
2957nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2958{
2959 if (inode == NULL || !nfs_have_layout(inode))
2960 return false;
2961
2962 return pnfs_wait_on_layoutreturn(inode, task);
2963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965struct nfs4_closedata {
2966 struct inode *inode;
2967 struct nfs4_state *state;
2968 struct nfs_closeargs arg;
2969 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002970 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002971 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002972 bool roc;
2973 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974};
2975
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002976static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002977{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002978 struct nfs4_closedata *calldata = data;
2979 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002980 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002981
Fred Isamanf7e89172011-01-06 11:36:32 +00002982 if (calldata->roc)
2983 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002984 nfs4_put_open_state(calldata->state);
2985 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002986 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002987 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002988 kfree(calldata);
2989}
2990
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002991static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002993 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002996 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Chuck Levera3ca5652012-03-01 17:00:40 -05002998 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002999 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3000 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003001 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 /* hmm. we are done with the inode, and in the process of freeing
3003 * the state_owner. we keep this around to process errors
3004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 switch (task->tk_status) {
3006 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003007 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003008 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003009 pnfs_roc_set_barrier(state->inode,
3010 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003011 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003012 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003013 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003015 case -NFS4ERR_EXPIRED:
3016 nfs4_free_revoked_stateid(server,
3017 &calldata->arg.stateid,
3018 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003019 case -NFS4ERR_OLD_STATEID:
3020 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003021 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003022 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003023 rpc_restart_call_prepare(task);
3024 goto out_release;
3025 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003026 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003029 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003030 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003031 goto out_release;
3032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003034 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3035 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003036out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003037 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003038 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003039 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003042static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003044 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003045 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003046 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003047 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003048 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003049
Chuck Levera3ca5652012-03-01 17:00:40 -05003050 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003051 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003052 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003053
Trond Myklebust88069f72009-12-08 08:33:16 -05003054 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003055 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003056 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3057 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3058 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003059 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003060 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003061 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003062 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003063 if (state->n_rdonly == 0)
3064 call_close |= is_rdonly;
3065 else if (is_rdonly)
3066 calldata->arg.fmode |= FMODE_READ;
3067 if (state->n_wronly == 0)
3068 call_close |= is_wronly;
3069 else if (is_wronly)
3070 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003071 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3072 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003073 } else if (is_rdwr)
3074 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3075
Trond Myklebust5d422302013-03-14 16:57:48 -04003076 if (!nfs4_valid_open_stateid(state))
3077 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003078 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003079
3080 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003081 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003082 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003083 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003084
Peng Tao500d7012015-09-22 11:35:22 +08003085 if (nfs4_wait_on_layoutreturn(inode, task)) {
3086 nfs_release_seqid(calldata->arg.seqid);
3087 goto out_wait;
3088 }
3089
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003090 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003091 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003092 if (calldata->roc)
3093 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3094
Trond Myklebust6ae37332015-01-30 14:21:14 -05003095 calldata->arg.share_access =
3096 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3097 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003098
Trond Myklebust516a6af2005-10-27 22:12:41 -04003099 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003100 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003101 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003102 &calldata->arg.seq_args,
3103 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003104 task) != 0)
3105 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003106 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003107 return;
3108out_no_action:
3109 task->tk_action = NULL;
3110out_wait:
3111 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003114static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003115 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003116 .rpc_call_done = nfs4_close_done,
3117 .rpc_release = nfs4_free_closedata,
3118};
3119
Peng Taofe08c542014-07-03 13:05:01 +08003120static bool nfs4_roc(struct inode *inode)
3121{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003122 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003123 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003124 return pnfs_roc(inode);
3125}
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127/*
3128 * It is possible for data to be read/written from a mem-mapped file
3129 * after the sys_close call (which hits the vfs layer as a flush).
3130 * This means that we can't safely call nfsv4 close on a file until
3131 * the inode is cleared. This in turn means that we are not good
3132 * NFSv4 citizens - we do not indicate to the server to update the file's
3133 * share state even when we are done with one of the three share
3134 * stateid's in the inode.
3135 *
3136 * NOTE: Caller must be holding the sp->so_owner semaphore!
3137 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003138int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003140 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003141 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003143 struct nfs4_state_owner *sp = state->owner;
3144 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003145 struct rpc_message msg = {
3146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3147 .rpc_cred = state->owner->so_cred,
3148 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003149 struct rpc_task_setup task_setup_data = {
3150 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003151 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003152 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003153 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003154 .flags = RPC_TASK_ASYNC,
3155 };
Trond Myklebust95121352005-10-18 14:20:12 -07003156 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003158 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3159 &task_setup_data.rpc_client, &msg);
3160
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003161 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003163 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003164 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003165 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003167 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003169 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3170 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003171 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003172 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003173 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003174 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003175 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003176 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003177 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003178 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003179 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003180
Trond Myklebust1174dd12010-12-21 10:52:24 -05003181 msg.rpc_argp = &calldata->arg;
3182 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003183 task_setup_data.callback_data = calldata;
3184 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003185 if (IS_ERR(task))
3186 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003187 status = 0;
3188 if (wait)
3189 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003190 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003191 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003192out_free_calldata:
3193 kfree(calldata);
3194out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003195 nfs4_put_open_state(state);
3196 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Trond Myklebust2b484292010-09-17 10:56:51 -04003200static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003201nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3202 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003205 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3206
3207 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Trond Myklebust565277f2007-10-15 18:17:53 -04003209 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003210 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003211
3212 nfs4_label_release_security(label);
3213
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003214 if (IS_ERR(state))
3215 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003216 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Trond Myklebust1185a552009-12-03 15:54:02 -05003219static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003220{
3221 if (ctx->state == NULL)
3222 return;
3223 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003224 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003225 else
Al Viro643168c2011-06-22 18:20:23 -04003226 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Trond Myklebustb944dba2013-11-04 15:20:20 -05003229#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3230#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003231#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3234{
Kinglong Mee8c612822015-08-26 21:12:58 +08003235 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003236 struct nfs4_server_caps_arg args = {
3237 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003238 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003239 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 struct nfs4_server_caps_res res = {};
3241 struct rpc_message msg = {
3242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003243 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 .rpc_resp = &res,
3245 };
3246 int status;
3247
Kinglong Mee8c612822015-08-26 21:12:58 +08003248 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3249 FATTR4_WORD0_FH_EXPIRE_TYPE |
3250 FATTR4_WORD0_LINK_SUPPORT |
3251 FATTR4_WORD0_SYMLINK_SUPPORT |
3252 FATTR4_WORD0_ACLSUPPORT;
3253 if (minorversion)
3254 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3255
Bryan Schumaker7c513052011-03-24 17:12:24 +00003256 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003258 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003259 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003260 case 0:
3261 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3262 res.attr_bitmask[2] = 0;
3263 break;
3264 case 1:
3265 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3266 break;
3267 case 2:
3268 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003271 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3272 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3273 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3274 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003275 NFS_CAP_CTIME|NFS_CAP_MTIME|
3276 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003277 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3278 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 server->caps |= NFS_CAP_ACLS;
3280 if (res.has_links != 0)
3281 server->caps |= NFS_CAP_HARDLINKS;
3282 if (res.has_symlinks != 0)
3283 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003284 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3285 server->caps |= NFS_CAP_FILEID;
3286 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3287 server->caps |= NFS_CAP_MODE;
3288 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3289 server->caps |= NFS_CAP_NLINK;
3290 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3291 server->caps |= NFS_CAP_OWNER;
3292 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3293 server->caps |= NFS_CAP_OWNER_GROUP;
3294 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3295 server->caps |= NFS_CAP_ATIME;
3296 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3297 server->caps |= NFS_CAP_CTIME;
3298 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3299 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003300#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3301 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3302 server->caps |= NFS_CAP_SECURITY_LABEL;
3303#endif
3304 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3305 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003306 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003307
Trond Myklebusta65318b2009-03-11 14:10:28 -04003308 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3309 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3310 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003311 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003312 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3313 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003315 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return status;
3319}
3320
Trond Myklebust55a97592006-06-09 09:34:19 -04003321int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 struct nfs4_exception exception = { };
3324 int err;
3325 do {
3326 err = nfs4_handle_exception(server,
3327 _nfs4_server_capabilities(server, fhandle),
3328 &exception);
3329 } while (exception.retry);
3330 return err;
3331}
3332
3333static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3334 struct nfs_fsinfo *info)
3335{
David Quigleyaa9c2662013-05-22 12:50:44 -04003336 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003338 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 };
3340 struct nfs4_lookup_res res = {
3341 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003342 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 .fh = fhandle,
3344 };
3345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3347 .rpc_argp = &args,
3348 .rpc_resp = &res,
3349 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003350
David Quigleyaa9c2662013-05-22 12:50:44 -04003351 bitmask[0] = nfs4_fattr_bitmap[0];
3352 bitmask[1] = nfs4_fattr_bitmap[1];
3353 /*
3354 * Process the label in the upcoming getfattr
3355 */
3356 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3357
Trond Myklebust0e574af2005-10-27 22:12:38 -04003358 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003359 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
3362static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3363 struct nfs_fsinfo *info)
3364{
3365 struct nfs4_exception exception = { };
3366 int err;
3367 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003368 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003369 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003370 switch (err) {
3371 case 0:
3372 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003373 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003374 default:
3375 err = nfs4_handle_exception(server, err, &exception);
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003378out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return err;
3380}
3381
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003382static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3383 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3384{
Trond Myklebustc2190662013-08-26 19:23:04 -04003385 struct rpc_auth_create_args auth_args = {
3386 .pseudoflavor = flavor,
3387 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003388 struct rpc_auth *auth;
3389 int ret;
3390
Trond Myklebustc2190662013-08-26 19:23:04 -04003391 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003392 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003393 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003394 goto out;
3395 }
3396 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003397out:
3398 return ret;
3399}
3400
Chuck Lever9a744ba2013-03-16 15:56:02 -04003401/*
3402 * Retry pseudoroot lookup with various security flavors. We do this when:
3403 *
3404 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3405 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3406 *
3407 * Returns zero on success, or a negative NFS4ERR value, or a
3408 * negative errno value.
3409 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003410static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003411 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003413 /* Per 3530bis 15.33.5 */
3414 static const rpc_authflavor_t flav_array[] = {
3415 RPC_AUTH_GSS_KRB5P,
3416 RPC_AUTH_GSS_KRB5I,
3417 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003418 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003419 RPC_AUTH_NULL,
3420 };
3421 int status = -EPERM;
3422 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003424 if (server->auth_info.flavor_len > 0) {
3425 /* try each flavor specified by user */
3426 for (i = 0; i < server->auth_info.flavor_len; i++) {
3427 status = nfs4_lookup_root_sec(server, fhandle, info,
3428 server->auth_info.flavors[i]);
3429 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3430 continue;
3431 break;
3432 }
3433 } else {
3434 /* no flavors specified by user, try default list */
3435 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3436 status = nfs4_lookup_root_sec(server, fhandle, info,
3437 flav_array[i]);
3438 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3439 continue;
3440 break;
3441 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003442 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003443
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003444 /*
3445 * -EACCESS could mean that the user doesn't have correct permissions
3446 * to access the mount. It could also mean that we tried to mount
3447 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3448 * existing mount programs don't handle -EACCES very well so it should
3449 * be mapped to -EPERM instead.
3450 */
3451 if (status == -EACCES)
3452 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003453 return status;
3454}
3455
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003456/**
3457 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3458 * @server: initialized nfs_server handle
3459 * @fhandle: we fill in the pseudo-fs root file handle
3460 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003461 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003462 *
3463 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003464 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003465int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003466 struct nfs_fsinfo *info,
3467 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003468{
Andre Przywarac7757072015-04-23 17:17:40 +01003469 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003470
Andre Przywarac7757072015-04-23 17:17:40 +01003471 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003472 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003473
3474 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003475 status = server->nfs_client->cl_mvops->find_root_sec(server,
3476 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (status == 0)
3479 status = nfs4_server_capabilities(server, fhandle);
3480 if (status == 0)
3481 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003482
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003483 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Bryan Schumakerbae36242012-05-10 15:07:31 -04003486static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3487 struct nfs_fsinfo *info)
3488{
3489 int error;
3490 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003491 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003492
3493 error = nfs4_server_capabilities(server, mntfh);
3494 if (error < 0) {
3495 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3496 return error;
3497 }
3498
David Quigley14c43f72013-05-22 12:50:43 -04003499 label = nfs4_label_alloc(server, GFP_KERNEL);
3500 if (IS_ERR(label))
3501 return PTR_ERR(label);
3502
David Quigley1775fd32013-05-22 12:50:42 -04003503 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003504 if (error < 0) {
3505 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003506 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003507 }
3508
3509 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3510 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3511 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3512
David Quigley14c43f72013-05-22 12:50:43 -04003513err_free_label:
3514 nfs4_label_free(label);
3515
Bryan Schumakerbae36242012-05-10 15:07:31 -04003516 return error;
3517}
3518
Manoj Naik6b97fd32006-06-09 09:34:29 -04003519/*
3520 * Get locations and (maybe) other attributes of a referral.
3521 * Note that we'll actually follow the referral later when
3522 * we detect fsid mismatch in inode revalidation
3523 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003524static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3525 const struct qstr *name, struct nfs_fattr *fattr,
3526 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003527{
3528 int status = -ENOMEM;
3529 struct page *page = NULL;
3530 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003531
3532 page = alloc_page(GFP_KERNEL);
3533 if (page == NULL)
3534 goto out;
3535 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3536 if (locations == NULL)
3537 goto out;
3538
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003539 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003540 if (status != 0)
3541 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003542
3543 /*
3544 * If the fsid didn't change, this is a migration event, not a
3545 * referral. Cause us to drop into the exception handler, which
3546 * will kick off migration recovery.
3547 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003548 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003549 dprintk("%s: server did not return a different fsid for"
3550 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003551 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003552 goto out;
3553 }
Andy Adamson533eb462011-06-13 18:25:56 -04003554 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3555 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003556
Andy Adamson533eb462011-06-13 18:25:56 -04003557 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003558 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003559 memset(fhandle, 0, sizeof(struct nfs_fh));
3560out:
3561 if (page)
3562 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003563 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003564 return status;
3565}
3566
David Quigley1775fd32013-05-22 12:50:42 -04003567static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3568 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
3570 struct nfs4_getattr_arg args = {
3571 .fh = fhandle,
3572 .bitmask = server->attr_bitmask,
3573 };
3574 struct nfs4_getattr_res res = {
3575 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003576 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 .server = server,
3578 };
3579 struct rpc_message msg = {
3580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3581 .rpc_argp = &args,
3582 .rpc_resp = &res,
3583 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003584
3585 args.bitmask = nfs4_bitmask(server, label);
3586
Trond Myklebust0e574af2005-10-27 22:12:38 -04003587 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003588 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
David Quigley1775fd32013-05-22 12:50:42 -04003591static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3592 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct nfs4_exception exception = { };
3595 int err;
3596 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003597 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3598 trace_nfs4_getattr(server, fhandle, fattr, err);
3599 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 &exception);
3601 } while (exception.retry);
3602 return err;
3603}
3604
3605/*
3606 * The file is not closed if it is opened due to the a request to change
3607 * the size of the file. The open call will not be needed once the
3608 * VFS layer lookup-intents are implemented.
3609 *
3610 * Close is called when the inode is destroyed.
3611 * If we haven't opened the file for O_WRONLY, we
3612 * need to in the size_change case to obtain a stateid.
3613 *
3614 * Got race?
3615 * Because OPEN is always done by name in nfsv4, it is
3616 * possible that we opened a different file by the same
3617 * name. We can recognize this race condition, but we
3618 * can't do anything about it besides returning an error.
3619 *
3620 * This will be fixed with VFS changes (lookup-intent).
3621 */
3622static int
3623nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3624 struct iattr *sattr)
3625{
David Howells2b0143b2015-03-17 22:25:59 +00003626 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003627 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003628 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003629 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 int status;
3631
Peng Tao88ac8152014-09-12 11:04:10 +08003632 if (pnfs_ld_layoutret_on_setattr(inode) &&
3633 sattr->ia_valid & ATTR_SIZE &&
3634 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003635 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003636
Trond Myklebust0e574af2005-10-27 22:12:38 -04003637 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Andy Adamson26699402012-05-30 16:12:24 -04003639 /* Deal with open(O_TRUNC) */
3640 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003641 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003642
3643 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003644 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003645 return 0;
3646
Trond Myklebustd5308382005-11-04 15:33:38 -05003647 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003648 if (sattr->ia_valid & ATTR_FILE) {
3649 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003650
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003651 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003652 if (ctx) {
3653 cred = ctx->cred;
3654 state = ctx->state;
3655 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003656 }
3657
David Quigley14c43f72013-05-22 12:50:43 -04003658 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3659 if (IS_ERR(label))
3660 return PTR_ERR(label);
3661
3662 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003663 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003664 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003665 nfs_setsecurity(inode, fattr, label);
3666 }
David Quigley14c43f72013-05-22 12:50:43 -04003667 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return status;
3669}
3670
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003671static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3672 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003673 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003674{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003675 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003676 int status;
3677 struct nfs4_lookup_arg args = {
3678 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003679 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003680 .name = name,
3681 };
3682 struct nfs4_lookup_res res = {
3683 .server = server,
3684 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003685 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003686 .fh = fhandle,
3687 };
3688 struct rpc_message msg = {
3689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3690 .rpc_argp = &args,
3691 .rpc_resp = &res,
3692 };
3693
David Quigleyaa9c2662013-05-22 12:50:44 -04003694 args.bitmask = nfs4_bitmask(server, label);
3695
David Howells2b3de442006-08-22 20:06:09 -04003696 nfs_fattr_init(fattr);
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003699 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 dprintk("NFS reply lookup: %d\n", status);
3701 return status;
3702}
3703
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003704static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003705{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003706 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003707 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003708 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3709 fattr->nlink = 2;
3710}
3711
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003712static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003713 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003714 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003715{
3716 struct nfs4_exception exception = { };
3717 struct rpc_clnt *client = *clnt;
3718 int err;
3719 do {
David Quigley1775fd32013-05-22 12:50:42 -04003720 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003721 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003722 switch (err) {
3723 case -NFS4ERR_BADNAME:
3724 err = -ENOENT;
3725 goto out;
3726 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003727 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003728 if (err == -NFS4ERR_MOVED)
3729 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003730 goto out;
3731 case -NFS4ERR_WRONGSEC:
3732 err = -EPERM;
3733 if (client != *clnt)
3734 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003735 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003736 if (IS_ERR(client))
3737 return PTR_ERR(client);
3738
3739 exception.retry = 1;
3740 break;
3741 default:
3742 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3743 }
3744 } while (exception.retry);
3745
3746out:
3747 if (err == 0)
3748 *clnt = client;
3749 else if (client != *clnt)
3750 rpc_shutdown_client(client);
3751
3752 return err;
3753}
3754
Al Virobeffb8f2016-07-20 16:34:42 -04003755static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003756 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3757 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003759 int status;
3760 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003761
David Quigley1775fd32013-05-22 12:50:42 -04003762 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003763 if (client != NFS_CLIENT(dir)) {
3764 rpc_shutdown_client(client);
3765 nfs_fixup_secinfo_attributes(fattr);
3766 }
3767 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}
3769
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003770struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003771nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003772 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3773{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003774 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003775 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003776
David Quigley1775fd32013-05-22 12:50:42 -04003777 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003778 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003779 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003780 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003781}
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3784{
Trond Myklebust76b32992007-08-10 17:45:11 -04003785 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 struct nfs4_accessargs args = {
3787 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003788 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003790 struct nfs4_accessres res = {
3791 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003792 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 struct rpc_message msg = {
3794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3795 .rpc_argp = &args,
3796 .rpc_resp = &res,
3797 .rpc_cred = entry->cred,
3798 };
3799 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003800 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 /*
3803 * Determine which access bits we want to ask for...
3804 */
3805 if (mode & MAY_READ)
3806 args.access |= NFS4_ACCESS_READ;
3807 if (S_ISDIR(inode->i_mode)) {
3808 if (mode & MAY_WRITE)
3809 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3810 if (mode & MAY_EXEC)
3811 args.access |= NFS4_ACCESS_LOOKUP;
3812 } else {
3813 if (mode & MAY_WRITE)
3814 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3815 if (mode & MAY_EXEC)
3816 args.access |= NFS4_ACCESS_EXECUTE;
3817 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003818
3819 res.fattr = nfs_alloc_fattr();
3820 if (res.fattr == NULL)
3821 return -ENOMEM;
3822
Bryan Schumaker7c513052011-03-24 17:12:24 +00003823 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003825 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003826 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003828 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return status;
3830}
3831
3832static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3833{
3834 struct nfs4_exception exception = { };
3835 int err;
3836 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003837 err = _nfs4_proc_access(inode, entry);
3838 trace_nfs4_access(inode, err);
3839 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 &exception);
3841 } while (exception.retry);
3842 return err;
3843}
3844
3845/*
3846 * TODO: For the time being, we don't try to get any attributes
3847 * along with any of the zero-copy operations READ, READDIR,
3848 * READLINK, WRITE.
3849 *
3850 * In the case of the first three, we want to put the GETATTR
3851 * after the read-type operation -- this is because it is hard
3852 * to predict the length of a GETATTR response in v4, and thus
3853 * align the READ data correctly. This means that the GETATTR
3854 * may end up partially falling into the page cache, and we should
3855 * shift it into the 'tail' of the xdr_buf before processing.
3856 * To do this efficiently, we need to know the total length
3857 * of data received, which doesn't seem to be available outside
3858 * of the RPC layer.
3859 *
3860 * In the case of WRITE, we also want to put the GETATTR after
3861 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003862 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 *
3864 * Both of these changes to the XDR layer would in fact be quite
3865 * minor, but I decided to leave them for a subsequent patch.
3866 */
3867static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3868 unsigned int pgbase, unsigned int pglen)
3869{
3870 struct nfs4_readlink args = {
3871 .fh = NFS_FH(inode),
3872 .pgbase = pgbase,
3873 .pglen = pglen,
3874 .pages = &page,
3875 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003876 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 struct rpc_message msg = {
3878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3879 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003880 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 };
3882
Bryan Schumaker7c513052011-03-24 17:12:24 +00003883 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 -07003884}
3885
3886static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3887 unsigned int pgbase, unsigned int pglen)
3888{
3889 struct nfs4_exception exception = { };
3890 int err;
3891 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003892 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3893 trace_nfs4_readlink(inode, err);
3894 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 &exception);
3896 } while (exception.retry);
3897 return err;
3898}
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003901 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903static int
3904nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003905 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
David Quigleyaa9c2662013-05-22 12:50:44 -04003907 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003908 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 int status = 0;
3911
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003912 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3913 if (IS_ERR(ctx))
3914 return PTR_ERR(ctx);
3915
David Quigleyaa9c2662013-05-22 12:50:44 -04003916 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3917
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003918 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003919 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 if (IS_ERR(state)) {
3921 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003925 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003926 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return status;
3928}
3929
Al Virobeffb8f2016-07-20 16:34:42 -04003930static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Trond Myklebust16e42952005-10-27 22:12:44 -04003932 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003933 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003935 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003937 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003938 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003939 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3942 .rpc_argp = &args,
3943 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003945 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003946
Bryan Schumaker7c513052011-03-24 17:12:24 +00003947 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003948 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003949 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return status;
3951}
3952
Al Virobeffb8f2016-07-20 16:34:42 -04003953static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 struct nfs4_exception exception = { };
3956 int err;
3957 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003958 err = _nfs4_proc_remove(dir, name);
3959 trace_nfs4_remove(dir, name, err);
3960 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 &exception);
3962 } while (exception.retry);
3963 return err;
3964}
3965
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003966static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003968 struct nfs_server *server = NFS_SERVER(dir);
3969 struct nfs_removeargs *args = msg->rpc_argp;
3970 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003972 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003974 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003975
3976 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003979static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3980{
Al Viro884be172016-04-28 23:56:31 -04003981 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003982 &data->args.seq_args,
3983 &data->res.seq_res,
3984 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003987static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003989 struct nfs_unlinkdata *data = task->tk_calldata;
3990 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003991
Trond Myklebust14516c32010-07-31 14:29:06 -04003992 if (!nfs4_sequence_done(task, &res->seq_res))
3993 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003994 if (nfs4_async_handle_error(task, res->server, NULL,
3995 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003996 return 0;
3997 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
Jeff Laytond3d41522010-09-17 17:31:57 -04004001static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4002{
4003 struct nfs_server *server = NFS_SERVER(dir);
4004 struct nfs_renameargs *arg = msg->rpc_argp;
4005 struct nfs_renameres *res = msg->rpc_resp;
4006
4007 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004008 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004009 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004010}
4011
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004012static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4013{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004014 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4015 &data->args.seq_args,
4016 &data->res.seq_res,
4017 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004018}
4019
4020static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4021 struct inode *new_dir)
4022{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004023 struct nfs_renamedata *data = task->tk_calldata;
4024 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004025
4026 if (!nfs4_sequence_done(task, &res->seq_res))
4027 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004028 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004029 return 0;
4030
4031 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004032 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004033 return 1;
4034}
4035
Al Virobeffb8f2016-07-20 16:34:42 -04004036static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004038 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 struct nfs4_link_arg arg = {
4040 .fh = NFS_FH(inode),
4041 .dir_fh = NFS_FH(dir),
4042 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004043 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004045 struct nfs4_link_res res = {
4046 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004047 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 struct rpc_message msg = {
4050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4051 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004052 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004054 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Trond Myklebust136f2622010-04-16 16:22:49 -04004056 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004057 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004058 goto out;
4059
David Quigley14c43f72013-05-22 12:50:43 -04004060 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4061 if (IS_ERR(res.label)) {
4062 status = PTR_ERR(res.label);
4063 goto out;
4064 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004065 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004066
Bryan Schumaker7c513052011-03-24 17:12:24 +00004067 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004068 if (!status) {
4069 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004070 status = nfs_post_op_update_inode(inode, res.fattr);
4071 if (!status)
4072 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004073 }
David Quigley14c43f72013-05-22 12:50:43 -04004074
4075
4076 nfs4_label_free(res.label);
4077
Trond Myklebust136f2622010-04-16 16:22:49 -04004078out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004079 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return status;
4081}
4082
Al Virobeffb8f2016-07-20 16:34:42 -04004083static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 struct nfs4_exception exception = { };
4086 int err;
4087 do {
4088 err = nfs4_handle_exception(NFS_SERVER(inode),
4089 _nfs4_proc_link(inode, dir, name),
4090 &exception);
4091 } while (exception.retry);
4092 return err;
4093}
4094
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004095struct nfs4_createdata {
4096 struct rpc_message msg;
4097 struct nfs4_create_arg arg;
4098 struct nfs4_create_res res;
4099 struct nfs_fh fh;
4100 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004101 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004102};
4103
4104static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004105 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004106{
4107 struct nfs4_createdata *data;
4108
4109 data = kzalloc(sizeof(*data), GFP_KERNEL);
4110 if (data != NULL) {
4111 struct nfs_server *server = NFS_SERVER(dir);
4112
David Quigley14c43f72013-05-22 12:50:43 -04004113 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4114 if (IS_ERR(data->label))
4115 goto out_free;
4116
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004117 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4118 data->msg.rpc_argp = &data->arg;
4119 data->msg.rpc_resp = &data->res;
4120 data->arg.dir_fh = NFS_FH(dir);
4121 data->arg.server = server;
4122 data->arg.name = name;
4123 data->arg.attrs = sattr;
4124 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004125 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004126 data->res.server = server;
4127 data->res.fh = &data->fh;
4128 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004129 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004130 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004131 }
4132 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004133out_free:
4134 kfree(data);
4135 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004136}
4137
4138static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4139{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004140 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004141 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004142 if (status == 0) {
4143 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004144 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004145 }
4146 return status;
4147}
4148
4149static void nfs4_free_createdata(struct nfs4_createdata *data)
4150{
David Quigley14c43f72013-05-22 12:50:43 -04004151 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004152 kfree(data);
4153}
4154
Chuck Lever4f390c12006-08-22 20:06:22 -04004155static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004156 struct page *page, unsigned int len, struct iattr *sattr,
4157 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 struct nfs4_createdata *data;
4160 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Chuck Lever94a6d752006-08-22 20:06:23 -04004162 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004163 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004164
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004165 status = -ENOMEM;
4166 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4167 if (data == NULL)
4168 goto out;
4169
4170 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4171 data->arg.u.symlink.pages = &page;
4172 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004173 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004175 status = nfs4_do_create(dir, dentry, data);
4176
4177 nfs4_free_createdata(data);
4178out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return status;
4180}
4181
Chuck Lever4f390c12006-08-22 20:06:22 -04004182static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004183 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004186 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004188
4189 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004192 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4193 trace_nfs4_symlink(dir, &dentry->d_name, err);
4194 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 &exception);
4196 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004197
4198 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return err;
4200}
4201
4202static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004203 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205 struct nfs4_createdata *data;
4206 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4209 if (data == NULL)
4210 goto out;
4211
David Quigley1775fd32013-05-22 12:50:42 -04004212 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004213 status = nfs4_do_create(dir, dentry, data);
4214
4215 nfs4_free_createdata(data);
4216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 return status;
4218}
4219
4220static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4221 struct iattr *sattr)
4222{
4223 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004224 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004226
David Quigleyaa9c2662013-05-22 12:50:44 -04004227 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4228
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004229 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004231 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4232 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4233 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 &exception);
4235 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004236 nfs4_label_release_security(label);
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return err;
4239}
4240
4241static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004242 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
David Howells2b0143b2015-03-17 22:25:59 +00004244 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 struct nfs4_readdir_arg args = {
4246 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004247 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 .pgbase = 0,
4249 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004250 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004251 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 };
4253 struct nfs4_readdir_res res;
4254 struct rpc_message msg = {
4255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4256 .rpc_argp = &args,
4257 .rpc_resp = &res,
4258 .rpc_cred = cred,
4259 };
4260 int status;
4261
Al Viro6de14722013-09-16 10:53:17 -04004262 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4263 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004264 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004265 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004267 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 -05004268 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004269 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004270 status += args.pgbase;
4271 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004272
4273 nfs_invalidate_atime(dir);
4274
Harvey Harrison3110ff82008-05-02 13:42:44 -07004275 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return status;
4277}
4278
4279static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004280 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
4282 struct nfs4_exception exception = { };
4283 int err;
4284 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004285 err = _nfs4_proc_readdir(dentry, cred, cookie,
4286 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004287 trace_nfs4_readdir(d_inode(dentry), err);
4288 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 &exception);
4290 } while (exception.retry);
4291 return err;
4292}
4293
4294static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004295 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004297 struct nfs4_createdata *data;
4298 int mode = sattr->ia_mode;
4299 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004301 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4302 if (data == NULL)
4303 goto out;
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004306 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004308 data->arg.ftype = NF4BLK;
4309 data->arg.u.device.specdata1 = MAJOR(rdev);
4310 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
4312 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004313 data->arg.ftype = NF4CHR;
4314 data->arg.u.device.specdata1 = MAJOR(rdev);
4315 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004316 } else if (!S_ISSOCK(mode)) {
4317 status = -EINVAL;
4318 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 }
David Quigley1775fd32013-05-22 12:50:42 -04004320
David Quigleyaa9c2662013-05-22 12:50:44 -04004321 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004322 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004323out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004324 nfs4_free_createdata(data);
4325out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return status;
4327}
4328
4329static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4330 struct iattr *sattr, dev_t rdev)
4331{
4332 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004333 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004335
David Quigleyaa9c2662013-05-22 12:50:44 -04004336 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4337
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004338 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004340 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4341 trace_nfs4_mknod(dir, &dentry->d_name, err);
4342 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 &exception);
4344 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004345
4346 nfs4_label_release_security(label);
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return err;
4349}
4350
4351static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4352 struct nfs_fsstat *fsstat)
4353{
4354 struct nfs4_statfs_arg args = {
4355 .fh = fhandle,
4356 .bitmask = server->attr_bitmask,
4357 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004358 struct nfs4_statfs_res res = {
4359 .fsstat = fsstat,
4360 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 struct rpc_message msg = {
4362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4363 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004364 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 };
4366
Trond Myklebust0e574af2005-10-27 22:12:38 -04004367 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004368 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370
4371static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4372{
4373 struct nfs4_exception exception = { };
4374 int err;
4375 do {
4376 err = nfs4_handle_exception(server,
4377 _nfs4_proc_statfs(server, fhandle, fsstat),
4378 &exception);
4379 } while (exception.retry);
4380 return err;
4381}
4382
4383static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4384 struct nfs_fsinfo *fsinfo)
4385{
4386 struct nfs4_fsinfo_arg args = {
4387 .fh = fhandle,
4388 .bitmask = server->attr_bitmask,
4389 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004390 struct nfs4_fsinfo_res res = {
4391 .fsinfo = fsinfo,
4392 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 struct rpc_message msg = {
4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4395 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004396 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 };
4398
Bryan Schumaker7c513052011-03-24 17:12:24 +00004399 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400}
4401
4402static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4403{
4404 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004405 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 int err;
4407
4408 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004409 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004410 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004411 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004412 nfs4_set_lease_period(server->nfs_client,
4413 fsinfo->lease_time * HZ,
4414 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004415 break;
4416 }
4417 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 } while (exception.retry);
4419 return err;
4420}
4421
4422static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4423{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004424 int error;
4425
Trond Myklebust0e574af2005-10-27 22:12:38 -04004426 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004427 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004428 if (error == 0) {
4429 /* block layout checks this! */
4430 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004431 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004432 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004433
4434 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
4437static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4438 struct nfs_pathconf *pathconf)
4439{
4440 struct nfs4_pathconf_arg args = {
4441 .fh = fhandle,
4442 .bitmask = server->attr_bitmask,
4443 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004444 struct nfs4_pathconf_res res = {
4445 .pathconf = pathconf,
4446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 struct rpc_message msg = {
4448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4449 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004450 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 };
4452
4453 /* None of the pathconf attributes are mandatory to implement */
4454 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4455 memset(pathconf, 0, sizeof(*pathconf));
4456 return 0;
4457 }
4458
Trond Myklebust0e574af2005-10-27 22:12:38 -04004459 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004460 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4464 struct nfs_pathconf *pathconf)
4465{
4466 struct nfs4_exception exception = { };
4467 int err;
4468
4469 do {
4470 err = nfs4_handle_exception(server,
4471 _nfs4_proc_pathconf(server, fhandle, pathconf),
4472 &exception);
4473 } while (exception.retry);
4474 return err;
4475}
4476
Trond Myklebust5521abf2013-03-16 20:54:34 -04004477int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004478 const struct nfs_open_context *ctx,
4479 const struct nfs_lock_context *l_ctx,
4480 fmode_t fmode)
4481{
4482 const struct nfs_lockowner *lockowner = NULL;
4483
4484 if (l_ctx != NULL)
4485 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004486 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004487}
4488EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4489
Trond Myklebust5521abf2013-03-16 20:54:34 -04004490static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4491 const struct nfs_open_context *ctx,
4492 const struct nfs_lock_context *l_ctx,
4493 fmode_t fmode)
4494{
4495 nfs4_stateid current_stateid;
4496
Trond Myklebuste1253be2014-03-05 08:44:23 -05004497 /* If the current stateid represents a lost lock, then exit */
4498 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4499 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004500 return nfs4_stateid_match(stateid, &current_stateid);
4501}
4502
4503static bool nfs4_error_stateid_expired(int err)
4504{
4505 switch (err) {
4506 case -NFS4ERR_DELEG_REVOKED:
4507 case -NFS4ERR_ADMIN_REVOKED:
4508 case -NFS4ERR_BAD_STATEID:
4509 case -NFS4ERR_STALE_STATEID:
4510 case -NFS4ERR_OLD_STATEID:
4511 case -NFS4ERR_OPENMODE:
4512 case -NFS4ERR_EXPIRED:
4513 return true;
4514 }
4515 return false;
4516}
4517
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004518void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004519{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004520 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004521}
4522
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004523static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004525 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004527 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004528 if (task->tk_status < 0) {
4529 struct nfs4_exception exception = {
4530 .inode = hdr->inode,
4531 .state = hdr->args.context->state,
4532 .stateid = &hdr->args.stateid,
4533 };
4534 task->tk_status = nfs4_async_handle_exception(task,
4535 server, task->tk_status, &exception);
4536 if (exception.retry) {
4537 rpc_restart_call_prepare(task);
4538 return -EAGAIN;
4539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004541
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004542 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Trond Myklebust5521abf2013-03-16 20:54:34 -04004548static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004549 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004550{
4551
4552 if (!nfs4_error_stateid_expired(task->tk_status) ||
4553 nfs4_stateid_is_current(&args->stateid,
4554 args->context,
4555 args->lock_context,
4556 FMODE_READ))
4557 return false;
4558 rpc_restart_call_prepare(task);
4559 return true;
4560}
4561
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004562static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004563{
4564
4565 dprintk("--> %s\n", __func__);
4566
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004567 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004568 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004569 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004570 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004571 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4572 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004573}
4574
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004575static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4576 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004578 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004579 if (!hdr->pgio_done_cb)
4580 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004581 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004582 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583}
4584
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004585static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4586 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004587{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004588 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4589 &hdr->args.seq_args,
4590 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004591 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004592 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004593 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4594 hdr->args.lock_context,
4595 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004596 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004598 return -EIO;
4599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004602static int nfs4_write_done_cb(struct rpc_task *task,
4603 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004606
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004607 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004608 if (task->tk_status < 0) {
4609 struct nfs4_exception exception = {
4610 .inode = hdr->inode,
4611 .state = hdr->args.context->state,
4612 .stateid = &hdr->args.stateid,
4613 };
4614 task->tk_status = nfs4_async_handle_exception(task,
4615 NFS_SERVER(inode), task->tk_status,
4616 &exception);
4617 if (exception.retry) {
4618 rpc_restart_call_prepare(task);
4619 return -EAGAIN;
4620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004622 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004624 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004625 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627}
4628
Trond Myklebust5521abf2013-03-16 20:54:34 -04004629static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004630 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004631{
4632
4633 if (!nfs4_error_stateid_expired(task->tk_status) ||
4634 nfs4_stateid_is_current(&args->stateid,
4635 args->context,
4636 args->lock_context,
4637 FMODE_WRITE))
4638 return false;
4639 rpc_restart_call_prepare(task);
4640 return true;
4641}
4642
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004644{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004646 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004647 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004648 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4650 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004651}
4652
Trond Myklebust5a37f852012-04-28 14:55:16 -04004653static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004655{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004656 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004658 return false;
4659 /* Otherwise, request attributes if and only if we don't hold
4660 * a delegation
4661 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004662 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004663}
Fred Isamana69aef12011-03-03 15:13:47 +00004664
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4666 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004669
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004670 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4671 hdr->args.bitmask = NULL;
4672 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004673 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004675
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676 if (!hdr->pgio_done_cb)
4677 hdr->pgio_done_cb = nfs4_write_done_cb;
4678 hdr->res.server = server;
4679 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004681 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004682 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Fred Isaman0b7c0152012-04-20 14:47:39 -04004685static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4686{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004687 nfs4_setup_sequence(NFS_SERVER(data->inode),
4688 &data->args.seq_args,
4689 &data->res.seq_res,
4690 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Fred Isaman0b7c0152012-04-20 14:47:39 -04004693static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004696
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004697 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004698 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4699 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004700 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004701 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
Fred Isaman0b7c0152012-04-20 14:47:39 -04004706static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004707{
4708 if (!nfs4_sequence_done(task, &data->res.seq_res))
4709 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004710 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004711}
4712
Fred Isaman0b7c0152012-04-20 14:47:39 -04004713static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004715 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004716
Fred Isaman0b7c0152012-04-20 14:47:39 -04004717 if (data->commit_done_cb == NULL)
4718 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004719 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004720 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004721 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004724struct nfs4_renewdata {
4725 struct nfs_client *client;
4726 unsigned long timestamp;
4727};
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729/*
4730 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4731 * standalone procedure for queueing an asynchronous RENEW.
4732 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004733static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004734{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004735 struct nfs4_renewdata *data = calldata;
4736 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004737
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004738 if (atomic_read(&clp->cl_count) > 1)
4739 nfs4_schedule_state_renewal(clp);
4740 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004741 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004742}
4743
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004744static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004746 struct nfs4_renewdata *data = calldata;
4747 struct nfs_client *clp = data->client;
4748 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004750 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004751 switch (task->tk_status) {
4752 case 0:
4753 break;
4754 case -NFS4ERR_LEASE_MOVED:
4755 nfs4_schedule_lease_moved_recovery(clp);
4756 break;
4757 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004758 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004759 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4760 return;
4761 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004762 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004763 return;
4764 }
4765 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004767 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768}
4769
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004770static const struct rpc_call_ops nfs4_renew_ops = {
4771 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004772 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004773};
4774
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004775static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
4777 struct rpc_message msg = {
4778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4779 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004780 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004782 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004784 if (renew_flags == 0)
4785 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004786 if (!atomic_inc_not_zero(&clp->cl_count))
4787 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004788 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004789 if (data == NULL)
4790 return -ENOMEM;
4791 data->client = clp;
4792 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004793 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004794 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004797static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
4799 struct rpc_message msg = {
4800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4801 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004802 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 };
4804 unsigned long now = jiffies;
4805 int status;
4806
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004807 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 if (status < 0)
4809 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004810 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return 0;
4812}
4813
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004814static inline int nfs4_server_supports_acls(struct nfs_server *server)
4815{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004816 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004817}
4818
Trond Myklebust21f498c2012-08-24 10:59:25 -04004819/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4820 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004821 * the stack.
4822 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004823#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004824
Neil Hormane9e3d722011-03-04 19:26:03 -05004825static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004826 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004827{
4828 struct page *newpage, **spages;
4829 int rc = 0;
4830 size_t len;
4831 spages = pages;
4832
4833 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004834 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004835 newpage = alloc_page(GFP_KERNEL);
4836
4837 if (newpage == NULL)
4838 goto unwind;
4839 memcpy(page_address(newpage), buf, len);
4840 buf += len;
4841 buflen -= len;
4842 *pages++ = newpage;
4843 rc++;
4844 } while (buflen != 0);
4845
4846 return rc;
4847
4848unwind:
4849 for(; rc > 0; rc--)
4850 __free_page(spages[rc-1]);
4851 return -ENOMEM;
4852}
4853
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004854struct nfs4_cached_acl {
4855 int cached;
4856 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004857 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004858};
4859
4860static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004861{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004862 struct nfs_inode *nfsi = NFS_I(inode);
4863
4864 spin_lock(&inode->i_lock);
4865 kfree(nfsi->nfs4_acl);
4866 nfsi->nfs4_acl = acl;
4867 spin_unlock(&inode->i_lock);
4868}
4869
4870static void nfs4_zap_acl_attr(struct inode *inode)
4871{
4872 nfs4_set_cached_acl(inode, NULL);
4873}
4874
4875static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4876{
4877 struct nfs_inode *nfsi = NFS_I(inode);
4878 struct nfs4_cached_acl *acl;
4879 int ret = -ENOENT;
4880
4881 spin_lock(&inode->i_lock);
4882 acl = nfsi->nfs4_acl;
4883 if (acl == NULL)
4884 goto out;
4885 if (buf == NULL) /* user is just asking for length */
4886 goto out_len;
4887 if (acl->cached == 0)
4888 goto out;
4889 ret = -ERANGE; /* see getxattr(2) man page */
4890 if (acl->len > buflen)
4891 goto out;
4892 memcpy(buf, acl->data, acl->len);
4893out_len:
4894 ret = acl->len;
4895out:
4896 spin_unlock(&inode->i_lock);
4897 return ret;
4898}
4899
Sachin Prabhu5794d212012-04-17 14:36:40 +01004900static 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 +00004901{
4902 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004903 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004904
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004905 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004906 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004907 if (acl == NULL)
4908 goto out;
4909 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004910 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004911 } else {
4912 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4913 if (acl == NULL)
4914 goto out;
4915 acl->cached = 0;
4916 }
4917 acl->len = acl_len;
4918out:
4919 nfs4_set_cached_acl(inode, acl);
4920}
4921
Andy Adamsonbf118a32011-12-07 11:55:27 -05004922/*
4923 * The getxattr API returns the required buffer length when called with a
4924 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4925 * the required buf. On a NULL buf, we send a page of data to the server
4926 * guessing that the ACL request can be serviced by a page. If so, we cache
4927 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4928 * the cache. If not so, we throw away the page, and cache the required
4929 * length. The next getxattr call will then produce another round trip to
4930 * the server, this time with the input buf of the required size.
4931 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004932static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004933{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004934 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004935 struct nfs_getaclargs args = {
4936 .fh = NFS_FH(inode),
4937 .acl_pages = pages,
4938 .acl_len = buflen,
4939 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004940 struct nfs_getaclres res = {
4941 .acl_len = buflen,
4942 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004943 struct rpc_message msg = {
4944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4945 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004946 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004947 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004948 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4949 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004950
Andy Adamsonbf118a32011-12-07 11:55:27 -05004951 /* As long as we're doing a round trip to the server anyway,
4952 * let's be prepared for a page of acl data. */
4953 if (npages == 0)
4954 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004955 if (npages > ARRAY_SIZE(pages))
4956 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004957
Andy Adamsonbf118a32011-12-07 11:55:27 -05004958 for (i = 0; i < npages; i++) {
4959 pages[i] = alloc_page(GFP_KERNEL);
4960 if (!pages[i])
4961 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004962 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004963
4964 /* for decoding across pages */
4965 res.acl_scratch = alloc_page(GFP_KERNEL);
4966 if (!res.acl_scratch)
4967 goto out_free;
4968
Andy Adamsonbf118a32011-12-07 11:55:27 -05004969 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004970
Peng Taode040be2012-01-10 22:42:47 +08004971 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004972 __func__, buf, buflen, npages, args.acl_len);
4973 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4974 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004975 if (ret)
4976 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004977
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004978 /* Handle the case where the passed-in buffer is too short */
4979 if (res.acl_flags & NFS4_ACL_TRUNC) {
4980 /* Did the user only issue a request for the acl length? */
4981 if (buf == NULL)
4982 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004983 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004984 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004985 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004986 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004987 if (buf) {
4988 if (res.acl_len > buflen) {
4989 ret = -ERANGE;
4990 goto out_free;
4991 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004992 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004993 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004994out_ok:
4995 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004996out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004997 for (i = 0; i < npages; i++)
4998 if (pages[i])
4999 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005000 if (res.acl_scratch)
5001 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005002 return ret;
5003}
5004
Trond Myklebust16b42892006-08-24 12:27:15 -04005005static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5006{
5007 struct nfs4_exception exception = { };
5008 ssize_t ret;
5009 do {
5010 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005011 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005012 if (ret >= 0)
5013 break;
5014 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5015 } while (exception.retry);
5016 return ret;
5017}
5018
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005019static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5020{
5021 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005022 int ret;
5023
5024 if (!nfs4_server_supports_acls(server))
5025 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005026 ret = nfs_revalidate_inode(server, inode);
5027 if (ret < 0)
5028 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005029 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5030 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005031 ret = nfs4_read_cached_acl(inode, buf, buflen);
5032 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005033 /* -ENOENT is returned if there is no ACL or if there is an ACL
5034 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005035 return ret;
5036 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005037}
5038
Trond Myklebust16b42892006-08-24 12:27:15 -04005039static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005040{
5041 struct nfs_server *server = NFS_SERVER(inode);
5042 struct page *pages[NFS4ACL_MAXPAGES];
5043 struct nfs_setaclargs arg = {
5044 .fh = NFS_FH(inode),
5045 .acl_pages = pages,
5046 .acl_len = buflen,
5047 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005048 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005049 struct rpc_message msg = {
5050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5051 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005052 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005053 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005054 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005055 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005056
5057 if (!nfs4_server_supports_acls(server))
5058 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005059 if (npages > ARRAY_SIZE(pages))
5060 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005061 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005062 if (i < 0)
5063 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005064 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005065 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005066
5067 /*
5068 * Free each page after tx, so the only ref left is
5069 * held by the network stack
5070 */
5071 for (; i > 0; i--)
5072 put_page(pages[i-1]);
5073
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005074 /*
5075 * Acl update can result in inode attribute update.
5076 * so mark the attribute cache invalid.
5077 */
5078 spin_lock(&inode->i_lock);
5079 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5080 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005081 nfs_access_zap_cache(inode);
5082 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005083 return ret;
5084}
5085
Trond Myklebust16b42892006-08-24 12:27:15 -04005086static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5087{
5088 struct nfs4_exception exception = { };
5089 int err;
5090 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005091 err = __nfs4_proc_set_acl(inode, buf, buflen);
5092 trace_nfs4_set_acl(inode, err);
5093 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005094 &exception);
5095 } while (exception.retry);
5096 return err;
5097}
5098
David Quigleyaa9c2662013-05-22 12:50:44 -04005099#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5100static int _nfs4_get_security_label(struct inode *inode, void *buf,
5101 size_t buflen)
5102{
5103 struct nfs_server *server = NFS_SERVER(inode);
5104 struct nfs_fattr fattr;
5105 struct nfs4_label label = {0, 0, buflen, buf};
5106
5107 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005108 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005109 .fh = NFS_FH(inode),
5110 .bitmask = bitmask,
5111 };
5112 struct nfs4_getattr_res res = {
5113 .fattr = &fattr,
5114 .label = &label,
5115 .server = server,
5116 };
5117 struct rpc_message msg = {
5118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005119 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005120 .rpc_resp = &res,
5121 };
5122 int ret;
5123
5124 nfs_fattr_init(&fattr);
5125
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005126 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005127 if (ret)
5128 return ret;
5129 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5130 return -ENOENT;
5131 if (buflen < label.len)
5132 return -ERANGE;
5133 return 0;
5134}
5135
5136static int nfs4_get_security_label(struct inode *inode, void *buf,
5137 size_t buflen)
5138{
5139 struct nfs4_exception exception = { };
5140 int err;
5141
5142 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5143 return -EOPNOTSUPP;
5144
5145 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005146 err = _nfs4_get_security_label(inode, buf, buflen);
5147 trace_nfs4_get_security_label(inode, err);
5148 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005149 &exception);
5150 } while (exception.retry);
5151 return err;
5152}
5153
5154static int _nfs4_do_set_security_label(struct inode *inode,
5155 struct nfs4_label *ilabel,
5156 struct nfs_fattr *fattr,
5157 struct nfs4_label *olabel)
5158{
5159
5160 struct iattr sattr = {0};
5161 struct nfs_server *server = NFS_SERVER(inode);
5162 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005163 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005164 .fh = NFS_FH(inode),
5165 .iap = &sattr,
5166 .server = server,
5167 .bitmask = bitmask,
5168 .label = ilabel,
5169 };
5170 struct nfs_setattrres res = {
5171 .fattr = fattr,
5172 .label = olabel,
5173 .server = server,
5174 };
5175 struct rpc_message msg = {
5176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005177 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005178 .rpc_resp = &res,
5179 };
5180 int status;
5181
Jeff Layton12207f62013-11-01 10:49:32 -04005182 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005183
Jeff Layton12207f62013-11-01 10:49:32 -04005184 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005185 if (status)
5186 dprintk("%s failed: %d\n", __func__, status);
5187
5188 return status;
5189}
5190
5191static int nfs4_do_set_security_label(struct inode *inode,
5192 struct nfs4_label *ilabel,
5193 struct nfs_fattr *fattr,
5194 struct nfs4_label *olabel)
5195{
5196 struct nfs4_exception exception = { };
5197 int err;
5198
5199 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005200 err = _nfs4_do_set_security_label(inode, ilabel,
5201 fattr, olabel);
5202 trace_nfs4_set_security_label(inode, err);
5203 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005204 &exception);
5205 } while (exception.retry);
5206 return err;
5207}
5208
5209static int
Al Viro59301222016-05-27 10:19:30 -04005210nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005211{
5212 struct nfs4_label ilabel, *olabel = NULL;
5213 struct nfs_fattr fattr;
5214 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005215 int status;
5216
5217 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5218 return -EOPNOTSUPP;
5219
5220 nfs_fattr_init(&fattr);
5221
5222 ilabel.pi = 0;
5223 ilabel.lfs = 0;
5224 ilabel.label = (char *)buf;
5225 ilabel.len = buflen;
5226
5227 cred = rpc_lookup_cred();
5228 if (IS_ERR(cred))
5229 return PTR_ERR(cred);
5230
5231 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5232 if (IS_ERR(olabel)) {
5233 status = -PTR_ERR(olabel);
5234 goto out;
5235 }
5236
5237 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5238 if (status == 0)
5239 nfs_setsecurity(inode, &fattr, olabel);
5240
5241 nfs4_label_free(olabel);
5242out:
5243 put_rpccred(cred);
5244 return status;
5245}
5246#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5247
5248
Chuck Leverf0920752012-05-21 22:45:41 -04005249static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5250 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005251{
5252 __be32 verf[2];
5253
Chuck Lever2c820d92012-05-21 22:45:33 -04005254 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5255 /* An impossible timestamp guarantees this value
5256 * will never match a generated boot time. */
5257 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005258 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005259 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005260 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005261 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5262 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005263 }
Chuck Levercd937102012-03-02 17:14:31 -05005264 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5265}
5266
Jeff Laytona3192682015-06-09 19:43:59 -04005267static int
5268nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005269{
Jeff Laytona3192682015-06-09 19:43:59 -04005270 size_t len;
5271 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005272
Trond Myklebustceb3a162015-01-03 15:16:04 -05005273 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005274 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005275
Jeff Laytona3192682015-06-09 19:43:59 -04005276 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005277 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005278 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5279 1 +
5280 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5281 1;
5282 rcu_read_unlock();
5283
5284 if (len > NFS4_OPAQUE_LIMIT + 1)
5285 return -EINVAL;
5286
5287 /*
5288 * Since this string is allocated at mount time, and held until the
5289 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5290 * about a memory-reclaim deadlock.
5291 */
5292 str = kmalloc(len, GFP_KERNEL);
5293 if (!str)
5294 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005295
Chuck Levere984a552012-09-14 17:24:21 -04005296 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005297 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005298 clp->cl_ipaddr,
5299 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5300 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005301 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005302
Jeff Laytona3192682015-06-09 19:43:59 -04005303 clp->cl_owner_id = str;
5304 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005305}
5306
Jeff Layton873e3852015-06-09 19:44:00 -04005307static int
5308nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005309{
Jeff Layton873e3852015-06-09 19:44:00 -04005310 size_t len;
5311 char *str;
5312
5313 len = 10 + 10 + 1 + 10 + 1 +
5314 strlen(nfs4_client_id_uniquifier) + 1 +
5315 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5316
5317 if (len > NFS4_OPAQUE_LIMIT + 1)
5318 return -EINVAL;
5319
5320 /*
5321 * Since this string is allocated at mount time, and held until the
5322 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5323 * about a memory-reclaim deadlock.
5324 */
5325 str = kmalloc(len, GFP_KERNEL);
5326 if (!str)
5327 return -ENOMEM;
5328
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005329 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005330 clp->rpc_ops->version, clp->cl_minorversion,
5331 nfs4_client_id_uniquifier,
5332 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005333 clp->cl_owner_id = str;
5334 return 0;
5335}
5336
5337static int
5338nfs4_init_uniform_client_string(struct nfs_client *clp)
5339{
Jeff Layton873e3852015-06-09 19:44:00 -04005340 size_t len;
5341 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005342
5343 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005344 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005345
5346 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005347 return nfs4_init_uniquifier_client_string(clp);
5348
5349 len = 10 + 10 + 1 + 10 + 1 +
5350 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5351
5352 if (len > NFS4_OPAQUE_LIMIT + 1)
5353 return -EINVAL;
5354
5355 /*
5356 * Since this string is allocated at mount time, and held until the
5357 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5358 * about a memory-reclaim deadlock.
5359 */
5360 str = kmalloc(len, GFP_KERNEL);
5361 if (!str)
5362 return -ENOMEM;
5363
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005364 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005365 clp->rpc_ops->version, clp->cl_minorversion,
5366 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005367 clp->cl_owner_id = str;
5368 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005369}
5370
Chuck Lever706cb8d2014-03-12 12:51:47 -04005371/*
5372 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5373 * services. Advertise one based on the address family of the
5374 * clientaddr.
5375 */
5376static unsigned int
5377nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5378{
5379 if (strchr(clp->cl_ipaddr, ':') != NULL)
5380 return scnprintf(buf, len, "tcp6");
5381 else
5382 return scnprintf(buf, len, "tcp");
5383}
5384
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005385static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5386{
5387 struct nfs4_setclientid *sc = calldata;
5388
5389 if (task->tk_status == 0)
5390 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5391}
5392
5393static const struct rpc_call_ops nfs4_setclientid_ops = {
5394 .rpc_call_done = nfs4_setclientid_done,
5395};
5396
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005397/**
5398 * nfs4_proc_setclientid - Negotiate client ID
5399 * @clp: state data structure
5400 * @program: RPC program for NFSv4 callback service
5401 * @port: IP port number for NFS4 callback service
5402 * @cred: RPC credential to use for this call
5403 * @res: where to place the result
5404 *
5405 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5406 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005407int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5408 unsigned short port, struct rpc_cred *cred,
5409 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410{
5411 nfs4_verifier sc_verifier;
5412 struct nfs4_setclientid setclientid = {
5413 .sc_verifier = &sc_verifier,
5414 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005415 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 };
5417 struct rpc_message msg = {
5418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5419 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005420 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005421 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005423 struct rpc_task *task;
5424 struct rpc_task_setup task_setup_data = {
5425 .rpc_client = clp->cl_rpcclient,
5426 .rpc_message = &msg,
5427 .callback_ops = &nfs4_setclientid_ops,
5428 .callback_data = &setclientid,
5429 .flags = RPC_TASK_TIMEOUT,
5430 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005431 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Chuck Leverde734832012-07-11 16:30:50 -04005433 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005434 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005435
5436 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5437 status = nfs4_init_uniform_client_string(clp);
5438 else
Jeff Laytona3192682015-06-09 19:43:59 -04005439 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005440
5441 if (status)
5442 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005443
Chuck Leverde734832012-07-11 16:30:50 -04005444 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005445 setclientid.sc_netid_len =
5446 nfs4_init_callback_netid(clp,
5447 setclientid.sc_netid,
5448 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005449 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005450 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 clp->cl_ipaddr, port >> 8, port & 255);
5452
Jeff Layton3a6bb732015-06-09 19:43:57 -04005453 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005454 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005455 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005456 task = rpc_run_task(&task_setup_data);
5457 if (IS_ERR(task)) {
5458 status = PTR_ERR(task);
5459 goto out;
5460 }
5461 status = task->tk_status;
5462 if (setclientid.sc_cred) {
5463 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5464 put_rpccred(setclientid.sc_cred);
5465 }
5466 rpc_put_task(task);
5467out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005468 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005469 dprintk("NFS reply setclientid: %d\n", status);
5470 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471}
5472
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005473/**
5474 * nfs4_proc_setclientid_confirm - Confirm client ID
5475 * @clp: state data structure
5476 * @res: result of a previous SETCLIENTID
5477 * @cred: RPC credential to use for this call
5478 *
5479 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5480 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005481int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005482 struct nfs4_setclientid_res *arg,
5483 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 struct rpc_message msg = {
5486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005487 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005488 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 int status;
5491
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005492 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5493 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5494 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005495 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005496 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005497 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 return status;
5499}
5500
Trond Myklebustfe650402006-01-03 09:55:18 +01005501struct nfs4_delegreturndata {
5502 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005503 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005504 struct nfs_fh fh;
5505 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005506 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005507 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005508 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005509 struct inode *inode;
5510 bool roc;
5511 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005512};
5513
Trond Myklebustfe650402006-01-03 09:55:18 +01005514static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5515{
5516 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005517
Trond Myklebust14516c32010-07-31 14:29:06 -04005518 if (!nfs4_sequence_done(task, &data->res.seq_res))
5519 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005520
Trond Myklebustca8acf82013-08-13 10:36:56 -04005521 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005522 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005523 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005524 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005525 case -NFS4ERR_ADMIN_REVOKED:
5526 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005527 case -NFS4ERR_EXPIRED:
5528 nfs4_free_revoked_stateid(data->res.server,
5529 data->args.stateid,
5530 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005531 case -NFS4ERR_BAD_STATEID:
5532 case -NFS4ERR_OLD_STATEID:
5533 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005534 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005535 if (data->roc)
5536 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005537 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005538 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005539 if (nfs4_async_handle_error(task, data->res.server,
5540 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005541 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005542 return;
5543 }
5544 }
5545 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005546}
5547
5548static void nfs4_delegreturn_release(void *calldata)
5549{
Peng Tao039b7562014-07-03 13:05:02 +08005550 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005551 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005552
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005553 if (inode) {
5554 if (data->roc)
5555 pnfs_roc_release(inode);
5556 nfs_iput_and_deactive(inode);
5557 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005558 kfree(calldata);
5559}
5560
Andy Adamson938e1012009-04-01 09:22:28 -04005561static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5562{
5563 struct nfs4_delegreturndata *d_data;
5564
5565 d_data = (struct nfs4_delegreturndata *)data;
5566
Peng Tao500d7012015-09-22 11:35:22 +08005567 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5568 return;
5569
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005570 if (d_data->roc)
5571 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005572
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005573 nfs4_setup_sequence(d_data->res.server,
5574 &d_data->args.seq_args,
5575 &d_data->res.seq_res,
5576 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005577}
Andy Adamson938e1012009-04-01 09:22:28 -04005578
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005579static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005580 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005581 .rpc_call_done = nfs4_delegreturn_done,
5582 .rpc_release = nfs4_delegreturn_release,
5583};
5584
Trond Myklebuste6f81072008-01-24 18:14:34 -05005585static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005586{
5587 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005588 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005589 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005590 struct rpc_message msg = {
5591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5592 .rpc_cred = cred,
5593 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005594 struct rpc_task_setup task_setup_data = {
5595 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005596 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005597 .callback_ops = &nfs4_delegreturn_ops,
5598 .flags = RPC_TASK_ASYNC,
5599 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005600 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005601
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005602 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005603 if (data == NULL)
5604 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005605 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005606
5607 nfs4_state_protect(server->nfs_client,
5608 NFS_SP4_MACH_CRED_CLEANUP,
5609 &task_setup_data.rpc_client, &msg);
5610
Trond Myklebustfe650402006-01-03 09:55:18 +01005611 data->args.fhandle = &data->fh;
5612 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005613 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005614 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005615 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005616 data->res.fattr = &data->fattr;
5617 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005618 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005619 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005620 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005621 data->inode = nfs_igrab_and_active(inode);
5622 if (data->inode)
5623 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005624
Trond Myklebustc970aa82007-07-14 15:39:59 -04005625 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005626 msg.rpc_argp = &data->args;
5627 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005628 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005629 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005630 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005631 if (!issync)
5632 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005633 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005634 if (status != 0)
5635 goto out;
5636 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005637 if (status == 0)
5638 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5639 else
5640 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005641out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005642 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005643 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644}
5645
Trond Myklebuste6f81072008-01-24 18:14:34 -05005646int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647{
5648 struct nfs_server *server = NFS_SERVER(inode);
5649 struct nfs4_exception exception = { };
5650 int err;
5651 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005652 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005653 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 switch (err) {
5655 case -NFS4ERR_STALE_STATEID:
5656 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 case 0:
5658 return 0;
5659 }
5660 err = nfs4_handle_exception(server, err, &exception);
5661 } while (exception.retry);
5662 return err;
5663}
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5666{
5667 struct inode *inode = state->inode;
5668 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005669 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005670 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005672 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005674 struct nfs_lockt_res res = {
5675 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 };
5677 struct rpc_message msg = {
5678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5679 .rpc_argp = &arg,
5680 .rpc_resp = &res,
5681 .rpc_cred = state->owner->so_cred,
5682 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 struct nfs4_lock_state *lsp;
5684 int status;
5685
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005686 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005687 status = nfs4_set_lock_state(state, request);
5688 if (status != 0)
5689 goto out;
5690 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005691 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005692 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005693 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005694 switch (status) {
5695 case 0:
5696 request->fl_type = F_UNLCK;
5697 break;
5698 case -NFS4ERR_DENIED:
5699 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005701 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005702 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005703out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 return status;
5705}
5706
5707static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5708{
5709 struct nfs4_exception exception = { };
5710 int err;
5711
5712 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005713 err = _nfs4_proc_getlk(state, cmd, request);
5714 trace_nfs4_get_lock(request, state, cmd, err);
5715 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 &exception);
5717 } while (exception.retry);
5718 return err;
5719}
5720
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005721struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005722 struct nfs_locku_args arg;
5723 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005724 struct nfs4_lock_state *lsp;
5725 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005726 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005727 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005728 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005729};
5730
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005731static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5732 struct nfs_open_context *ctx,
5733 struct nfs4_lock_state *lsp,
5734 struct nfs_seqid *seqid)
5735{
5736 struct nfs4_unlockdata *p;
5737 struct inode *inode = lsp->ls_state->inode;
5738
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005739 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005740 if (p == NULL)
5741 return NULL;
5742 p->arg.fh = NFS_FH(inode);
5743 p->arg.fl = &p->fl;
5744 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005745 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005746 p->lsp = lsp;
5747 atomic_inc(&lsp->ls_count);
5748 /* Ensure we don't close file until we're done freeing locks! */
5749 p->ctx = get_nfs_open_context(ctx);
5750 memcpy(&p->fl, fl, sizeof(p->fl));
5751 p->server = NFS_SERVER(inode);
5752 return p;
5753}
5754
Trond Myklebust06f814a2006-01-03 09:55:07 +01005755static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005756{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005757 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005758 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005759 nfs4_put_lock_state(calldata->lsp);
5760 put_nfs_open_context(calldata->ctx);
5761 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005762}
5763
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005764static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005765{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005766 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005767
Trond Myklebust14516c32010-07-31 14:29:06 -04005768 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5769 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005770 switch (task->tk_status) {
5771 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005772 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005773 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005774 if (nfs4_update_lock_stateid(calldata->lsp,
5775 &calldata->res.stateid))
5776 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005777 case -NFS4ERR_ADMIN_REVOKED:
5778 case -NFS4ERR_EXPIRED:
5779 nfs4_free_revoked_stateid(calldata->server,
5780 &calldata->arg.stateid,
5781 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005782 case -NFS4ERR_BAD_STATEID:
5783 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005784 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005785 if (!nfs4_stateid_match(&calldata->arg.stateid,
5786 &calldata->lsp->ls_stateid))
5787 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005788 break;
5789 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005790 if (nfs4_async_handle_error(task, calldata->server,
5791 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005792 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005793 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005794 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005795}
5796
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005797static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005798{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005799 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005801 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005802 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005803 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005804 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005805 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005806 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005807 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005808 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005809 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005810 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005811 &calldata->res.seq_res,
5812 task) != 0)
5813 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005814 return;
5815out_no_action:
5816 task->tk_action = NULL;
5817out_wait:
5818 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005821static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005822 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005823 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005824 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005825};
5826
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005827static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5828 struct nfs_open_context *ctx,
5829 struct nfs4_lock_state *lsp,
5830 struct nfs_seqid *seqid)
5831{
5832 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005833 struct rpc_message msg = {
5834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5835 .rpc_cred = ctx->cred,
5836 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005837 struct rpc_task_setup task_setup_data = {
5838 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005839 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005840 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005841 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005842 .flags = RPC_TASK_ASYNC,
5843 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005844
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005845 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5846 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5847
Frank Filz137d6ac2007-07-09 15:32:29 -07005848 /* Ensure this is an unlock - when canceling a lock, the
5849 * canceled lock is passed in, and it won't be an unlock.
5850 */
5851 fl->fl_type = F_UNLCK;
5852
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005853 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5854 if (data == NULL) {
5855 nfs_free_seqid(seqid);
5856 return ERR_PTR(-ENOMEM);
5857 }
5858
Chuck Levera9c92d62013-08-09 12:48:18 -04005859 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005860 msg.rpc_argp = &data->arg;
5861 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005862 task_setup_data.callback_data = data;
5863 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005864}
5865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5867{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005868 struct inode *inode = state->inode;
5869 struct nfs4_state_owner *sp = state->owner;
5870 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005871 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005873 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005874 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005875 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005876 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
Trond Myklebust9b073572006-06-29 16:38:34 -04005878 status = nfs4_set_lock_state(state, request);
5879 /* Unlock _before_ we do the RPC call */
5880 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005881 /* Exclude nfs_delegation_claim_locks() */
5882 mutex_lock(&sp->so_delegreturn_mutex);
5883 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005884 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005885 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005886 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005887 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005888 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005889 }
5890 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005891 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005892 if (status != 0)
5893 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005894 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005895 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005896 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5897 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005898 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5899 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005900 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005901 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005902 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005903 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005904 status = PTR_ERR(task);
5905 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005906 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005907 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005908 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005909out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005910 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005911 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913}
5914
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005915struct nfs4_lockdata {
5916 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005917 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005918 struct nfs4_lock_state *lsp;
5919 struct nfs_open_context *ctx;
5920 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005921 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005922 int rpc_status;
5923 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005924 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005925};
5926
5927static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005928 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5929 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930{
5931 struct nfs4_lockdata *p;
5932 struct inode *inode = lsp->ls_state->inode;
5933 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005934 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005936 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005937 if (p == NULL)
5938 return NULL;
5939
5940 p->arg.fh = NFS_FH(inode);
5941 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005942 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005943 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005944 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005945 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5946 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005947 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005948 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005949 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005950 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005951 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005952 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005953 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005954 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005955 atomic_inc(&lsp->ls_count);
5956 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005957 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005958 memcpy(&p->fl, fl, sizeof(p->fl));
5959 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005960out_free_seqid:
5961 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005962out_free:
5963 kfree(p);
5964 return NULL;
5965}
5966
5967static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5968{
5969 struct nfs4_lockdata *data = calldata;
5970 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
Harvey Harrison3110ff82008-05-02 13:42:44 -07005972 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005973 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005974 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005975 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005976 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005977 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005978 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005979 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005980 nfs4_stateid_copy(&data->arg.open_stateid,
5981 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005982 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005983 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005984 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005985 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005986 nfs4_stateid_copy(&data->arg.lock_stateid,
5987 &data->lsp->ls_stateid);
5988 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005989 if (!nfs4_valid_open_stateid(state)) {
5990 data->rpc_status = -EBADF;
5991 task->tk_action = NULL;
5992 goto out_release_open_seqid;
5993 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005994 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005995 if (nfs4_setup_sequence(data->server,
5996 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005997 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005998 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005999 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006000out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006001 nfs_release_seqid(data->arg.open_seqid);
6002out_release_lock_seqid:
6003 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006004out_wait:
6005 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006006 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006007}
6008
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006009static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6010{
6011 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006012 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006013
Harvey Harrison3110ff82008-05-02 13:42:44 -07006014 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006015
Trond Myklebust14516c32010-07-31 14:29:06 -04006016 if (!nfs4_sequence_done(task, &data->res.seq_res))
6017 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006018
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006020 switch (task->tk_status) {
6021 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006022 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006023 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006024 if (data->arg.new_lock) {
6025 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006026 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006027 rpc_restart_call_prepare(task);
6028 break;
6029 }
6030 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006031 if (data->arg.new_lock_owner != 0) {
6032 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6033 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6034 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6035 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6036 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006037 break;
6038 case -NFS4ERR_BAD_STATEID:
6039 case -NFS4ERR_OLD_STATEID:
6040 case -NFS4ERR_STALE_STATEID:
6041 case -NFS4ERR_EXPIRED:
6042 if (data->arg.new_lock_owner != 0) {
6043 if (!nfs4_stateid_match(&data->arg.open_stateid,
6044 &lsp->ls_state->open_stateid))
6045 rpc_restart_call_prepare(task);
6046 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6047 &lsp->ls_stateid))
6048 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006049 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006050 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051}
6052
6053static void nfs4_lock_release(void *calldata)
6054{
6055 struct nfs4_lockdata *data = calldata;
6056
Harvey Harrison3110ff82008-05-02 13:42:44 -07006057 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006058 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006059 if (data->cancelled != 0) {
6060 struct rpc_task *task;
6061 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6062 data->arg.lock_seqid);
6063 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006064 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006065 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066 } else
6067 nfs_free_seqid(data->arg.lock_seqid);
6068 nfs4_put_lock_state(data->lsp);
6069 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006070 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006072 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006073}
6074
6075static const struct rpc_call_ops nfs4_lock_ops = {
6076 .rpc_call_prepare = nfs4_lock_prepare,
6077 .rpc_call_done = nfs4_lock_done,
6078 .rpc_release = nfs4_lock_release,
6079};
6080
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006081static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6082{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006083 switch (error) {
6084 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006085 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006086 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006087 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006088 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006089 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006090 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006091 break;
6092 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006093 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006094 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006095 };
6096}
6097
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006098static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099{
6100 struct nfs4_lockdata *data;
6101 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6104 .rpc_cred = state->owner->so_cred,
6105 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006106 struct rpc_task_setup task_setup_data = {
6107 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006108 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006109 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006110 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006111 .flags = RPC_TASK_ASYNC,
6112 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113 int ret;
6114
Harvey Harrison3110ff82008-05-02 13:42:44 -07006115 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006116 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006117 fl->fl_u.nfs4_fl.owner,
6118 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006119 if (data == NULL)
6120 return -ENOMEM;
6121 if (IS_SETLKW(cmd))
6122 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006123 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006124 msg.rpc_argp = &data->arg;
6125 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006126 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006127 if (recovery_type > NFS_LOCK_NEW) {
6128 if (recovery_type == NFS_LOCK_RECLAIM)
6129 data->arg.reclaim = NFS_LOCK_RECLAIM;
6130 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006131 } else
6132 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006133 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006134 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006136 ret = nfs4_wait_for_completion_rpc_task(task);
6137 if (ret == 0) {
6138 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006139 if (ret)
6140 nfs4_handle_setlk_error(data->server, data->lsp,
6141 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006142 } else
6143 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006144 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006145 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006146 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148}
6149
6150static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6151{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006152 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006153 struct nfs4_exception exception = {
6154 .inode = state->inode,
6155 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006156 int err;
6157
6158 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006159 /* Cache the lock if possible... */
6160 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6161 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006162 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006163 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006164 break;
6165 nfs4_handle_exception(server, err, &exception);
6166 } while (exception.retry);
6167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168}
6169
6170static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6171{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006172 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006173 struct nfs4_exception exception = {
6174 .inode = state->inode,
6175 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006176 int err;
6177
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006178 err = nfs4_set_lock_state(state, request);
6179 if (err != 0)
6180 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006181 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006182 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6183 return 0;
6184 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006185 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006186 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6187 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006188 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006189 switch (err) {
6190 default:
6191 goto out;
6192 case -NFS4ERR_GRACE:
6193 case -NFS4ERR_DELAY:
6194 nfs4_handle_exception(server, err, &exception);
6195 err = 0;
6196 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006197 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006198out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200}
6201
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006202#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006203static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6204{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006205 struct nfs4_lock_state *lsp;
6206 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006207
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006208 status = nfs4_set_lock_state(state, request);
6209 if (status != 0)
6210 return status;
6211 lsp = request->fl_u.nfs4_fl.owner;
6212 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6213 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6214 return 0;
6215 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006216 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006217}
6218#endif
6219
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6221{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006222 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006223 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006224 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006225 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Trond Myklebust01c3b862006-06-29 16:38:39 -04006227 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006228 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006229 if (status < 0)
6230 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006231 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006232 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006233 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006234 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006235 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006236 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006237 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006238 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006239 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006240 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006241 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006242 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006243 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006244 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006245out:
6246 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 return status;
6248}
6249
6250static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6251{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006252 struct nfs4_exception exception = {
6253 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006254 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 int err;
6257
6258 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006259 err = _nfs4_proc_setlk(state, cmd, request);
6260 if (err == -NFS4ERR_DENIED)
6261 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006263 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 } while (exception.retry);
6265 return err;
6266}
6267
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006268#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6269#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6270
6271static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006272nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6273 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006274{
6275 int status = -ERESTARTSYS;
6276 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6277
6278 while(!signalled()) {
6279 status = nfs4_proc_setlk(state, cmd, request);
6280 if ((status != -EAGAIN) || IS_SETLK(cmd))
6281 break;
6282 freezable_schedule_timeout_interruptible(timeout);
6283 timeout *= 2;
6284 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6285 status = -ERESTARTSYS;
6286 }
6287 return status;
6288}
6289
Jeff Laytona1d617d82016-09-17 18:17:39 -04006290#ifdef CONFIG_NFS_V4_1
6291struct nfs4_lock_waiter {
6292 struct task_struct *task;
6293 struct inode *inode;
6294 struct nfs_lowner *owner;
6295 bool notified;
6296};
6297
6298static int
6299nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6300{
6301 int ret;
6302 struct cb_notify_lock_args *cbnl = key;
6303 struct nfs4_lock_waiter *waiter = wait->private;
6304 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6305 *wowner = waiter->owner;
6306
6307 /* Only wake if the callback was for the same owner */
6308 if (lowner->clientid != wowner->clientid ||
6309 lowner->id != wowner->id ||
6310 lowner->s_dev != wowner->s_dev)
6311 return 0;
6312
6313 /* Make sure it's for the right inode */
6314 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6315 return 0;
6316
6317 waiter->notified = true;
6318
6319 /* override "private" so we can use default_wake_function */
6320 wait->private = waiter->task;
6321 ret = autoremove_wake_function(wait, mode, flags, key);
6322 wait->private = waiter;
6323 return ret;
6324}
6325
6326static int
6327nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6328{
6329 int status = -ERESTARTSYS;
6330 unsigned long flags;
6331 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6332 struct nfs_server *server = NFS_SERVER(state->inode);
6333 struct nfs_client *clp = server->nfs_client;
6334 wait_queue_head_t *q = &clp->cl_lock_waitq;
6335 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6336 .id = lsp->ls_seqid.owner_id,
6337 .s_dev = server->s_dev };
6338 struct nfs4_lock_waiter waiter = { .task = current,
6339 .inode = state->inode,
6340 .owner = &owner,
6341 .notified = false };
6342 wait_queue_t wait;
6343
6344 /* Don't bother with waitqueue if we don't expect a callback */
6345 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6346 return nfs4_retry_setlk_simple(state, cmd, request);
6347
6348 init_wait(&wait);
6349 wait.private = &waiter;
6350 wait.func = nfs4_wake_lock_waiter;
6351 add_wait_queue(q, &wait);
6352
6353 while(!signalled()) {
6354 status = nfs4_proc_setlk(state, cmd, request);
6355 if ((status != -EAGAIN) || IS_SETLK(cmd))
6356 break;
6357
6358 status = -ERESTARTSYS;
6359 spin_lock_irqsave(&q->lock, flags);
6360 if (waiter.notified) {
6361 spin_unlock_irqrestore(&q->lock, flags);
6362 continue;
6363 }
6364 set_current_state(TASK_INTERRUPTIBLE);
6365 spin_unlock_irqrestore(&q->lock, flags);
6366
6367 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6368 }
6369
6370 finish_wait(q, &wait);
6371 return status;
6372}
6373#else /* !CONFIG_NFS_V4_1 */
6374static inline int
6375nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6376{
6377 return nfs4_retry_setlk_simple(state, cmd, request);
6378}
6379#endif
6380
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381static int
6382nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6383{
6384 struct nfs_open_context *ctx;
6385 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 int status;
6387
6388 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006389 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 state = ctx->state;
6391
6392 if (request->fl_start < 0 || request->fl_end < 0)
6393 return -EINVAL;
6394
Trond Myklebustd9531262009-07-21 19:22:38 -04006395 if (IS_GETLK(cmd)) {
6396 if (state != NULL)
6397 return nfs4_proc_getlk(state, F_GETLK, request);
6398 return 0;
6399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
6401 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6402 return -EINVAL;
6403
Trond Myklebustd9531262009-07-21 19:22:38 -04006404 if (request->fl_type == F_UNLCK) {
6405 if (state != NULL)
6406 return nfs4_proc_unlck(state, cmd, request);
6407 return 0;
6408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
Trond Myklebustd9531262009-07-21 19:22:38 -04006410 if (state == NULL)
6411 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006412
6413 if ((request->fl_flags & FL_POSIX) &&
6414 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6415 return -ENOLCK;
6416
Trond Myklebust55725512012-04-18 12:48:35 -04006417 /*
6418 * Don't rely on the VFS having checked the file open mode,
6419 * since it won't do this for flock() locks.
6420 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006421 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006422 case F_RDLCK:
6423 if (!(filp->f_mode & FMODE_READ))
6424 return -EBADF;
6425 break;
6426 case F_WRLCK:
6427 if (!(filp->f_mode & FMODE_WRITE))
6428 return -EBADF;
6429 }
6430
Jeff Layton1ea67db2016-09-17 18:17:37 -04006431 status = nfs4_set_lock_state(state, request);
6432 if (status != 0)
6433 return status;
6434
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006435 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436}
6437
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006438int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006439{
6440 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006441 int err;
6442
6443 err = nfs4_set_lock_state(state, fl);
6444 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006445 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006446 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006447 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006448}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006449
Trond Myklebustcf470c32012-03-07 13:49:12 -05006450struct nfs_release_lockowner_data {
6451 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006452 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006453 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006454 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006455 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006456};
6457
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006458static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6459{
6460 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006461 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006462 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6463 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006464 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006465 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006466}
6467
6468static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6469{
6470 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006471 struct nfs_server *server = data->server;
6472
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006473 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006474
6475 switch (task->tk_status) {
6476 case 0:
6477 renew_lease(server, data->timestamp);
6478 break;
6479 case -NFS4ERR_STALE_CLIENTID:
6480 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006481 nfs4_schedule_lease_recovery(server->nfs_client);
6482 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006483 case -NFS4ERR_LEASE_MOVED:
6484 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006485 if (nfs4_async_handle_error(task, server,
6486 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006487 rpc_restart_call_prepare(task);
6488 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006489}
6490
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006491static void nfs4_release_lockowner_release(void *calldata)
6492{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006493 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006494 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006495 kfree(calldata);
6496}
6497
Trond Myklebust17280172012-03-11 13:11:00 -04006498static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006499 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6500 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006501 .rpc_release = nfs4_release_lockowner_release,
6502};
6503
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006504static void
6505nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006506{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006507 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006508 struct rpc_message msg = {
6509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6510 };
6511
6512 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006513 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006514
Trond Myklebustcf470c32012-03-07 13:49:12 -05006515 data = kmalloc(sizeof(*data), GFP_NOFS);
6516 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006517 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006518 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006519 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006520 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6521 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6522 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006523
Trond Myklebustcf470c32012-03-07 13:49:12 -05006524 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006525 msg.rpc_resp = &data->res;
6526 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006527 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006528}
6529
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006530#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6531
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006532static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006533 struct dentry *unused, struct inode *inode,
6534 const char *key, const void *buf,
6535 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006536{
Al Viro59301222016-05-27 10:19:30 -04006537 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006538}
6539
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006540static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006541 struct dentry *unused, struct inode *inode,
6542 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006543{
Al Virob2968212016-04-10 20:48:24 -04006544 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006545}
6546
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006547static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006548{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006549 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006550}
6551
David Quigleyc9bccef2013-05-22 12:50:45 -04006552#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006553
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006554static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006555 struct dentry *unused, struct inode *inode,
6556 const char *key, const void *buf,
6557 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006558{
6559 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006560 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006561
6562 return -EOPNOTSUPP;
6563}
6564
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006565static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006566 struct dentry *unused, struct inode *inode,
6567 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006568{
6569 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006570 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006571 return -EOPNOTSUPP;
6572}
6573
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006574static ssize_t
6575nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006576{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006577 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006578
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006579 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6580 len = security_inode_listsecurity(inode, list, list_len);
6581 if (list_len && len > list_len)
6582 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006583 }
6584 return len;
6585}
6586
6587static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6588 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006589 .get = nfs4_xattr_get_nfs4_label,
6590 .set = nfs4_xattr_set_nfs4_label,
6591};
David Quigleyc9bccef2013-05-22 12:50:45 -04006592
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006593#else
6594
6595static ssize_t
6596nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6597{
6598 return 0;
6599}
6600
6601#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006602
Andy Adamson533eb462011-06-13 18:25:56 -04006603/*
6604 * nfs_fhget will use either the mounted_on_fileid or the fileid
6605 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006606static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6607{
Andy Adamson533eb462011-06-13 18:25:56 -04006608 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6609 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6610 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006611 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006612 return;
6613
6614 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006615 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006616 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6617 fattr->nlink = 2;
6618}
6619
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006620static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6621 const struct qstr *name,
6622 struct nfs4_fs_locations *fs_locations,
6623 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006624{
6625 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006626 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006627 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006628 };
6629 struct nfs4_fs_locations_arg args = {
6630 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006631 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006632 .page = page,
6633 .bitmask = bitmask,
6634 };
Benny Halevy22958462009-04-01 09:22:02 -04006635 struct nfs4_fs_locations_res res = {
6636 .fs_locations = fs_locations,
6637 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006638 struct rpc_message msg = {
6639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6640 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006641 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006642 };
6643 int status;
6644
Harvey Harrison3110ff82008-05-02 13:42:44 -07006645 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006646
6647 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6648 * is not supported */
6649 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6650 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6651 else
6652 bitmask[0] |= FATTR4_WORD0_FILEID;
6653
Trond Myklebustc228fd32007-01-13 02:28:11 -05006654 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006655 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006656 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006657 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006658 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006659 return status;
6660}
6661
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006662int 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)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006666{
6667 struct nfs4_exception exception = { };
6668 int err;
6669 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006670 err = _nfs4_proc_fs_locations(client, dir, name,
6671 fs_locations, page);
6672 trace_nfs4_get_fs_locations(dir, name, err);
6673 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006674 &exception);
6675 } while (exception.retry);
6676 return err;
6677}
6678
Chuck Leverb03d7352013-10-17 14:12:50 -04006679/*
6680 * This operation also signals the server that this client is
6681 * performing migration recovery. The server can stop returning
6682 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6683 * appended to this compound to identify the client ID which is
6684 * performing recovery.
6685 */
6686static int _nfs40_proc_get_locations(struct inode *inode,
6687 struct nfs4_fs_locations *locations,
6688 struct page *page, struct rpc_cred *cred)
6689{
6690 struct nfs_server *server = NFS_SERVER(inode);
6691 struct rpc_clnt *clnt = server->client;
6692 u32 bitmask[2] = {
6693 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6694 };
6695 struct nfs4_fs_locations_arg args = {
6696 .clientid = server->nfs_client->cl_clientid,
6697 .fh = NFS_FH(inode),
6698 .page = page,
6699 .bitmask = bitmask,
6700 .migration = 1, /* skip LOOKUP */
6701 .renew = 1, /* append RENEW */
6702 };
6703 struct nfs4_fs_locations_res res = {
6704 .fs_locations = locations,
6705 .migration = 1,
6706 .renew = 1,
6707 };
6708 struct rpc_message msg = {
6709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6710 .rpc_argp = &args,
6711 .rpc_resp = &res,
6712 .rpc_cred = cred,
6713 };
6714 unsigned long now = jiffies;
6715 int status;
6716
6717 nfs_fattr_init(&locations->fattr);
6718 locations->server = server;
6719 locations->nlocations = 0;
6720
6721 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6722 nfs4_set_sequence_privileged(&args.seq_args);
6723 status = nfs4_call_sync_sequence(clnt, server, &msg,
6724 &args.seq_args, &res.seq_res);
6725 if (status)
6726 return status;
6727
6728 renew_lease(server, now);
6729 return 0;
6730}
6731
6732#ifdef CONFIG_NFS_V4_1
6733
6734/*
6735 * This operation also signals the server that this client is
6736 * performing migration recovery. The server can stop asserting
6737 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6738 * performing this operation is identified in the SEQUENCE
6739 * operation in this compound.
6740 *
6741 * When the client supports GETATTR(fs_locations_info), it can
6742 * be plumbed in here.
6743 */
6744static int _nfs41_proc_get_locations(struct inode *inode,
6745 struct nfs4_fs_locations *locations,
6746 struct page *page, struct rpc_cred *cred)
6747{
6748 struct nfs_server *server = NFS_SERVER(inode);
6749 struct rpc_clnt *clnt = server->client;
6750 u32 bitmask[2] = {
6751 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6752 };
6753 struct nfs4_fs_locations_arg args = {
6754 .fh = NFS_FH(inode),
6755 .page = page,
6756 .bitmask = bitmask,
6757 .migration = 1, /* skip LOOKUP */
6758 };
6759 struct nfs4_fs_locations_res res = {
6760 .fs_locations = locations,
6761 .migration = 1,
6762 };
6763 struct rpc_message msg = {
6764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6765 .rpc_argp = &args,
6766 .rpc_resp = &res,
6767 .rpc_cred = cred,
6768 };
6769 int status;
6770
6771 nfs_fattr_init(&locations->fattr);
6772 locations->server = server;
6773 locations->nlocations = 0;
6774
6775 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6776 nfs4_set_sequence_privileged(&args.seq_args);
6777 status = nfs4_call_sync_sequence(clnt, server, &msg,
6778 &args.seq_args, &res.seq_res);
6779 if (status == NFS4_OK &&
6780 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6781 status = -NFS4ERR_LEASE_MOVED;
6782 return status;
6783}
6784
6785#endif /* CONFIG_NFS_V4_1 */
6786
6787/**
6788 * nfs4_proc_get_locations - discover locations for a migrated FSID
6789 * @inode: inode on FSID that is migrating
6790 * @locations: result of query
6791 * @page: buffer
6792 * @cred: credential to use for this operation
6793 *
6794 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6795 * operation failed, or a negative errno if a local error occurred.
6796 *
6797 * On success, "locations" is filled in, but if the server has
6798 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6799 * asserted.
6800 *
6801 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6802 * from this client that require migration recovery.
6803 */
6804int nfs4_proc_get_locations(struct inode *inode,
6805 struct nfs4_fs_locations *locations,
6806 struct page *page, struct rpc_cred *cred)
6807{
6808 struct nfs_server *server = NFS_SERVER(inode);
6809 struct nfs_client *clp = server->nfs_client;
6810 const struct nfs4_mig_recovery_ops *ops =
6811 clp->cl_mvops->mig_recovery_ops;
6812 struct nfs4_exception exception = { };
6813 int status;
6814
6815 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6816 (unsigned long long)server->fsid.major,
6817 (unsigned long long)server->fsid.minor,
6818 clp->cl_hostname);
6819 nfs_display_fhandle(NFS_FH(inode), __func__);
6820
6821 do {
6822 status = ops->get_locations(inode, locations, page, cred);
6823 if (status != -NFS4ERR_DELAY)
6824 break;
6825 nfs4_handle_exception(server, status, &exception);
6826 } while (exception.retry);
6827 return status;
6828}
6829
Chuck Lever44c99932013-10-17 14:13:30 -04006830/*
6831 * This operation also signals the server that this client is
6832 * performing "lease moved" recovery. The server can stop
6833 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6834 * is appended to this compound to identify the client ID which is
6835 * performing recovery.
6836 */
6837static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6838{
6839 struct nfs_server *server = NFS_SERVER(inode);
6840 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6841 struct rpc_clnt *clnt = server->client;
6842 struct nfs4_fsid_present_arg args = {
6843 .fh = NFS_FH(inode),
6844 .clientid = clp->cl_clientid,
6845 .renew = 1, /* append RENEW */
6846 };
6847 struct nfs4_fsid_present_res res = {
6848 .renew = 1,
6849 };
6850 struct rpc_message msg = {
6851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6852 .rpc_argp = &args,
6853 .rpc_resp = &res,
6854 .rpc_cred = cred,
6855 };
6856 unsigned long now = jiffies;
6857 int status;
6858
6859 res.fh = nfs_alloc_fhandle();
6860 if (res.fh == NULL)
6861 return -ENOMEM;
6862
6863 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6864 nfs4_set_sequence_privileged(&args.seq_args);
6865 status = nfs4_call_sync_sequence(clnt, server, &msg,
6866 &args.seq_args, &res.seq_res);
6867 nfs_free_fhandle(res.fh);
6868 if (status)
6869 return status;
6870
6871 do_renew_lease(clp, now);
6872 return 0;
6873}
6874
6875#ifdef CONFIG_NFS_V4_1
6876
6877/*
6878 * This operation also signals the server that this client is
6879 * performing "lease moved" recovery. The server can stop asserting
6880 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6881 * this operation is identified in the SEQUENCE operation in this
6882 * compound.
6883 */
6884static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6885{
6886 struct nfs_server *server = NFS_SERVER(inode);
6887 struct rpc_clnt *clnt = server->client;
6888 struct nfs4_fsid_present_arg args = {
6889 .fh = NFS_FH(inode),
6890 };
6891 struct nfs4_fsid_present_res res = {
6892 };
6893 struct rpc_message msg = {
6894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6895 .rpc_argp = &args,
6896 .rpc_resp = &res,
6897 .rpc_cred = cred,
6898 };
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 == NFS4_OK &&
6911 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6912 status = -NFS4ERR_LEASE_MOVED;
6913 return status;
6914}
6915
6916#endif /* CONFIG_NFS_V4_1 */
6917
6918/**
6919 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6920 * @inode: inode on FSID to check
6921 * @cred: credential to use for this operation
6922 *
6923 * Server indicates whether the FSID is present, moved, or not
6924 * recognized. This operation is necessary to clear a LEASE_MOVED
6925 * condition for this client ID.
6926 *
6927 * Returns NFS4_OK if the FSID is present on this server,
6928 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6929 * NFS4ERR code if some error occurred on the server, or a
6930 * negative errno if a local failure occurred.
6931 */
6932int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6933{
6934 struct nfs_server *server = NFS_SERVER(inode);
6935 struct nfs_client *clp = server->nfs_client;
6936 const struct nfs4_mig_recovery_ops *ops =
6937 clp->cl_mvops->mig_recovery_ops;
6938 struct nfs4_exception exception = { };
6939 int status;
6940
6941 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6942 (unsigned long long)server->fsid.major,
6943 (unsigned long long)server->fsid.minor,
6944 clp->cl_hostname);
6945 nfs_display_fhandle(NFS_FH(inode), __func__);
6946
6947 do {
6948 status = ops->fsid_present(inode, cred);
6949 if (status != -NFS4ERR_DELAY)
6950 break;
6951 nfs4_handle_exception(server, status, &exception);
6952 } while (exception.retry);
6953 return status;
6954}
6955
Andy Adamson5ec16a82013-08-08 10:57:55 -04006956/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006957 * If 'use_integrity' is true and the state managment nfs_client
6958 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6959 * and the machine credential as per RFC3530bis and RFC5661 Security
6960 * Considerations sections. Otherwise, just use the user cred with the
6961 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006962 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006963static 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 +00006964{
6965 int status;
6966 struct nfs4_secinfo_arg args = {
6967 .dir_fh = NFS_FH(dir),
6968 .name = name,
6969 };
6970 struct nfs4_secinfo_res res = {
6971 .flavors = flavors,
6972 };
6973 struct rpc_message msg = {
6974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6975 .rpc_argp = &args,
6976 .rpc_resp = &res,
6977 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006978 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006979 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006980
6981 if (use_integrity) {
6982 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006983 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6984 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006985 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006986
6987 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006988
6989 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6990 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6991
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006992 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6993 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006994 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006995
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006996 if (cred)
6997 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006998
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006999 return status;
7000}
7001
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007002int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7003 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007004{
7005 struct nfs4_exception exception = { };
7006 int err;
7007 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007008 err = -NFS4ERR_WRONGSEC;
7009
7010 /* try to use integrity protection with machine cred */
7011 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7012 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7013
7014 /*
7015 * if unable to use integrity protection, or SECINFO with
7016 * integrity protection returns NFS4ERR_WRONGSEC (which is
7017 * disallowed by spec, but exists in deployed servers) use
7018 * the current filesystem's rpc_client and the user cred.
7019 */
7020 if (err == -NFS4ERR_WRONGSEC)
7021 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7022
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007023 trace_nfs4_secinfo(dir, name, err);
7024 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007025 &exception);
7026 } while (exception.retry);
7027 return err;
7028}
7029
Andy Adamson557134a2009-04-01 09:21:53 -04007030#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007031/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007032 * Check the exchange flags returned by the server for invalid flags, having
7033 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7034 * DS flags set.
7035 */
7036static int nfs4_check_cl_exchange_flags(u32 flags)
7037{
7038 if (flags & ~EXCHGID4_FLAG_MASK_R)
7039 goto out_inval;
7040 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7041 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7042 goto out_inval;
7043 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7044 goto out_inval;
7045 return NFS_OK;
7046out_inval:
7047 return -NFS4ERR_INVAL;
7048}
7049
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007050static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007051nfs41_same_server_scope(struct nfs41_server_scope *a,
7052 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007053{
7054 if (a->server_scope_sz == b->server_scope_sz &&
7055 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7056 return true;
7057
7058 return false;
7059}
7060
Andy Adamson02a95de2016-02-05 16:08:37 -05007061static void
7062nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7063{
7064}
7065
7066static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7067 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7068};
7069
Andy Adamson357f54d2010-12-14 10:11:57 -05007070/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007071 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007072 *
7073 * The 4.1 client currently uses the same TCP connection for the
7074 * fore and backchannel.
7075 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007076static
7077int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7078 struct rpc_xprt *xprt,
7079 struct nfs_client *clp,
7080 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007081{
7082 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007083 struct nfs41_bind_conn_to_session_args args = {
7084 .client = clp,
7085 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7086 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007087 struct nfs41_bind_conn_to_session_res res;
7088 struct rpc_message msg = {
7089 .rpc_proc =
7090 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007091 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007092 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007093 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007094 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007095 struct rpc_task_setup task_setup_data = {
7096 .rpc_client = clnt,
7097 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007098 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007099 .rpc_message = &msg,
7100 .flags = RPC_TASK_TIMEOUT,
7101 };
7102 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007103
7104 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007105
Trond Myklebust71a097c2015-02-18 09:27:18 -08007106 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7107 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7108 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007109
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007110 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7111 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7112 args.dir = NFS4_CDFC4_FORE;
7113
7114 task = rpc_run_task(&task_setup_data);
7115 if (!IS_ERR(task)) {
7116 status = task->tk_status;
7117 rpc_put_task(task);
7118 } else
7119 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007120 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007121 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007122 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007123 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7124 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7125 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007126 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007127 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007128 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007129 dprintk("NFS: %s: Unexpected direction from server\n",
7130 __func__);
7131 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007132 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007133 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007134 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007135 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7136 __func__);
7137 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007138 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007139 }
7140 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007141out:
7142 dprintk("<-- %s status= %d\n", __func__, status);
7143 return status;
7144}
7145
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007146struct rpc_bind_conn_calldata {
7147 struct nfs_client *clp;
7148 struct rpc_cred *cred;
7149};
7150
7151static int
7152nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7153 struct rpc_xprt *xprt,
7154 void *calldata)
7155{
7156 struct rpc_bind_conn_calldata *p = calldata;
7157
7158 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7159}
7160
7161int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7162{
7163 struct rpc_bind_conn_calldata data = {
7164 .clp = clp,
7165 .cred = cred,
7166 };
7167 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7168 nfs4_proc_bind_conn_to_session_callback, &data);
7169}
7170
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007171/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007172 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7173 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007174 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007175static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7176 .how = SP4_MACH_CRED,
7177 .enforce.u.words = {
7178 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7179 1 << (OP_EXCHANGE_ID - 32) |
7180 1 << (OP_CREATE_SESSION - 32) |
7181 1 << (OP_DESTROY_SESSION - 32) |
7182 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007183 },
7184 .allow.u.words = {
7185 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007186 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007187 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007188 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007189 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007190 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007191 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007192 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007193 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007194 1 << (OP_FREE_STATEID - 32) |
7195 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007196 }
7197};
7198
7199/*
7200 * Select the state protection mode for client `clp' given the server results
7201 * from exchange_id in `sp'.
7202 *
7203 * Returns 0 on success, negative errno otherwise.
7204 */
7205static int nfs4_sp4_select_mode(struct nfs_client *clp,
7206 struct nfs41_state_protection *sp)
7207{
7208 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7209 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7210 1 << (OP_EXCHANGE_ID - 32) |
7211 1 << (OP_CREATE_SESSION - 32) |
7212 1 << (OP_DESTROY_SESSION - 32) |
7213 1 << (OP_DESTROY_CLIENTID - 32)
7214 };
7215 unsigned int i;
7216
7217 if (sp->how == SP4_MACH_CRED) {
7218 /* Print state protect result */
7219 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7220 for (i = 0; i <= LAST_NFS4_OP; i++) {
7221 if (test_bit(i, sp->enforce.u.longs))
7222 dfprintk(MOUNT, " enforce op %d\n", i);
7223 if (test_bit(i, sp->allow.u.longs))
7224 dfprintk(MOUNT, " allow op %d\n", i);
7225 }
7226
7227 /* make sure nothing is on enforce list that isn't supported */
7228 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7229 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7230 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7231 return -EINVAL;
7232 }
7233 }
7234
7235 /*
7236 * Minimal mode - state operations are allowed to use machine
7237 * credential. Note this already happens by default, so the
7238 * client doesn't have to do anything more than the negotiation.
7239 *
7240 * NOTE: we don't care if EXCHANGE_ID is in the list -
7241 * we're already using the machine cred for exchange_id
7242 * and will never use a different cred.
7243 */
7244 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7245 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7246 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7247 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7248 dfprintk(MOUNT, "sp4_mach_cred:\n");
7249 dfprintk(MOUNT, " minimal mode enabled\n");
7250 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7251 } else {
7252 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7253 return -EINVAL;
7254 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007255
7256 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007257 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7258 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007259 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7260 dfprintk(MOUNT, " cleanup mode enabled\n");
7261 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7262 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007263
Andrew Elble99ade3c2015-12-02 09:39:51 -05007264 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7265 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7266 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7267 &clp->cl_sp4_flags);
7268 }
7269
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007270 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7271 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7272 dfprintk(MOUNT, " secinfo mode enabled\n");
7273 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7274 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007275
7276 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7277 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7278 dfprintk(MOUNT, " stateid mode enabled\n");
7279 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7280 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007281
7282 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7283 dfprintk(MOUNT, " write mode enabled\n");
7284 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7285 }
7286
7287 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7288 dfprintk(MOUNT, " commit mode enabled\n");
7289 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7290 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007291 }
7292
7293 return 0;
7294}
7295
Andy Adamson8d89bd72016-09-09 09:22:18 -04007296struct nfs41_exchange_id_data {
7297 struct nfs41_exchange_id_res res;
7298 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007299 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007300 int rpc_status;
7301};
7302
7303static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7304{
7305 struct nfs41_exchange_id_data *cdata =
7306 (struct nfs41_exchange_id_data *)data;
7307 struct nfs_client *clp = cdata->args.client;
7308 int status = task->tk_status;
7309
7310 trace_nfs4_exchange_id(clp, status);
7311
7312 if (status == 0)
7313 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007314
7315 if (cdata->xprt && status == 0) {
7316 status = nfs4_detect_session_trunking(clp, &cdata->res,
7317 cdata->xprt);
7318 goto out;
7319 }
7320
Andy Adamson8d89bd72016-09-09 09:22:18 -04007321 if (status == 0)
7322 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7323
7324 if (status == 0) {
7325 clp->cl_clientid = cdata->res.clientid;
7326 clp->cl_exchange_flags = cdata->res.flags;
7327 /* Client ID is not confirmed */
7328 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7329 clear_bit(NFS4_SESSION_ESTABLISHED,
7330 &clp->cl_session->session_state);
7331 clp->cl_seqid = cdata->res.seqid;
7332 }
7333
7334 kfree(clp->cl_serverowner);
7335 clp->cl_serverowner = cdata->res.server_owner;
7336 cdata->res.server_owner = NULL;
7337
7338 /* use the most recent implementation id */
7339 kfree(clp->cl_implid);
7340 clp->cl_implid = cdata->res.impl_id;
7341 cdata->res.impl_id = NULL;
7342
7343 if (clp->cl_serverscope != NULL &&
7344 !nfs41_same_server_scope(clp->cl_serverscope,
7345 cdata->res.server_scope)) {
7346 dprintk("%s: server_scope mismatch detected\n",
7347 __func__);
7348 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7349 kfree(clp->cl_serverscope);
7350 clp->cl_serverscope = NULL;
7351 }
7352
7353 if (clp->cl_serverscope == NULL) {
7354 clp->cl_serverscope = cdata->res.server_scope;
7355 cdata->res.server_scope = NULL;
7356 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007357 /* Save the EXCHANGE_ID verifier session trunk tests */
7358 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7359 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007360 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007361out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007362 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007363 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007364}
7365
7366static void nfs4_exchange_id_release(void *data)
7367{
7368 struct nfs41_exchange_id_data *cdata =
7369 (struct nfs41_exchange_id_data *)data;
7370
7371 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007372 if (cdata->xprt) {
7373 xprt_put(cdata->xprt);
7374 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7375 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007376 kfree(cdata->res.impl_id);
7377 kfree(cdata->res.server_scope);
7378 kfree(cdata->res.server_owner);
7379 kfree(cdata);
7380}
7381
7382static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7383 .rpc_call_done = nfs4_exchange_id_done,
7384 .rpc_release = nfs4_exchange_id_release,
7385};
7386
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007387/*
7388 * _nfs4_proc_exchange_id()
7389 *
7390 * Wrapper for EXCHANGE_ID operation.
7391 */
7392static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007393 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007394{
7395 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007396 struct rpc_message msg = {
7397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007398 .rpc_cred = cred,
7399 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007400 struct rpc_task_setup task_setup_data = {
7401 .rpc_client = clp->cl_rpcclient,
7402 .callback_ops = &nfs4_exchange_id_call_ops,
7403 .rpc_message = &msg,
7404 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7405 };
7406 struct nfs41_exchange_id_data *calldata;
7407 struct rpc_task *task;
7408 int status = -EIO;
7409
7410 if (!atomic_inc_not_zero(&clp->cl_count))
7411 goto out;
7412
7413 status = -ENOMEM;
7414 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7415 if (!calldata)
7416 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007417
Andy Adamsonad0849a2016-09-09 09:22:28 -04007418 if (!xprt)
7419 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007420
7421 status = nfs4_init_uniform_client_string(clp);
7422 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007423 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007424
7425 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007426 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007427 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007428
Andy Adamson8d89bd72016-09-09 09:22:18 -04007429 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7430 GFP_NOFS);
7431 status = -ENOMEM;
7432 if (unlikely(calldata->res.server_owner == NULL))
7433 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007434
Andy Adamson8d89bd72016-09-09 09:22:18 -04007435 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007436 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007437 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007438 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007439
Andy Adamson8d89bd72016-09-09 09:22:18 -04007440 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7441 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007442 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007443
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007444 switch (sp4_how) {
7445 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007447 break;
7448
7449 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007450 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007451 break;
7452
7453 default:
7454 /* unsupported! */
7455 WARN_ON_ONCE(1);
7456 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007457 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007458 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007459 if (xprt) {
7460 calldata->xprt = xprt;
7461 task_setup_data.rpc_xprt = xprt;
7462 task_setup_data.flags =
7463 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7464 calldata->args.verifier = &clp->cl_confirm;
7465 } else {
7466 calldata->args.verifier = &verifier;
7467 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007468 calldata->args.client = clp;
7469#ifdef CONFIG_NFS_V4_1_MIGRATION
7470 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7471 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7472 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7473#else
7474 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7475 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7476#endif
7477 msg.rpc_argp = &calldata->args;
7478 msg.rpc_resp = &calldata->res;
7479 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007480
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 task = rpc_run_task(&task_setup_data);
7482 if (IS_ERR(task)) {
7483 status = PTR_ERR(task);
7484 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007485 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007486
Andy Adamsonad0849a2016-09-09 09:22:28 -04007487 if (!xprt) {
7488 status = rpc_wait_for_completion_task(task);
7489 if (!status)
7490 status = calldata->rpc_status;
7491 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007493
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007495out:
Chuck Lever177313f2012-05-21 22:44:58 -04007496 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007497 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007498 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007499 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007500 clp->cl_implid->date.seconds,
7501 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007502 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007503 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007504
7505out_impl_id:
7506 kfree(calldata->res.impl_id);
7507out_server_scope:
7508 kfree(calldata->res.server_scope);
7509out_server_owner:
7510 kfree(calldata->res.server_owner);
7511out_calldata:
7512 kfree(calldata);
7513 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007514}
7515
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007516/*
7517 * nfs4_proc_exchange_id()
7518 *
7519 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7520 *
7521 * Since the clientid has expired, all compounds using sessions
7522 * associated with the stale clientid will be returning
7523 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7524 * be in some phase of session reset.
7525 *
7526 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7527 */
7528int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7529{
7530 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7531 int status;
7532
7533 /* try SP4_MACH_CRED if krb5i/p */
7534 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7535 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007536 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007537 if (!status)
7538 return 0;
7539 }
7540
7541 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007542 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007543}
7544
Andy Adamson04fa2c62016-09-09 09:22:29 -04007545/**
7546 * nfs4_test_session_trunk
7547 *
7548 * This is an add_xprt_test() test function called from
7549 * rpc_clnt_setup_test_and_add_xprt.
7550 *
7551 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7552 * and is dereferrenced in nfs4_exchange_id_release
7553 *
7554 * Upon success, add the new transport to the rpc_clnt
7555 *
7556 * @clnt: struct rpc_clnt to get new transport
7557 * @xprt: the rpc_xprt to test
7558 * @data: call data for _nfs4_proc_exchange_id.
7559 */
7560int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7561 void *data)
7562{
7563 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7564 u32 sp4_how;
7565
7566 dprintk("--> %s try %s\n", __func__,
7567 xprt->address_strings[RPC_DISPLAY_ADDR]);
7568
7569 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7570
7571 /* Test connection for session trunking. Async exchange_id call */
7572 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7573}
7574EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7575
Trond Myklebust66245532012-05-25 17:18:09 -04007576static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7577 struct rpc_cred *cred)
7578{
7579 struct rpc_message msg = {
7580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7581 .rpc_argp = clp,
7582 .rpc_cred = cred,
7583 };
7584 int status;
7585
7586 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007587 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007588 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007589 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007590 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7591 return status;
7592}
7593
7594static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7595 struct rpc_cred *cred)
7596{
7597 unsigned int loop;
7598 int ret;
7599
7600 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7601 ret = _nfs4_proc_destroy_clientid(clp, cred);
7602 switch (ret) {
7603 case -NFS4ERR_DELAY:
7604 case -NFS4ERR_CLIENTID_BUSY:
7605 ssleep(1);
7606 break;
7607 default:
7608 return ret;
7609 }
7610 }
7611 return 0;
7612}
7613
7614int nfs4_destroy_clientid(struct nfs_client *clp)
7615{
7616 struct rpc_cred *cred;
7617 int ret = 0;
7618
7619 if (clp->cl_mvops->minor_version < 1)
7620 goto out;
7621 if (clp->cl_exchange_flags == 0)
7622 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007623 if (clp->cl_preserve_clid)
7624 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007625 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007626 ret = nfs4_proc_destroy_clientid(clp, cred);
7627 if (cred)
7628 put_rpccred(cred);
7629 switch (ret) {
7630 case 0:
7631 case -NFS4ERR_STALE_CLIENTID:
7632 clp->cl_exchange_flags = 0;
7633 }
7634out:
7635 return ret;
7636}
7637
Andy Adamson2050f0c2009-04-01 09:22:30 -04007638struct nfs4_get_lease_time_data {
7639 struct nfs4_get_lease_time_args *args;
7640 struct nfs4_get_lease_time_res *res;
7641 struct nfs_client *clp;
7642};
7643
7644static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7645 void *calldata)
7646{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007647 struct nfs4_get_lease_time_data *data =
7648 (struct nfs4_get_lease_time_data *)calldata;
7649
7650 dprintk("--> %s\n", __func__);
7651 /* just setup sequence, do not trigger session recovery
7652 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007653 nfs41_setup_sequence(data->clp->cl_session,
7654 &data->args->la_seq_args,
7655 &data->res->lr_seq_res,
7656 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007657 dprintk("<-- %s\n", __func__);
7658}
7659
7660/*
7661 * Called from nfs4_state_manager thread for session setup, so don't recover
7662 * from sequence operation or clientid errors.
7663 */
7664static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7665{
7666 struct nfs4_get_lease_time_data *data =
7667 (struct nfs4_get_lease_time_data *)calldata;
7668
7669 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007670 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7671 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007672 switch (task->tk_status) {
7673 case -NFS4ERR_DELAY:
7674 case -NFS4ERR_GRACE:
7675 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7676 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7677 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007678 /* fall through */
7679 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007680 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007681 return;
7682 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007683 dprintk("<-- %s\n", __func__);
7684}
7685
Trond Myklebust17280172012-03-11 13:11:00 -04007686static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007687 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7688 .rpc_call_done = nfs4_get_lease_time_done,
7689};
7690
7691int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7692{
7693 struct rpc_task *task;
7694 struct nfs4_get_lease_time_args args;
7695 struct nfs4_get_lease_time_res res = {
7696 .lr_fsinfo = fsinfo,
7697 };
7698 struct nfs4_get_lease_time_data data = {
7699 .args = &args,
7700 .res = &res,
7701 .clp = clp,
7702 };
7703 struct rpc_message msg = {
7704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7705 .rpc_argp = &args,
7706 .rpc_resp = &res,
7707 };
7708 struct rpc_task_setup task_setup = {
7709 .rpc_client = clp->cl_rpcclient,
7710 .rpc_message = &msg,
7711 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007712 .callback_data = &data,
7713 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007714 };
7715 int status;
7716
Chuck Levera9c92d62013-08-09 12:48:18 -04007717 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007718 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007719 dprintk("--> %s\n", __func__);
7720 task = rpc_run_task(&task_setup);
7721
7722 if (IS_ERR(task))
7723 status = PTR_ERR(task);
7724 else {
7725 status = task->tk_status;
7726 rpc_put_task(task);
7727 }
7728 dprintk("<-- %s return %d\n", __func__, status);
7729
7730 return status;
7731}
7732
Andy Adamsonfc931582009-04-01 09:22:31 -04007733/*
7734 * Initialize the values to be used by the client in CREATE_SESSION
7735 * If nfs4_init_session set the fore channel request and response sizes,
7736 * use them.
7737 *
7738 * Set the back channel max_resp_sz_cached to zero to force the client to
7739 * always set csa_cachethis to FALSE because the current implementation
7740 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7741 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007742static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7743 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007744{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007745 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007746 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007747
Andy Adamson18aad3d2013-06-26 12:21:49 -04007748 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7749 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7750
Andy Adamsonfc931582009-04-01 09:22:31 -04007751 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007752 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7753 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007754 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007755 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007756
7757 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007758 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007759 __func__,
7760 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007761 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007762
7763 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007764 args->bc_attrs.max_rqst_sz = max_bc_payload;
7765 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007766 args->bc_attrs.max_resp_sz_cached = 0;
7767 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007768 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007769
7770 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7771 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7772 __func__,
7773 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7774 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7775 args->bc_attrs.max_reqs);
7776}
7777
Trond Myklebust79969dd2015-02-18 11:30:18 -08007778static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7779 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007780{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007781 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007782 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007783
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007784 if (rcvd->max_resp_sz > sent->max_resp_sz)
7785 return -EINVAL;
7786 /*
7787 * Our requested max_ops is the minimum we need; we're not
7788 * prepared to break up compounds into smaller pieces than that.
7789 * So, no point even trying to continue if the server won't
7790 * cooperate:
7791 */
7792 if (rcvd->max_ops < sent->max_ops)
7793 return -EINVAL;
7794 if (rcvd->max_reqs == 0)
7795 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007796 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7797 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007798 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007799}
7800
Trond Myklebust79969dd2015-02-18 11:30:18 -08007801static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7802 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007803{
7804 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007805 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007806
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007807 if (!(res->flags & SESSION4_BACK_CHAN))
7808 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007809 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7810 return -EINVAL;
7811 if (rcvd->max_resp_sz < sent->max_resp_sz)
7812 return -EINVAL;
7813 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7814 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007815 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007816 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007817 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007818 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007819out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007820 return 0;
7821}
Andy Adamson8d353012009-04-01 09:22:32 -04007822
Andy Adamson8d353012009-04-01 09:22:32 -04007823static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007824 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007825{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007826 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007827
Trond Myklebust79969dd2015-02-18 11:30:18 -08007828 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007829 if (ret)
7830 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007831 return nfs4_verify_back_channel_attrs(args, res);
7832}
7833
7834static void nfs4_update_session(struct nfs4_session *session,
7835 struct nfs41_create_session_res *res)
7836{
7837 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007838 /* Mark client id and session as being confirmed */
7839 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7840 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007841 session->flags = res->flags;
7842 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007843 if (res->flags & SESSION4_BACK_CHAN)
7844 memcpy(&session->bc_attrs, &res->bc_attrs,
7845 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007846}
7847
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007848static int _nfs4_proc_create_session(struct nfs_client *clp,
7849 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007850{
7851 struct nfs4_session *session = clp->cl_session;
7852 struct nfs41_create_session_args args = {
7853 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007854 .clientid = clp->cl_clientid,
7855 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007856 .cb_program = NFS4_CALLBACK,
7857 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007858 struct nfs41_create_session_res res;
7859
Andy Adamsonfc931582009-04-01 09:22:31 -04007860 struct rpc_message msg = {
7861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7862 .rpc_argp = &args,
7863 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007864 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007865 };
7866 int status;
7867
Chuck Lever6b26cc82016-05-02 14:40:40 -04007868 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007869 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007870
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007871 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007872 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007873
Trond Myklebustb519d402016-09-11 14:50:01 -04007874 switch (status) {
7875 case -NFS4ERR_STALE_CLIENTID:
7876 case -NFS4ERR_DELAY:
7877 case -ETIMEDOUT:
7878 case -EACCES:
7879 case -EAGAIN:
7880 goto out;
7881 };
7882
7883 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007884 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007885 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007886 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007887 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888 if (status)
7889 goto out;
7890 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007891 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007893 return status;
7894}
7895
7896/*
7897 * Issues a CREATE_SESSION operation to the server.
7898 * It is the responsibility of the caller to verify the session is
7899 * expired before calling this routine.
7900 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007901int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007902{
7903 int status;
7904 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007905 struct nfs4_session *session = clp->cl_session;
7906
7907 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7908
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007909 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007910 if (status)
7911 goto out;
7912
Andy Adamsonaacd5532011-11-09 13:58:21 -05007913 /* Init or reset the session slot tables */
7914 status = nfs4_setup_session_slot_tables(session);
7915 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007916 if (status)
7917 goto out;
7918
7919 ptr = (unsigned *)&session->sess_id.data[0];
7920 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7921 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007922out:
7923 dprintk("<-- %s\n", __func__);
7924 return status;
7925}
7926
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007927/*
7928 * Issue the over-the-wire RPC DESTROY_SESSION.
7929 * The caller must serialize access to this routine.
7930 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007931int nfs4_proc_destroy_session(struct nfs4_session *session,
7932 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007933{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007934 struct rpc_message msg = {
7935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7936 .rpc_argp = session,
7937 .rpc_cred = cred,
7938 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007939 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007940
7941 dprintk("--> nfs4_proc_destroy_session\n");
7942
7943 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007944 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7945 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007946
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007947 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007948 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007949
7950 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007951 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007952 "Session has been destroyed regardless...\n", status);
7953
7954 dprintk("<-- nfs4_proc_destroy_session\n");
7955 return status;
7956}
7957
Trond Myklebust7b38c362012-05-23 13:23:31 -04007958/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007959 * Renew the cl_session lease.
7960 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007961struct nfs4_sequence_data {
7962 struct nfs_client *clp;
7963 struct nfs4_sequence_args args;
7964 struct nfs4_sequence_res res;
7965};
7966
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007967static void nfs41_sequence_release(void *data)
7968{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007969 struct nfs4_sequence_data *calldata = data;
7970 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007971
Alexandros Batsakis71358402010-02-05 03:45:05 -08007972 if (atomic_read(&clp->cl_count) > 1)
7973 nfs4_schedule_state_renewal(clp);
7974 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007975 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007976}
7977
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007978static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7979{
7980 switch(task->tk_status) {
7981 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007982 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7983 return -EAGAIN;
7984 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007985 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007986 }
7987 return 0;
7988}
7989
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007990static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007991{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007992 struct nfs4_sequence_data *calldata = data;
7993 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007994
Trond Myklebust14516c32010-07-31 14:29:06 -04007995 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7996 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007997
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007998 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007999 if (task->tk_status < 0) {
8000 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008001 if (atomic_read(&clp->cl_count) == 1)
8002 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008003
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008004 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8005 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008006 return;
8007 }
8008 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008009 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008010out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008011 dprintk("<-- %s\n", __func__);
8012}
8013
8014static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8015{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008016 struct nfs4_sequence_data *calldata = data;
8017 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008018 struct nfs4_sequence_args *args;
8019 struct nfs4_sequence_res *res;
8020
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008021 args = task->tk_msg.rpc_argp;
8022 res = task->tk_msg.rpc_resp;
8023
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008024 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008025}
8026
8027static const struct rpc_call_ops nfs41_sequence_ops = {
8028 .rpc_call_done = nfs41_sequence_call_done,
8029 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008030 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008031};
8032
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008033static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8034 struct rpc_cred *cred,
8035 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008036{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008037 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008038 struct rpc_message msg = {
8039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8040 .rpc_cred = cred,
8041 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008042 struct rpc_task_setup task_setup_data = {
8043 .rpc_client = clp->cl_rpcclient,
8044 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008045 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008046 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008047 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008048
Alexandros Batsakis71358402010-02-05 03:45:05 -08008049 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008050 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008051 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008052 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008053 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008054 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008055 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008056 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008057 if (is_privileged)
8058 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008059 msg.rpc_argp = &calldata->args;
8060 msg.rpc_resp = &calldata->res;
8061 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008062 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008064 return rpc_run_task(&task_setup_data);
8065}
8066
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008067static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008068{
8069 struct rpc_task *task;
8070 int ret = 0;
8071
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008072 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008073 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008074 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008075 if (IS_ERR(task))
8076 ret = PTR_ERR(task);
8077 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008078 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008079 dprintk("<-- %s status=%d\n", __func__, ret);
8080 return ret;
8081}
8082
8083static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8084{
8085 struct rpc_task *task;
8086 int ret;
8087
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008088 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008089 if (IS_ERR(task)) {
8090 ret = PTR_ERR(task);
8091 goto out;
8092 }
8093 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008094 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008095 ret = task->tk_status;
8096 rpc_put_task(task);
8097out:
8098 dprintk("<-- %s status=%d\n", __func__, ret);
8099 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100}
8101
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008102struct nfs4_reclaim_complete_data {
8103 struct nfs_client *clp;
8104 struct nfs41_reclaim_complete_args arg;
8105 struct nfs41_reclaim_complete_res res;
8106};
8107
8108static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8109{
8110 struct nfs4_reclaim_complete_data *calldata = data;
8111
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008112 nfs41_setup_sequence(calldata->clp->cl_session,
8113 &calldata->arg.seq_args,
8114 &calldata->res.seq_res,
8115 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008116}
8117
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008118static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8119{
8120 switch(task->tk_status) {
8121 case 0:
8122 case -NFS4ERR_COMPLETE_ALREADY:
8123 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8124 break;
8125 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008126 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008127 /* fall through */
8128 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008129 return -EAGAIN;
8130 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008131 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008132 }
8133 return 0;
8134}
8135
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008136static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8137{
8138 struct nfs4_reclaim_complete_data *calldata = data;
8139 struct nfs_client *clp = calldata->clp;
8140 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8141
8142 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008143 if (!nfs41_sequence_done(task, res))
8144 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008145
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008146 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008147 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8148 rpc_restart_call_prepare(task);
8149 return;
8150 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008151 dprintk("<-- %s\n", __func__);
8152}
8153
8154static void nfs4_free_reclaim_complete_data(void *data)
8155{
8156 struct nfs4_reclaim_complete_data *calldata = data;
8157
8158 kfree(calldata);
8159}
8160
8161static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8162 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8163 .rpc_call_done = nfs4_reclaim_complete_done,
8164 .rpc_release = nfs4_free_reclaim_complete_data,
8165};
8166
8167/*
8168 * Issue a global reclaim complete.
8169 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008170static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8171 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008172{
8173 struct nfs4_reclaim_complete_data *calldata;
8174 struct rpc_task *task;
8175 struct rpc_message msg = {
8176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008177 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008178 };
8179 struct rpc_task_setup task_setup_data = {
8180 .rpc_client = clp->cl_rpcclient,
8181 .rpc_message = &msg,
8182 .callback_ops = &nfs4_reclaim_complete_call_ops,
8183 .flags = RPC_TASK_ASYNC,
8184 };
8185 int status = -ENOMEM;
8186
8187 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008188 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008189 if (calldata == NULL)
8190 goto out;
8191 calldata->clp = clp;
8192 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008193
Chuck Levera9c92d62013-08-09 12:48:18 -04008194 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008195 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008196 msg.rpc_argp = &calldata->arg;
8197 msg.rpc_resp = &calldata->res;
8198 task_setup_data.callback_data = calldata;
8199 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008200 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008201 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008202 goto out;
8203 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008204 status = nfs4_wait_for_completion_rpc_task(task);
8205 if (status == 0)
8206 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008207 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008208 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008209out:
8210 dprintk("<-- %s status=%d\n", __func__, status);
8211 return status;
8212}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008213
8214static void
8215nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8216{
8217 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008218 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008219 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008220
8221 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008222 nfs41_setup_sequence(session, &lgp->args.seq_args,
8223 &lgp->res.seq_res, task);
8224 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008225}
8226
8227static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8228{
8229 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008230
8231 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008232 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008233 dprintk("<-- %s\n", __func__);
8234}
8235
8236static int
8237nfs4_layoutget_handle_exception(struct rpc_task *task,
8238 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8239{
Trond Myklebustee314c22012-10-01 17:25:48 -07008240 struct inode *inode = lgp->args.inode;
8241 struct nfs_server *server = NFS_SERVER(inode);
8242 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008243 int nfs4err = task->tk_status;
8244 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008245 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008246
Boaz Harroshed7e5422014-01-22 20:34:54 +02008247 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008248
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008249 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008250 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008251 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008252
8253 /*
8254 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8255 * on the file. set tk_status to -ENODATA to tell upper layer to
8256 * retry go inband.
8257 */
8258 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008259 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008260 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008261 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008262 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8263 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8264 */
8265 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008266 status = -EOVERFLOW;
8267 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008268 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008269 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008270 * (or clients) writing to the same RAID stripe except when
8271 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008272 *
8273 * Treat it like we would RECALLCONFLICT -- we retry for a little
8274 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008275 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008276 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008277 if (lgp->args.minlength == 0) {
8278 status = -EOVERFLOW;
8279 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008280 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008281 status = -EBUSY;
8282 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008283 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008284 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008285 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008286 case -NFS4ERR_DELEG_REVOKED:
8287 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008288 case -NFS4ERR_EXPIRED:
8289 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008290 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008291 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008292 lo = NFS_I(inode)->layout;
8293 /* If the open stateid was bad, then recover it. */
8294 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8295 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008296 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008297 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008298 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008299 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008300 break;
8301 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008302
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008303 /*
8304 * Mark the bad layout state as invalid, then retry
8305 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008306 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008307 spin_unlock(&inode->i_lock);
8308 pnfs_free_lseg_list(&head);
8309 status = -EAGAIN;
8310 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008311 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008312
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008313 err = nfs4_handle_exception(server, nfs4err, exception);
8314 if (!status) {
8315 if (exception->retry)
8316 status = -EAGAIN;
8317 else
8318 status = err;
8319 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008320out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008321 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008322 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008323}
8324
Idan Kedar85541162012-08-02 11:47:10 +03008325static size_t max_response_pages(struct nfs_server *server)
8326{
8327 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8328 return nfs_page_array_len(0, max_resp_sz);
8329}
8330
8331static void nfs4_free_pages(struct page **pages, size_t size)
8332{
8333 int i;
8334
8335 if (!pages)
8336 return;
8337
8338 for (i = 0; i < size; i++) {
8339 if (!pages[i])
8340 break;
8341 __free_page(pages[i]);
8342 }
8343 kfree(pages);
8344}
8345
8346static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8347{
8348 struct page **pages;
8349 int i;
8350
8351 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8352 if (!pages) {
8353 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8354 return NULL;
8355 }
8356
8357 for (i = 0; i < size; i++) {
8358 pages[i] = alloc_page(gfp_flags);
8359 if (!pages[i]) {
8360 dprintk("%s: failed to allocate page\n", __func__);
8361 nfs4_free_pages(pages, size);
8362 return NULL;
8363 }
8364 }
8365
8366 return pages;
8367}
8368
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008369static void nfs4_layoutget_release(void *calldata)
8370{
8371 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008372 struct inode *inode = lgp->args.inode;
8373 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008374 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008375
8376 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008377 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008378 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008379 put_nfs_open_context(lgp->args.ctx);
8380 kfree(calldata);
8381 dprintk("<-- %s\n", __func__);
8382}
8383
8384static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8385 .rpc_call_prepare = nfs4_layoutget_prepare,
8386 .rpc_call_done = nfs4_layoutget_done,
8387 .rpc_release = nfs4_layoutget_release,
8388};
8389
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008390struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008391nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008392{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008393 struct inode *inode = lgp->args.inode;
8394 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008395 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396 struct rpc_task *task;
8397 struct rpc_message msg = {
8398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8399 .rpc_argp = &lgp->args,
8400 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008401 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008402 };
8403 struct rpc_task_setup task_setup_data = {
8404 .rpc_client = server->client,
8405 .rpc_message = &msg,
8406 .callback_ops = &nfs4_layoutget_call_ops,
8407 .callback_data = lgp,
8408 .flags = RPC_TASK_ASYNC,
8409 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008410 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008411 struct nfs4_exception exception = {
8412 .inode = inode,
8413 .timeout = *timeout,
8414 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008415 int status = 0;
8416
8417 dprintk("--> %s\n", __func__);
8418
Peng Tao4bd5a982014-11-17 11:05:17 +08008419 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8420 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8421
Idan Kedar85541162012-08-02 11:47:10 +03008422 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8423 if (!lgp->args.layout.pages) {
8424 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008425 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008426 }
8427 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8428
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008429 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008430 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008431 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008432
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008433 task = rpc_run_task(&task_setup_data);
8434 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008435 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008436 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008437 if (status == 0) {
8438 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8439 *timeout = exception.timeout;
8440 }
8441
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008442 trace_nfs4_layoutget(lgp->args.ctx,
8443 &lgp->args.range,
8444 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008445 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008446 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008447
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008448 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8449 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008450 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008451 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008452 rpc_put_task(task);
8453 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008454 if (status)
8455 return ERR_PTR(status);
8456 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457}
8458
Benny Halevycbe82602011-05-22 19:52:37 +03008459static void
8460nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8461{
8462 struct nfs4_layoutreturn *lrp = calldata;
8463
8464 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008465 nfs41_setup_sequence(lrp->clp->cl_session,
8466 &lrp->args.seq_args,
8467 &lrp->res.seq_res,
8468 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008469}
8470
8471static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8472{
8473 struct nfs4_layoutreturn *lrp = calldata;
8474 struct nfs_server *server;
8475
8476 dprintk("--> %s\n", __func__);
8477
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008478 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008479 return;
8480
8481 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008482 switch (task->tk_status) {
8483 default:
8484 task->tk_status = 0;
8485 case 0:
8486 break;
8487 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008488 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008489 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008490 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008491 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008492 return;
8493 }
Benny Halevycbe82602011-05-22 19:52:37 +03008494 dprintk("<-- %s\n", __func__);
8495}
8496
8497static void nfs4_layoutreturn_release(void *calldata)
8498{
8499 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008500 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008501 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008502
8503 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008504 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008505 if (lrp->res.lrs_present) {
8506 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8507 &lrp->args.range,
8508 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008509 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008510 } else
8511 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008512 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008513 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008514 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008515 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008516 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008517 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008518 kfree(calldata);
8519 dprintk("<-- %s\n", __func__);
8520}
8521
8522static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8523 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8524 .rpc_call_done = nfs4_layoutreturn_done,
8525 .rpc_release = nfs4_layoutreturn_release,
8526};
8527
Peng Tao6c166052014-11-17 09:30:40 +08008528int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008529{
8530 struct rpc_task *task;
8531 struct rpc_message msg = {
8532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8533 .rpc_argp = &lrp->args,
8534 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008535 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008536 };
8537 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008538 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008539 .rpc_message = &msg,
8540 .callback_ops = &nfs4_layoutreturn_call_ops,
8541 .callback_data = lrp,
8542 };
Peng Tao6c166052014-11-17 09:30:40 +08008543 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008544
Andrew Elble99ade3c2015-12-02 09:39:51 -05008545 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8546 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8547 &task_setup_data.rpc_client, &msg);
8548
Benny Halevycbe82602011-05-22 19:52:37 +03008549 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008550 if (!sync) {
8551 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8552 if (!lrp->inode) {
8553 nfs4_layoutreturn_release(lrp);
8554 return -EAGAIN;
8555 }
8556 task_setup_data.flags |= RPC_TASK_ASYNC;
8557 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008558 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008559 task = rpc_run_task(&task_setup_data);
8560 if (IS_ERR(task))
8561 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008562 if (sync)
8563 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008564 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008565 dprintk("<-- %s status=%d\n", __func__, status);
8566 rpc_put_task(task);
8567 return status;
8568}
8569
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008570static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008571_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8572 struct pnfs_device *pdev,
8573 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008574{
8575 struct nfs4_getdeviceinfo_args args = {
8576 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008577 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8578 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008579 };
8580 struct nfs4_getdeviceinfo_res res = {
8581 .pdev = pdev,
8582 };
8583 struct rpc_message msg = {
8584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8585 .rpc_argp = &args,
8586 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008587 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008588 };
8589 int status;
8590
8591 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008592 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008593 if (res.notification & ~args.notify_types)
8594 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008595 if (res.notification != args.notify_types)
8596 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008597
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008598 dprintk("<-- %s status=%d\n", __func__, status);
8599
8600 return status;
8601}
8602
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008603int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8604 struct pnfs_device *pdev,
8605 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008606{
8607 struct nfs4_exception exception = { };
8608 int err;
8609
8610 do {
8611 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008612 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008613 &exception);
8614 } while (exception.retry);
8615 return err;
8616}
8617EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8618
Andy Adamson863a3c62011-03-23 13:27:54 +00008619static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8620{
8621 struct nfs4_layoutcommit_data *data = calldata;
8622 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008623 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008624
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008625 nfs41_setup_sequence(session,
8626 &data->args.seq_args,
8627 &data->res.seq_res,
8628 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008629}
8630
8631static void
8632nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8633{
8634 struct nfs4_layoutcommit_data *data = calldata;
8635 struct nfs_server *server = NFS_SERVER(data->args.inode);
8636
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008637 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008638 return;
8639
8640 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008641 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8642 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8643 case -NFS4ERR_BADLAYOUT: /* no layout */
8644 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008645 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008646 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008647 break;
8648 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008649 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008650 rpc_restart_call_prepare(task);
8651 return;
8652 }
8653 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008654}
8655
8656static void nfs4_layoutcommit_release(void *calldata)
8657{
8658 struct nfs4_layoutcommit_data *data = calldata;
8659
Andy Adamsondb29c082011-07-30 20:52:38 -04008660 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008661 nfs_post_op_update_inode_force_wcc(data->args.inode,
8662 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008663 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008664 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008665 kfree(data);
8666}
8667
8668static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8669 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8670 .rpc_call_done = nfs4_layoutcommit_done,
8671 .rpc_release = nfs4_layoutcommit_release,
8672};
8673
8674int
Andy Adamsonef311532011-03-12 02:58:10 -05008675nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008676{
8677 struct rpc_message msg = {
8678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8679 .rpc_argp = &data->args,
8680 .rpc_resp = &data->res,
8681 .rpc_cred = data->cred,
8682 };
8683 struct rpc_task_setup task_setup_data = {
8684 .task = &data->task,
8685 .rpc_client = NFS_CLIENT(data->args.inode),
8686 .rpc_message = &msg,
8687 .callback_ops = &nfs4_layoutcommit_ops,
8688 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008689 };
8690 struct rpc_task *task;
8691 int status = 0;
8692
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008693 dprintk("NFS: initiating layoutcommit call. sync %d "
8694 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008695 data->args.lastbytewritten,
8696 data->args.inode->i_ino);
8697
Trond Myklebust472e2592015-02-05 16:50:30 -05008698 if (!sync) {
8699 data->inode = nfs_igrab_and_active(data->args.inode);
8700 if (data->inode == NULL) {
8701 nfs4_layoutcommit_release(data);
8702 return -EAGAIN;
8703 }
8704 task_setup_data.flags = RPC_TASK_ASYNC;
8705 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008706 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008707 task = rpc_run_task(&task_setup_data);
8708 if (IS_ERR(task))
8709 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008710 if (sync)
8711 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008712 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008713 dprintk("%s: status %d\n", __func__, status);
8714 rpc_put_task(task);
8715 return status;
8716}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008717
Andy Adamson97431202013-08-08 10:57:56 -04008718/**
8719 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8720 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8721 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008722static int
8723_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008724 struct nfs_fsinfo *info,
8725 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008726{
8727 struct nfs41_secinfo_no_name_args args = {
8728 .style = SECINFO_STYLE_CURRENT_FH,
8729 };
8730 struct nfs4_secinfo_res res = {
8731 .flavors = flavors,
8732 };
8733 struct rpc_message msg = {
8734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8735 .rpc_argp = &args,
8736 .rpc_resp = &res,
8737 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008738 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008739 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008740 int status;
8741
8742 if (use_integrity) {
8743 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008744 cred = nfs4_get_clid_cred(server->nfs_client);
8745 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008746 }
8747
8748 dprintk("--> %s\n", __func__);
8749 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8750 &res.seq_res, 0);
8751 dprintk("<-- %s status=%d\n", __func__, status);
8752
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008753 if (cred)
8754 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008755
8756 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008757}
8758
8759static int
8760nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8761 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8762{
8763 struct nfs4_exception exception = { };
8764 int err;
8765 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008766 /* first try using integrity protection */
8767 err = -NFS4ERR_WRONGSEC;
8768
8769 /* try to use integrity protection with machine cred */
8770 if (_nfs4_is_integrity_protected(server->nfs_client))
8771 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8772 flavors, true);
8773
8774 /*
8775 * if unable to use integrity protection, or SECINFO with
8776 * integrity protection returns NFS4ERR_WRONGSEC (which is
8777 * disallowed by spec, but exists in deployed servers) use
8778 * the current filesystem's rpc_client and the user cred.
8779 */
8780 if (err == -NFS4ERR_WRONGSEC)
8781 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8782 flavors, false);
8783
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008784 switch (err) {
8785 case 0:
8786 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008787 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008788 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008789 default:
8790 err = nfs4_handle_exception(server, err, &exception);
8791 }
8792 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008793out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008794 return err;
8795}
8796
8797static int
8798nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8799 struct nfs_fsinfo *info)
8800{
8801 int err;
8802 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008803 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008804 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008805 struct nfs4_secinfo4 *secinfo;
8806 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008807
8808 page = alloc_page(GFP_KERNEL);
8809 if (!page) {
8810 err = -ENOMEM;
8811 goto out;
8812 }
8813
8814 flavors = page_address(page);
8815 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8816
8817 /*
8818 * Fall back on "guess and check" method if
8819 * the server doesn't support SECINFO_NO_NAME
8820 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008821 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008822 err = nfs4_find_root_sec(server, fhandle, info);
8823 goto out_freepage;
8824 }
8825 if (err)
8826 goto out_freepage;
8827
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008828 for (i = 0; i < flavors->num_flavors; i++) {
8829 secinfo = &flavors->flavors[i];
8830
8831 switch (secinfo->flavor) {
8832 case RPC_AUTH_NULL:
8833 case RPC_AUTH_UNIX:
8834 case RPC_AUTH_GSS:
8835 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8836 &secinfo->flavor_info);
8837 break;
8838 default:
8839 flavor = RPC_AUTH_MAXFLAVOR;
8840 break;
8841 }
8842
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008843 if (!nfs_auth_info_match(&server->auth_info, flavor))
8844 flavor = RPC_AUTH_MAXFLAVOR;
8845
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008846 if (flavor != RPC_AUTH_MAXFLAVOR) {
8847 err = nfs4_lookup_root_sec(server, fhandle,
8848 info, flavor);
8849 if (!err)
8850 break;
8851 }
8852 }
8853
8854 if (flavor == RPC_AUTH_MAXFLAVOR)
8855 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008856
8857out_freepage:
8858 put_page(page);
8859 if (err == -EACCES)
8860 return -EPERM;
8861out:
8862 return err;
8863}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008864
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008865static int _nfs41_test_stateid(struct nfs_server *server,
8866 nfs4_stateid *stateid,
8867 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008868{
8869 int status;
8870 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008871 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008872 };
8873 struct nfs41_test_stateid_res res;
8874 struct rpc_message msg = {
8875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8876 .rpc_argp = &args,
8877 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008878 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008879 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008880 struct rpc_clnt *rpc_client = server->client;
8881
8882 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8883 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008884
Chuck Lever38527b12012-07-11 16:30:23 -04008885 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008886 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008887 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008888 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008889 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008890 if (status != NFS_OK) {
8891 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008892 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008893 }
8894 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008895 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008896}
8897
Trond Myklebust43912bb2016-09-22 13:38:56 -04008898static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8899 int err, struct nfs4_exception *exception)
8900{
8901 exception->retry = 0;
8902 switch(err) {
8903 case -NFS4ERR_DELAY:
8904 nfs4_handle_exception(server, err, exception);
8905 break;
8906 case -NFS4ERR_BADSESSION:
8907 case -NFS4ERR_BADSLOT:
8908 case -NFS4ERR_BAD_HIGH_SLOT:
8909 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8910 case -NFS4ERR_DEADSESSION:
8911 nfs4_do_handle_exception(server, err, exception);
8912 }
8913}
8914
Chuck Lever38527b12012-07-11 16:30:23 -04008915/**
8916 * nfs41_test_stateid - perform a TEST_STATEID operation
8917 *
8918 * @server: server / transport on which to perform the operation
8919 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008920 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008921 *
8922 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8923 * Otherwise a negative NFS4ERR value is returned if the operation
8924 * failed or the state ID is not currently valid.
8925 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008926static int nfs41_test_stateid(struct nfs_server *server,
8927 nfs4_stateid *stateid,
8928 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008929{
8930 struct nfs4_exception exception = { };
8931 int err;
8932 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008933 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008934 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008935 } while (exception.retry);
8936 return err;
8937}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008938
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008939struct nfs_free_stateid_data {
8940 struct nfs_server *server;
8941 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008942 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008943};
8944
8945static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8946{
8947 struct nfs_free_stateid_data *data = calldata;
8948 nfs41_setup_sequence(nfs4_get_session(data->server),
8949 &data->args.seq_args,
8950 &data->res.seq_res,
8951 task);
8952}
8953
8954static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8955{
8956 struct nfs_free_stateid_data *data = calldata;
8957
8958 nfs41_sequence_done(task, &data->res.seq_res);
8959
8960 switch (task->tk_status) {
8961 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008962 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008963 rpc_restart_call_prepare(task);
8964 }
8965}
8966
8967static void nfs41_free_stateid_release(void *calldata)
8968{
8969 kfree(calldata);
8970}
8971
Trond Myklebust17f26b12013-08-21 15:48:42 -04008972static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008973 .rpc_call_prepare = nfs41_free_stateid_prepare,
8974 .rpc_call_done = nfs41_free_stateid_done,
8975 .rpc_release = nfs41_free_stateid_release,
8976};
8977
8978static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04008979 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008980 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008981 bool privileged)
8982{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008983 struct rpc_message msg = {
8984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008985 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008986 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008987 struct rpc_task_setup task_setup = {
8988 .rpc_client = server->client,
8989 .rpc_message = &msg,
8990 .callback_ops = &nfs41_free_stateid_ops,
8991 .flags = RPC_TASK_ASYNC,
8992 };
8993 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008994
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008995 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8996 &task_setup.rpc_client, &msg);
8997
Chuck Lever38527b12012-07-11 16:30:23 -04008998 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008999 data = kmalloc(sizeof(*data), GFP_NOFS);
9000 if (!data)
9001 return ERR_PTR(-ENOMEM);
9002 data->server = server;
9003 nfs4_stateid_copy(&data->args.stateid, stateid);
9004
9005 task_setup.callback_data = data;
9006
9007 msg.rpc_argp = &data->args;
9008 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04009009 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009010 if (privileged)
9011 nfs4_set_sequence_privileged(&data->args.seq_args);
9012
9013 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009014}
9015
Chuck Lever38527b12012-07-11 16:30:23 -04009016/**
9017 * nfs41_free_stateid - perform a FREE_STATEID operation
9018 *
9019 * @server: server / transport on which to perform the operation
9020 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009021 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009022 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009023 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009024 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009025 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009026static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009027 const nfs4_stateid *stateid,
9028 struct rpc_cred *cred,
9029 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009030{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009031 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009032
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009033 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009034 if (IS_ERR(task))
9035 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009036 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009037 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009038}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009039
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009040static void
9041nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009042{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009043 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009044
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009045 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009046 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009047}
9048
Trond Myklebust36281ca2012-03-04 18:13:56 -05009049static bool nfs41_match_stateid(const nfs4_stateid *s1,
9050 const nfs4_stateid *s2)
9051{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009052 if (s1->type != s2->type)
9053 return false;
9054
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009055 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009056 return false;
9057
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009058 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009059 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009060 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009061 return true;
9062
9063 return false;
9064}
9065
Andy Adamson557134a2009-04-01 09:21:53 -04009066#endif /* CONFIG_NFS_V4_1 */
9067
Trond Myklebust36281ca2012-03-04 18:13:56 -05009068static bool nfs4_match_stateid(const nfs4_stateid *s1,
9069 const nfs4_stateid *s2)
9070{
Trond Myklebustf597c532012-03-04 18:13:56 -05009071 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009072}
9073
9074
Trond Myklebust17280172012-03-11 13:11:00 -04009075static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009076 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009077 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 .recover_open = nfs4_open_reclaim,
9079 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009080 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009081 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082};
9083
Andy Adamson591d71c2009-04-01 09:22:47 -04009084#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009085static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009086 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9087 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9088 .recover_open = nfs4_open_reclaim,
9089 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009090 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009091 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009092 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009093};
9094#endif /* CONFIG_NFS_V4_1 */
9095
Trond Myklebust17280172012-03-11 13:11:00 -04009096static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009097 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009098 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009099 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009101 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102};
9103
Andy Adamson591d71c2009-04-01 09:22:47 -04009104#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009105static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009106 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9107 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009108 .recover_open = nfs41_open_expired,
9109 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009110 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009111};
9112#endif /* CONFIG_NFS_V4_1 */
9113
Trond Myklebust17280172012-03-11 13:11:00 -04009114static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009115 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009116 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009117 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009118};
9119
9120#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009121static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009122 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009123 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009124 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009125};
9126#endif
9127
Chuck Leverec011fe2013-10-17 14:12:39 -04009128static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009129 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009130 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009131};
9132
9133#if defined(CONFIG_NFS_V4_1)
9134static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009135 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009136 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009137};
9138#endif /* CONFIG_NFS_V4_1 */
9139
Trond Myklebust97dc1352010-06-16 09:52:26 -04009140static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9141 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009142 .init_caps = NFS_CAP_READDIRPLUS
9143 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009144 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009145 .init_client = nfs40_init_client,
9146 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009147 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009148 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009149 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009150 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009151 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009152 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009153 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9154 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9155 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009156 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009157};
9158
9159#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009160static struct nfs_seqid *
9161nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9162{
9163 return NULL;
9164}
9165
Trond Myklebust97dc1352010-06-16 09:52:26 -04009166static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9167 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009168 .init_caps = NFS_CAP_READDIRPLUS
9169 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009170 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009171 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009172 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009173 .init_client = nfs41_init_client,
9174 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009175 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009176 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009177 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009178 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009179 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009180 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009181 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009182 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9183 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9184 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009185 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009186};
9187#endif
9188
Steve Dickson42c2c422013-05-22 12:50:38 -04009189#if defined(CONFIG_NFS_V4_2)
9190static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9191 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009192 .init_caps = NFS_CAP_READDIRPLUS
9193 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009194 | NFS_CAP_POSIX_LOCK
9195 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009196 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009197 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009198 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009199 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009200 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009201 | NFS_CAP_LAYOUTSTATS
9202 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009203 .init_client = nfs41_init_client,
9204 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009205 .match_stateid = nfs41_match_stateid,
9206 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009207 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009208 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009209 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009210 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009211 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009212 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9213 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9214 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009215 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009216};
9217#endif
9218
Trond Myklebust97dc1352010-06-16 09:52:26 -04009219const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9220 [0] = &nfs_v4_0_minor_ops,
9221#if defined(CONFIG_NFS_V4_1)
9222 [1] = &nfs_v4_1_minor_ops,
9223#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009224#if defined(CONFIG_NFS_V4_2)
9225 [2] = &nfs_v4_2_minor_ops,
9226#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009227};
9228
Trond Myklebust13997822016-07-24 17:10:52 -04009229static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009230{
9231 ssize_t error, error2;
9232
9233 error = generic_listxattr(dentry, list, size);
9234 if (error < 0)
9235 return error;
9236 if (list) {
9237 list += error;
9238 size -= error;
9239 }
9240
9241 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9242 if (error2 < 0)
9243 return error2;
9244 return error + error2;
9245}
9246
Trond Myklebust17f26b12013-08-21 15:48:42 -04009247static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009248 .create = nfs_create,
9249 .lookup = nfs_lookup,
9250 .atomic_open = nfs_atomic_open,
9251 .link = nfs_link,
9252 .unlink = nfs_unlink,
9253 .symlink = nfs_symlink,
9254 .mkdir = nfs_mkdir,
9255 .rmdir = nfs_rmdir,
9256 .mknod = nfs_mknod,
9257 .rename = nfs_rename,
9258 .permission = nfs_permission,
9259 .getattr = nfs_getattr,
9260 .setattr = nfs_setattr,
9261 .getxattr = generic_getxattr,
9262 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009263 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009264 .removexattr = generic_removexattr,
9265};
9266
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009267static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009268 .permission = nfs_permission,
9269 .getattr = nfs_getattr,
9270 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009271 .getxattr = generic_getxattr,
9272 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009273 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009274 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009275};
9276
David Howells509de812006-08-22 20:06:11 -04009277const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 .version = 4, /* protocol version */
9279 .dentry_ops = &nfs4_dentry_operations,
9280 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009281 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009282 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009284 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009285 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286 .getattr = nfs4_proc_getattr,
9287 .setattr = nfs4_proc_setattr,
9288 .lookup = nfs4_proc_lookup,
9289 .access = nfs4_proc_access,
9290 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291 .create = nfs4_proc_create,
9292 .remove = nfs4_proc_remove,
9293 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009294 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009296 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009297 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009298 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 .link = nfs4_proc_link,
9300 .symlink = nfs4_proc_symlink,
9301 .mkdir = nfs4_proc_mkdir,
9302 .rmdir = nfs4_proc_remove,
9303 .readdir = nfs4_proc_readdir,
9304 .mknod = nfs4_proc_mknod,
9305 .statfs = nfs4_proc_statfs,
9306 .fsinfo = nfs4_proc_fsinfo,
9307 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009308 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009310 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009312 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009314 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009316 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009317 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009319 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009320 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009321 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009322 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009323 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009324 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009325 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009326 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009327 .create_server = nfs4_create_server,
9328 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329};
9330
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009331static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009332 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009333 .list = nfs4_xattr_list_nfs4_acl,
9334 .get = nfs4_xattr_get_nfs4_acl,
9335 .set = nfs4_xattr_set_nfs4_acl,
9336};
9337
9338const struct xattr_handler *nfs4_xattr_handlers[] = {
9339 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009340#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9341 &nfs4_xattr_nfs4_label_handler,
9342#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009343 NULL
9344};
9345
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346/*
9347 * Local variables:
9348 * c-basic-offset: 8
9349 * End:
9350 */