blob: 389089e857f44c45cca60978ce758e45351c30c5 [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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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,
Trond Myklebust1393d962016-09-22 13:39:13 -04001402 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001403{
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)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001407 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001408 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001409 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001410 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001411 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1412 return true;
1413 return false;
1414}
1415
Trond Myklebustf95549c2015-01-23 18:06:09 -05001416static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1417{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001418 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1419 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001420 if (state->n_wronly)
1421 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1422 if (state->n_rdonly)
1423 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1424 if (state->n_rdwr)
1425 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001426 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001427}
1428
Trond Myklebust226056c2014-02-11 10:41:07 -05001429static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001430 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001431 nfs4_stateid *stateid, fmode_t fmode)
1432{
1433 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1434 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1435 case FMODE_WRITE:
1436 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1437 break;
1438 case FMODE_READ:
1439 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1440 break;
1441 case 0:
1442 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1443 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1444 clear_bit(NFS_OPEN_STATE, &state->flags);
1445 }
1446 if (stateid == NULL)
1447 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001448 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001449 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1450 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1451 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001452 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001453 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001454 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001455 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&state->stateid, stateid);
1457 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001458}
1459
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001460static void nfs_clear_open_stateid(struct nfs4_state *state,
1461 nfs4_stateid *arg_stateid,
1462 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001463{
1464 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001465 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001466 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001467 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1468 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001469}
1470
Trond Myklebust1393d962016-09-22 13:39:13 -04001471static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1472 const nfs4_stateid *stateid, fmode_t fmode,
1473 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001474{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001476 case FMODE_READ:
1477 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1478 break;
1479 case FMODE_WRITE:
1480 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1481 break;
1482 case FMODE_READ|FMODE_WRITE:
1483 set_bit(NFS_O_RDWR_STATE, &state->flags);
1484 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001485 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001486 return;
1487 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1488 nfs4_stateid_copy(&state->stateid, stateid);
1489 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001490}
1491
Trond Myklebust1393d962016-09-22 13:39:13 -04001492static void __update_open_stateid(struct nfs4_state *state,
1493 const nfs4_stateid *open_stateid,
1494 const nfs4_stateid *deleg_stateid,
1495 fmode_t fmode,
1496 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001498 /*
1499 * Protect the call to nfs4_state_set_mode_locked and
1500 * serialise the stateid update
1501 */
Andrew Elble361cad32015-12-02 09:20:57 -05001502 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001503 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001504 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001505 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001506 set_bit(NFS_DELEGATED_STATE, &state->flags);
1507 }
1508 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001509 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001510 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001511 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001512 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Trond Myklebust1393d962016-09-22 13:39:13 -04001515static int update_open_stateid(struct nfs4_state *state,
1516 const nfs4_stateid *open_stateid,
1517 const nfs4_stateid *delegation,
1518 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001519{
Trond Myklebust1393d962016-09-22 13:39:13 -04001520 struct nfs_server *server = NFS_SERVER(state->inode);
1521 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001522 struct nfs_inode *nfsi = NFS_I(state->inode);
1523 struct nfs_delegation *deleg_cur;
Trond Myklebust1393d962016-09-22 13:39:13 -04001524 nfs4_stateid freeme = {0};
Trond Myklebust34310432008-12-23 15:21:38 -05001525 int ret = 0;
1526
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001527 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001528
1529 rcu_read_lock();
1530 deleg_cur = rcu_dereference(nfsi->delegation);
1531 if (deleg_cur == NULL)
1532 goto no_delegation;
1533
1534 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001535 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001536 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001537 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001538 goto no_delegation_unlock;
1539
1540 if (delegation == NULL)
1541 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001542 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001543 goto no_delegation_unlock;
1544
Trond Myklebustb7391f42008-12-23 15:21:52 -05001545 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001546 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1547 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001548 ret = 1;
1549no_delegation_unlock:
1550 spin_unlock(&deleg_cur->lock);
1551no_delegation:
1552 rcu_read_unlock();
1553
1554 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001556 ret = 1;
1557 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001558 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 nfs4_schedule_state_manager(clp);
1560 if (freeme.type != 0)
1561 nfs4_test_and_free_stateid(server, &freeme,
1562 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001563
1564 return ret;
1565}
1566
Trond Myklebust39071e62015-01-24 15:07:56 -05001567static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1568 const nfs4_stateid *stateid)
1569{
1570 struct nfs4_state *state = lsp->ls_state;
1571 bool ret = false;
1572
1573 spin_lock(&state->state_lock);
1574 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1575 goto out_noupdate;
1576 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1577 goto out_noupdate;
1578 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1579 ret = true;
1580out_noupdate:
1581 spin_unlock(&state->state_lock);
1582 return ret;
1583}
Trond Myklebust34310432008-12-23 15:21:38 -05001584
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001585static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001586{
1587 struct nfs_delegation *delegation;
1588
1589 rcu_read_lock();
1590 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001591 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001592 rcu_read_unlock();
1593 return;
1594 }
1595 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001596 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001597}
1598
Trond Myklebust6ee41262007-07-08 14:11:36 -04001599static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001600{
1601 struct nfs4_state *state = opendata->state;
1602 struct nfs_inode *nfsi = NFS_I(state->inode);
1603 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001604 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001605 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001606 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001607 nfs4_stateid stateid;
1608 int ret = -EAGAIN;
1609
Trond Myklebustaac00a82007-07-05 19:02:21 -04001610 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001611 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001612 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001613 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001614 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001615 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001616 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001617 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001618 rcu_read_lock();
1619 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001620 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001621 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001622 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001623 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001625 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001626 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001627 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001628 if (!opendata->is_recover) {
1629 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1630 if (ret != 0)
1631 goto out;
1632 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001633 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001634
1635 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001636 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001637 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001638 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639out:
1640 return ERR_PTR(ret);
1641out_return_state:
1642 atomic_inc(&state->count);
1643 return state;
1644}
1645
Andy Adamsone23008e2012-10-02 21:07:32 -04001646static void
1647nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1648{
1649 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1650 struct nfs_delegation *delegation;
1651 int delegation_flags = 0;
1652
1653 rcu_read_lock();
1654 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1655 if (delegation)
1656 delegation_flags = delegation->flags;
1657 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001658 switch (data->o_arg.claim) {
1659 default:
1660 break;
1661 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1662 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001663 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1664 "returning a delegation for "
1665 "OPEN(CLAIM_DELEGATE_CUR)\n",
1666 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001667 return;
1668 }
1669 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001670 nfs_inode_set_delegation(state->inode,
1671 data->owner->so_cred,
1672 &data->o_res);
1673 else
1674 nfs_inode_reclaim_delegation(state->inode,
1675 data->owner->so_cred,
1676 &data->o_res);
1677}
1678
1679/*
1680 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1681 * and update the nfs4_state.
1682 */
1683static struct nfs4_state *
1684_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1685{
1686 struct inode *inode = data->state->inode;
1687 struct nfs4_state *state = data->state;
1688 int ret;
1689
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001690 if (!data->rpc_done) {
1691 if (data->rpc_status) {
1692 ret = data->rpc_status;
1693 goto err;
1694 }
1695 /* cached opens have already been processed */
1696 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001697 }
1698
Andy Adamsone23008e2012-10-02 21:07:32 -04001699 ret = nfs_refresh_inode(inode, &data->f_attr);
1700 if (ret)
1701 goto err;
1702
1703 if (data->o_res.delegation_type != 0)
1704 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001705update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001706 update_open_stateid(state, &data->o_res.stateid, NULL,
1707 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001708 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001709
1710 return state;
1711err:
1712 return ERR_PTR(ret);
1713
1714}
1715
1716static struct nfs4_state *
1717_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718{
1719 struct inode *inode;
1720 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001721 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001724 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001725 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726 goto out;
1727 }
1728
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001729 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001730 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001731 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001732 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001733 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001734 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001735 goto err;
1736 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001737 state = nfs4_get_open_state(inode, data->owner);
1738 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001739 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001740 if (data->o_res.delegation_type != 0)
1741 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001742 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001743 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001744 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001745out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001746 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001748err_put_inode:
1749 iput(inode);
1750err:
1751 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001752}
1753
Andy Adamsone23008e2012-10-02 21:07:32 -04001754static struct nfs4_state *
1755nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1756{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001757 struct nfs4_state *ret;
1758
Andy Adamsone23008e2012-10-02 21:07:32 -04001759 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001760 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1761 else
1762 ret = _nfs4_opendata_to_nfs4_state(data);
1763 nfs4_sequence_free_slot(&data->o_res.seq_res);
1764 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001765}
1766
Trond Myklebust864472e2006-01-03 09:55:15 +01001767static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1768{
1769 struct nfs_inode *nfsi = NFS_I(state->inode);
1770 struct nfs_open_context *ctx;
1771
1772 spin_lock(&state->inode->i_lock);
1773 list_for_each_entry(ctx, &nfsi->open_files, list) {
1774 if (ctx->state != state)
1775 continue;
1776 get_nfs_open_context(ctx);
1777 spin_unlock(&state->inode->i_lock);
1778 return ctx;
1779 }
1780 spin_unlock(&state->inode->i_lock);
1781 return ERR_PTR(-ENOENT);
1782}
1783
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001784static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1785 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001786{
1787 struct nfs4_opendata *opendata;
1788
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001789 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001790 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001791 if (opendata == NULL)
1792 return ERR_PTR(-ENOMEM);
1793 opendata->state = state;
1794 atomic_inc(&state->count);
1795 return opendata;
1796}
1797
Trond Myklebust24311f82015-09-20 10:50:17 -04001798static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1799 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001800{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001801 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001802 int ret;
1803
Trond Myklebust24311f82015-09-20 10:50:17 -04001804 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001805 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001806 opendata->o_arg.open_flags = 0;
1807 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001808 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1809 NFS_SB(opendata->dentry->d_sb),
1810 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001811 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1812 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1813 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001814 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001815 if (ret != 0)
1816 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001817 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001818 if (IS_ERR(newstate))
1819 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001820 if (newstate != opendata->state)
1821 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001822 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001823 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001824}
1825
1826static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1827{
Trond Myklebust864472e2006-01-03 09:55:15 +01001828 int ret;
1829
Trond Myklebust4f14c192014-02-12 19:15:06 -05001830 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1831 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1832 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1833 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001834 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001835 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001836 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001837 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001838 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1839 if (ret != 0)
1840 return ret;
1841 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1842 if (ret != 0)
1843 return ret;
1844 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1845 if (ret != 0)
1846 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001847 /*
1848 * We may have performed cached opens for all three recoveries.
1849 * Check if we need to update the current stateid.
1850 */
1851 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001852 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001853 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001854 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001855 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001856 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001857 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001858 return 0;
1859}
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/*
1862 * OPEN_RECLAIM:
1863 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001865static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001867 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001868 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001869 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int status;
1871
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001872 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1873 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001874 if (IS_ERR(opendata))
1875 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001876 rcu_read_lock();
1877 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001878 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001879 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001880 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001881 opendata->o_arg.u.delegation_type = delegation_type;
1882 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001883 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return status;
1885}
1886
Trond Myklebust539cd032007-06-05 11:46:42 -04001887static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 struct nfs_server *server = NFS_SERVER(state->inode);
1890 struct nfs4_exception exception = { };
1891 int err;
1892 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001893 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001894 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001895 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1896 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001897 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001898 break;
1899 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 } while (exception.retry);
1901 return err;
1902}
1903
Trond Myklebust864472e2006-01-03 09:55:15 +01001904static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1905{
1906 struct nfs_open_context *ctx;
1907 int ret;
1908
1909 ctx = nfs4_state_find_open_context(state);
1910 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001911 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001912 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001913 put_nfs_open_context(ctx);
1914 return ret;
1915}
1916
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001917static 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 -07001918{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001919 switch (err) {
1920 default:
1921 printk(KERN_ERR "NFS: %s: unhandled error "
1922 "%d.\n", __func__, err);
1923 case 0:
1924 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001925 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001926 case -ESTALE:
1927 break;
1928 case -NFS4ERR_BADSESSION:
1929 case -NFS4ERR_BADSLOT:
1930 case -NFS4ERR_BAD_HIGH_SLOT:
1931 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1932 case -NFS4ERR_DEADSESSION:
1933 set_bit(NFS_DELEGATED_STATE, &state->flags);
1934 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1935 return -EAGAIN;
1936 case -NFS4ERR_STALE_CLIENTID:
1937 case -NFS4ERR_STALE_STATEID:
1938 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001939 /* Don't recall a delegation if it was lost */
1940 nfs4_schedule_lease_recovery(server->nfs_client);
1941 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001942 case -NFS4ERR_MOVED:
1943 nfs4_schedule_migration_recovery(server);
1944 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001945 case -NFS4ERR_LEASE_MOVED:
1946 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1947 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001948 case -NFS4ERR_DELEG_REVOKED:
1949 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001950 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001951 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001952 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001953 nfs_inode_find_state_and_recover(state->inode,
1954 stateid);
1955 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001956 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001957 case -NFS4ERR_DELAY:
1958 case -NFS4ERR_GRACE:
1959 set_bit(NFS_DELEGATED_STATE, &state->flags);
1960 ssleep(1);
1961 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001962 case -ENOMEM:
1963 case -NFS4ERR_DENIED:
1964 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1965 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return err;
1968}
1969
Trond Myklebust24311f82015-09-20 10:50:17 -04001970int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1971 struct nfs4_state *state, const nfs4_stateid *stateid,
1972 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001973{
1974 struct nfs_server *server = NFS_SERVER(state->inode);
1975 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001976 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001977
1978 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1979 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1980 if (IS_ERR(opendata))
1981 return PTR_ERR(opendata);
1982 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001983 write_seqlock(&state->seqlock);
1984 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1985 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001986 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1987 switch (type & (FMODE_READ|FMODE_WRITE)) {
1988 case FMODE_READ|FMODE_WRITE:
1989 case FMODE_WRITE:
1990 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1991 if (err)
1992 break;
1993 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1994 if (err)
1995 break;
1996 case FMODE_READ:
1997 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1998 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001999 nfs4_opendata_put(opendata);
2000 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2001}
2002
Chuck Leverbe05c862013-08-09 12:49:47 -04002003static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2004{
2005 struct nfs4_opendata *data = calldata;
2006
Peng Taocb04ad22014-06-11 05:24:15 +08002007 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2008 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002009}
2010
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002011static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2012{
2013 struct nfs4_opendata *data = calldata;
2014
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002015 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002016
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002017 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002018 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002019 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002020 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002021 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002022 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002023 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002024}
2025
2026static void nfs4_open_confirm_release(void *calldata)
2027{
2028 struct nfs4_opendata *data = calldata;
2029 struct nfs4_state *state = NULL;
2030
2031 /* If this request hasn't been cancelled, do nothing */
2032 if (data->cancelled == 0)
2033 goto out_free;
2034 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002035 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002036 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002037 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002038 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002039 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002040out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002041 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002042}
2043
2044static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002045 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002046 .rpc_call_done = nfs4_open_confirm_done,
2047 .rpc_release = nfs4_open_confirm_release,
2048};
2049
2050/*
2051 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2052 */
2053static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2054{
David Howells2b0143b2015-03-17 22:25:59 +00002055 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002056 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002057 struct rpc_message msg = {
2058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2059 .rpc_argp = &data->c_arg,
2060 .rpc_resp = &data->c_res,
2061 .rpc_cred = data->owner->so_cred,
2062 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002063 struct rpc_task_setup task_setup_data = {
2064 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002065 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002066 .callback_ops = &nfs4_open_confirm_ops,
2067 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002068 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002069 .flags = RPC_TASK_ASYNC,
2070 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int status;
2072
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002073 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002074 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002075 data->rpc_done = 0;
2076 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002077 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002078 if (data->is_recover)
2079 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002080 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002081 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002082 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002083 status = nfs4_wait_for_completion_rpc_task(task);
2084 if (status != 0) {
2085 data->cancelled = 1;
2086 smp_wmb();
2087 } else
2088 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002089 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return status;
2091}
2092
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002093static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002095 struct nfs4_opendata *data = calldata;
2096 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002097 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002098 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002099
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002100 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002101 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002102 /*
2103 * Check if we still need to send an OPEN call, or if we can use
2104 * a delegation instead.
2105 */
2106 if (data->state != NULL) {
2107 struct nfs_delegation *delegation;
2108
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002109 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002110 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002111 rcu_read_lock();
2112 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002113 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002114 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002115 rcu_read_unlock();
2116 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002117 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002118 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002119 switch (claim) {
2120 default:
2121 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002122 case NFS4_OPEN_CLAIM_PREVIOUS:
2123 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2124 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002125 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002126 case NFS4_OPEN_CLAIM_FH:
2127 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002128 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2129 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002130 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002131 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002132 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002133 &data->o_res.seq_res,
2134 task) != 0)
2135 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002136
2137 /* Set the create mode (note dependency on the session type) */
2138 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2139 if (data->o_arg.open_flags & O_EXCL) {
2140 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2141 if (nfs4_has_persistent_session(clp))
2142 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2143 else if (clp->cl_mvops->minor_version > 0)
2144 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2145 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002146 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002147unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002148 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002149 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002150out_no_action:
2151 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002152out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002153 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002154}
2155
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002156static void nfs4_open_done(struct rpc_task *task, void *calldata)
2157{
2158 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002161
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002162 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002163 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002164
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002165 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002166 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2167 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002168 case S_IFREG:
2169 break;
2170 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002171 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002172 break;
2173 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002174 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002175 break;
2176 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002177 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002178 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002179 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002180 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002181 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2182 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002183 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002184 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002185}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002186
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187static void nfs4_open_release(void *calldata)
2188{
2189 struct nfs4_opendata *data = calldata;
2190 struct nfs4_state *state = NULL;
2191
2192 /* If this request hasn't been cancelled, do nothing */
2193 if (data->cancelled == 0)
2194 goto out_free;
2195 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002196 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002197 goto out_free;
2198 /* In case we need an open_confirm, no cleanup! */
2199 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2200 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002202 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002203 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002205 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002206}
2207
2208static const struct rpc_call_ops nfs4_open_ops = {
2209 .rpc_call_prepare = nfs4_open_prepare,
2210 .rpc_call_done = nfs4_open_done,
2211 .rpc_release = nfs4_open_release,
2212};
2213
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002214static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002215{
David Howells2b0143b2015-03-17 22:25:59 +00002216 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 struct nfs_server *server = NFS_SERVER(dir);
2218 struct nfs_openargs *o_arg = &data->o_arg;
2219 struct nfs_openres *o_res = &data->o_res;
2220 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2223 .rpc_argp = o_arg,
2224 .rpc_resp = o_res,
2225 .rpc_cred = data->owner->so_cred,
2226 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002227 struct rpc_task_setup task_setup_data = {
2228 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002229 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002230 .callback_ops = &nfs4_open_ops,
2231 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002232 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002233 .flags = RPC_TASK_ASYNC,
2234 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002235 int status;
2236
Chuck Levera9c92d62013-08-09 12:48:18 -04002237 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002238 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002239 data->rpc_done = 0;
2240 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002241 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002242 data->is_recover = 0;
2243 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002244 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002245 data->is_recover = 1;
2246 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002247 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002248 if (IS_ERR(task))
2249 return PTR_ERR(task);
2250 status = nfs4_wait_for_completion_rpc_task(task);
2251 if (status != 0) {
2252 data->cancelled = 1;
2253 smp_wmb();
2254 } else
2255 status = data->rpc_status;
2256 rpc_put_task(task);
2257
2258 return status;
2259}
2260
2261static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2262{
David Howells2b0143b2015-03-17 22:25:59 +00002263 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002264 struct nfs_openres *o_res = &data->o_res;
2265 int status;
2266
2267 status = nfs4_run_open_task(data, 1);
2268 if (status != 0 || !data->rpc_done)
2269 return status;
2270
Trond Myklebust6926afd12012-01-07 13:22:46 -05002271 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2272
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002273 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2274 status = _nfs4_proc_open_confirm(data);
2275 if (status != 0)
2276 return status;
2277 }
2278
2279 return status;
2280}
2281
Trond Myklebustf3792d62014-07-10 08:54:32 -04002282/*
2283 * Additional permission checks in order to distinguish between an
2284 * open for read, and an open for execute. This works around the
2285 * fact that NFSv4 OPEN treats read and execute permissions as being
2286 * the same.
2287 * Note that in the non-execute case, we want to turn off permission
2288 * checking if we just created a new file (POSIX open() semantics).
2289 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002290static int nfs4_opendata_access(struct rpc_cred *cred,
2291 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002292 struct nfs4_state *state, fmode_t fmode,
2293 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002294{
2295 struct nfs_access_entry cache;
2296 u32 mask;
2297
2298 /* access call failed or for some reason the server doesn't
2299 * support any access modes -- defer access call until later */
2300 if (opendata->o_res.access_supported == 0)
2301 return 0;
2302
2303 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002304 /*
2305 * Use openflags to check for exec, because fmode won't
2306 * always have FMODE_EXEC set when file open for exec.
2307 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002308 if (openflags & __FMODE_EXEC) {
2309 /* ONLY check for exec rights */
2310 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002311 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002312 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002313
2314 cache.cred = cred;
2315 cache.jiffies = jiffies;
2316 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2317 nfs_access_add_cache(state->inode, &cache);
2318
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002319 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002320 return 0;
2321
2322 /* even though OPEN succeeded, access is denied. Close the file */
2323 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002324 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002325}
2326
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002327/*
2328 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2329 */
2330static int _nfs4_proc_open(struct nfs4_opendata *data)
2331{
David Howells2b0143b2015-03-17 22:25:59 +00002332 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002333 struct nfs_server *server = NFS_SERVER(dir);
2334 struct nfs_openargs *o_arg = &data->o_arg;
2335 struct nfs_openres *o_res = &data->o_res;
2336 int status;
2337
2338 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002339 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002340 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002341 if (status != 0) {
2342 if (status == -NFS4ERR_BADNAME &&
2343 !(o_arg->open_flags & O_CREAT))
2344 return -ENOENT;
2345 return status;
2346 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002347
Trond Myklebust6926afd12012-01-07 13:22:46 -05002348 nfs_fattr_map_and_free_names(server, &data->f_attr);
2349
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002350 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002351 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002352 if (o_arg->open_flags & O_EXCL)
2353 data->file_created = 1;
2354 else if (o_res->cinfo.before != o_res->cinfo.after)
2355 data->file_created = 1;
2356 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002357 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2358 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002360 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002362 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002365 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Andy Adamsond83217c2011-03-01 01:34:17 +00002369static int nfs4_recover_expired_lease(struct nfs_server *server)
2370{
2371 return nfs4_client_recover_expired_lease(server->nfs_client);
2372}
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374/*
2375 * OPEN_EXPIRED:
2376 * reclaim state on the server after a network partition.
2377 * Assumes caller holds the appropriate lock
2378 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002379static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002381 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002382 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002384 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002385 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002386 if (IS_ERR(opendata))
2387 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002388 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002389 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002390 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002391 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002392 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002395static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002396{
Trond Myklebust539cd032007-06-05 11:46:42 -04002397 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002398 struct nfs4_exception exception = { };
2399 int err;
2400
2401 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002402 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002403 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002404 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2405 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002406 switch (err) {
2407 default:
2408 goto out;
2409 case -NFS4ERR_GRACE:
2410 case -NFS4ERR_DELAY:
2411 nfs4_handle_exception(server, err, &exception);
2412 err = 0;
2413 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002414 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002415out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002416 return err;
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002422 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Trond Myklebust864472e2006-01-03 09:55:15 +01002424 ctx = nfs4_state_find_open_context(state);
2425 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002426 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002427 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002428 put_nfs_open_context(ctx);
2429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Trond Myklebust41020b62016-09-22 13:38:58 -04002432static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2433 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002434{
Trond Myklebust41020b62016-09-22 13:38:58 -04002435 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002436 write_seqlock(&state->seqlock);
2437 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2438 write_sequnlock(&state->seqlock);
2439 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2440}
2441
2442static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2443{
2444 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002445 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002446}
2447
2448static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2449{
2450 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2451 nfs40_clear_delegation_stateid(state);
2452 return nfs4_open_expired(sp, state);
2453}
2454
Trond Myklebust45870d62016-09-22 13:38:59 -04002455static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2456 nfs4_stateid *stateid,
2457 struct rpc_cred *cred)
2458{
2459 return -NFS4ERR_BAD_STATEID;
2460}
2461
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002462#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002463static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2464 nfs4_stateid *stateid,
2465 struct rpc_cred *cred)
2466{
2467 int status;
2468
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002469 switch (stateid->type) {
2470 default:
2471 break;
2472 case NFS4_INVALID_STATEID_TYPE:
2473 case NFS4_SPECIAL_STATEID_TYPE:
2474 return -NFS4ERR_BAD_STATEID;
2475 case NFS4_REVOKED_STATEID_TYPE:
2476 goto out_free;
2477 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002478
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002479 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002480 switch (status) {
2481 case -NFS4ERR_EXPIRED:
2482 case -NFS4ERR_ADMIN_REVOKED:
2483 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002484 break;
2485 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002486 return status;
2487 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002488out_free:
2489 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002490 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002491 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002492}
2493
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002494static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002495{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002496 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002497 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002498 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002499 struct rpc_cred *cred;
2500 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002501
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002502 /* Get the delegation credential for use by test/free_stateid */
2503 rcu_read_lock();
2504 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002505 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002506 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002507 return;
2508 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002509
2510 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002511 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2512 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002513 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002514 return;
2515 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002516
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002517 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2518 rcu_read_unlock();
2519 return;
2520 }
2521
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002522 cred = get_rpccred(delegation->cred);
2523 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002524 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002526 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002527 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002528
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002530}
2531
2532/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002533 * nfs41_check_expired_locks - possibly free a lock stateid
2534 *
2535 * @state: NFSv4 state for an inode
2536 *
2537 * Returns NFS_OK if recovery for this stateid is now finished.
2538 * Otherwise a negative NFS4ERR value is returned.
2539 */
2540static int nfs41_check_expired_locks(struct nfs4_state *state)
2541{
2542 int status, ret = NFS_OK;
2543 struct nfs4_lock_state *lsp;
2544 struct nfs_server *server = NFS_SERVER(state->inode);
2545
2546 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2547 goto out;
2548 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2549 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2550 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2551
2552 status = nfs41_test_and_free_expired_stateid(server,
2553 &lsp->ls_stateid,
2554 cred);
2555 trace_nfs4_test_lock_stateid(state, lsp, status);
2556 if (status == -NFS4ERR_EXPIRED ||
2557 status == -NFS4ERR_BAD_STATEID) {
2558 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2559 if (!recover_lost_locks)
2560 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2561 } else if (status != NFS_OK) {
2562 ret = status;
2563 break;
2564 }
2565 }
2566 };
2567out:
2568 return ret;
2569}
2570
2571/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002572 * nfs41_check_open_stateid - possibly free an open stateid
2573 *
2574 * @state: NFSv4 state for an inode
2575 *
2576 * Returns NFS_OK if recovery for this stateid is now finished.
2577 * Otherwise a negative NFS4ERR value is returned.
2578 */
2579static int nfs41_check_open_stateid(struct nfs4_state *state)
2580{
2581 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002582 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002583 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002584 int status;
2585
2586 /* If a state reset has been done, test_stateid is unneeded */
2587 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2588 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2589 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2590 return -NFS4ERR_BAD_STATEID;
2591
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002592 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002593 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002594 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002595 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2596 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2597 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002598 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002599 }
2600 return status;
2601}
2602
2603static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2604{
Chuck Levereb64cf92012-07-11 16:30:05 -04002605 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002606
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002607 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002608 status = nfs41_check_expired_locks(state);
2609 if (status != NFS_OK)
2610 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002611 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002612 if (status != NFS_OK)
2613 status = nfs4_open_expired(sp, state);
2614 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002615}
2616#endif
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002619 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2620 * fields corresponding to attributes that were used to store the verifier.
2621 * Make sure we clobber those fields in the later setattr call
2622 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002623static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2624 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002625{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002626 const u32 *attrset = opendata->o_res.attrset;
2627
2628 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002629 !(sattr->ia_valid & ATTR_ATIME_SET))
2630 sattr->ia_valid |= ATTR_ATIME;
2631
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002632 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002633 !(sattr->ia_valid & ATTR_MTIME_SET))
2634 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002635
2636 /* Except MODE, it seems harmless of setting twice. */
2637 if ((attrset[1] & FATTR4_WORD1_MODE))
2638 sattr->ia_valid &= ~ATTR_MODE;
2639
2640 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2641 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002642}
2643
Trond Myklebustc21443c2013-02-07 14:26:21 -05002644static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2645 fmode_t fmode,
2646 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002647 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002648{
2649 struct nfs4_state_owner *sp = opendata->owner;
2650 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002651 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002652 struct nfs4_state *state;
2653 unsigned int seq;
2654 int ret;
2655
2656 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2657
2658 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002659 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002660 goto out;
2661
2662 state = nfs4_opendata_to_nfs4_state(opendata);
2663 ret = PTR_ERR(state);
2664 if (IS_ERR(state))
2665 goto out;
2666 if (server->caps & NFS_CAP_POSIX_LOCK)
2667 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002668 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2669 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002670
Trond Myklebust275bb302013-05-29 13:11:28 -04002671 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002672 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002673 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002674 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002675 alias = d_exact_alias(dentry, state->inode);
2676 if (!alias)
2677 alias = d_splice_alias(igrab(state->inode), dentry);
2678 /* d_splice_alias() can't fail here - it's a non-directory */
2679 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002680 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002681 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002682 }
2683 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002684 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002685 }
2686
Trond Myklebustc21443c2013-02-07 14:26:21 -05002687 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2688 if (ret != 0)
2689 goto out;
2690
Trond Myklebust3efb9722013-05-29 13:17:04 -04002691 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002692 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002693 nfs_inode_attach_open_context(ctx);
2694 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2695 nfs4_schedule_stateid_recovery(server, state);
2696 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002697out:
2698 return ret;
2699}
2700
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002701/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002702 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 */
Andy Adamson82be4172012-05-23 05:02:35 -04002704static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002705 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002706 int flags,
2707 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002708 struct nfs4_label *label,
2709 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct nfs4_state_owner *sp;
2712 struct nfs4_state *state = NULL;
2713 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002714 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002715 struct dentry *dentry = ctx->dentry;
2716 struct rpc_cred *cred = ctx->cred;
2717 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2718 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002719 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002720 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002721 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002725 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2726 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2728 goto out_err;
2729 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002730 status = nfs4_recover_expired_lease(server);
2731 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002732 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002733 if (d_really_is_positive(dentry))
2734 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002735 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002736 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002737 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002738 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002739 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002740 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002741 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
David Quigley14c43f72013-05-22 12:50:43 -04002743 if (label) {
2744 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2745 if (IS_ERR(olabel)) {
2746 status = PTR_ERR(olabel);
2747 goto err_opendata_put;
2748 }
2749 }
2750
Trond Myklebuste911b812014-03-26 13:24:37 -07002751 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2752 if (!opendata->f_attr.mdsthreshold) {
2753 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2754 if (!opendata->f_attr.mdsthreshold)
2755 goto err_free_label;
2756 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002757 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002758 }
David Howells2b0143b2015-03-17 22:25:59 +00002759 if (d_really_is_positive(dentry))
2760 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002761
Trond Myklebust3efb9722013-05-29 13:17:04 -04002762 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002763 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002764 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002765 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002766
NeilBrownefcbc042015-07-30 13:00:56 +10002767 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002768 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002769 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002770 /*
2771 * send create attributes which was not set by open
2772 * with an extra setattr.
2773 */
2774 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2775 nfs_fattr_init(opendata->o_res.f_attr);
2776 status = nfs4_do_setattr(state->inode, cred,
2777 opendata->o_res.f_attr, sattr,
2778 state, label, olabel);
2779 if (status == 0) {
2780 nfs_setattr_update_inode(state->inode, sattr,
2781 opendata->o_res.f_attr);
2782 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2783 }
David Quigley1775fd32013-05-22 12:50:42 -04002784 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002785 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002786 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002787 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002788
Trond Myklebuste911b812014-03-26 13:24:37 -07002789 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002790 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002791 opendata->f_attr.mdsthreshold = NULL;
2792 }
Andy Adamson82be4172012-05-23 05:02:35 -04002793
David Quigley14c43f72013-05-22 12:50:43 -04002794 nfs4_label_free(olabel);
2795
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002796 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002799err_free_label:
2800 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002801err_opendata_put:
2802 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002803err_put_state_owner:
2804 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return status;
2807}
2808
2809
Andy Adamson82be4172012-05-23 05:02:35 -04002810static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002811 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002812 int flags,
2813 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002814 struct nfs4_label *label,
2815 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002817 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct nfs4_exception exception = { };
2819 struct nfs4_state *res;
2820 int status;
2821
2822 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002823 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002824 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002825 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (status == 0)
2827 break;
2828 /* NOTE: BAD_SEQID means the server and client disagree about the
2829 * book-keeping w.r.t. state-changing operations
2830 * (OPEN/CLOSE/LOCK/LOCKU...)
2831 * It is actually a sign of a bug on the client or on the server.
2832 *
2833 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002834 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 * have unhashed the old state_owner for us, and that we can
2836 * therefore safely retry using a new one. We should still warn
2837 * the user though...
2838 */
2839 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002840 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002841 " returned a bad sequence-id error!\n",
2842 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 exception.retry = 1;
2844 continue;
2845 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002846 /*
2847 * BAD_STATEID on OPEN means that the server cancelled our
2848 * state before it received the OPEN_CONFIRM.
2849 * Recover by retrying the request as per the discussion
2850 * on Page 181 of RFC3530.
2851 */
2852 if (status == -NFS4ERR_BAD_STATEID) {
2853 exception.retry = 1;
2854 continue;
2855 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002856 if (status == -EAGAIN) {
2857 /* We must have found a delegation */
2858 exception.retry = 1;
2859 continue;
2860 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002861 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2862 continue;
2863 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 status, &exception));
2865 } while (exception.retry);
2866 return res;
2867}
2868
Trond Myklebust8487c472016-06-26 08:44:35 -04002869static int _nfs4_do_setattr(struct inode *inode,
2870 struct nfs_setattrargs *arg,
2871 struct nfs_setattrres *res,
2872 struct rpc_cred *cred,
2873 struct nfs4_state *state)
2874{
2875 struct nfs_server *server = NFS_SERVER(inode);
2876 struct rpc_message msg = {
2877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2878 .rpc_argp = arg,
2879 .rpc_resp = res,
2880 .rpc_cred = cred,
2881 };
2882 struct rpc_cred *delegation_cred = NULL;
2883 unsigned long timestamp = jiffies;
2884 fmode_t fmode;
2885 bool truncate;
2886 int status;
2887
2888 nfs_fattr_init(res->fattr);
2889
2890 /* Servers should only apply open mode checks for file size changes */
2891 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2892 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2893
2894 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2895 /* Use that stateid */
2896 } else if (truncate && state != NULL) {
2897 struct nfs_lockowner lockowner = {
2898 .l_owner = current->files,
2899 .l_pid = current->tgid,
2900 };
2901 if (!nfs4_valid_open_stateid(state))
2902 return -EBADF;
2903 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2904 &arg->stateid, &delegation_cred) == -EIO)
2905 return -EBADF;
2906 } else
2907 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2908 if (delegation_cred)
2909 msg.rpc_cred = delegation_cred;
2910
2911 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2912
2913 put_rpccred(delegation_cred);
2914 if (status == 0 && state != NULL)
2915 renew_lease(server, timestamp);
2916 trace_nfs4_setattr(inode, &arg->stateid, status);
2917 return status;
2918}
2919
2920static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2921 struct nfs_fattr *fattr, struct iattr *sattr,
2922 struct nfs4_state *state, struct nfs4_label *ilabel,
2923 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002925 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002927 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 .iap = sattr,
2929 .server = server,
2930 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002931 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 };
2933 struct nfs_setattrres res = {
2934 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002935 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .server = server,
2937 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002938 struct nfs4_exception exception = {
2939 .state = state,
2940 .inode = inode,
2941 .stateid = &arg.stateid,
2942 };
2943 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
David Quigleyaa9c2662013-05-22 12:50:44 -04002945 arg.bitmask = nfs4_bitmask(server, ilabel);
2946 if (ilabel)
2947 arg.bitmask = nfs4_bitmask(server, olabel);
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002950 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002951 switch (err) {
2952 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002953 if (!(sattr->ia_valid & ATTR_SIZE)) {
2954 pr_warn_once("NFSv4: server %s is incorrectly "
2955 "applying open mode checks to "
2956 "a SETATTR that is not "
2957 "changing file size.\n",
2958 server->nfs_client->cl_hostname);
2959 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002960 if (state && !(state->state & FMODE_WRITE)) {
2961 err = -EBADF;
2962 if (sattr->ia_valid & ATTR_OPEN)
2963 err = -EACCES;
2964 goto out;
2965 }
2966 }
2967 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002969out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return err;
2971}
2972
Peng Tao500d7012015-09-22 11:35:22 +08002973static bool
2974nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2975{
2976 if (inode == NULL || !nfs_have_layout(inode))
2977 return false;
2978
2979 return pnfs_wait_on_layoutreturn(inode, task);
2980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982struct nfs4_closedata {
2983 struct inode *inode;
2984 struct nfs4_state *state;
2985 struct nfs_closeargs arg;
2986 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002987 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002988 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002989 bool roc;
2990 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991};
2992
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002993static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002994{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002995 struct nfs4_closedata *calldata = data;
2996 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002997 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002998
Fred Isamanf7e89172011-01-06 11:36:32 +00002999 if (calldata->roc)
3000 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003001 nfs4_put_open_state(calldata->state);
3002 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003003 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003004 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003005 kfree(calldata);
3006}
3007
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003008static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003010 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003013 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Chuck Levera3ca5652012-03-01 17:00:40 -05003015 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003016 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3017 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003018 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 /* hmm. we are done with the inode, and in the process of freeing
3020 * the state_owner. we keep this around to process errors
3021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 switch (task->tk_status) {
3023 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003024 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003025 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003026 pnfs_roc_set_barrier(state->inode,
3027 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003028 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003029 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003030 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003032 case -NFS4ERR_EXPIRED:
3033 nfs4_free_revoked_stateid(server,
3034 &calldata->arg.stateid,
3035 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003036 case -NFS4ERR_OLD_STATEID:
3037 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003038 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003039 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003040 rpc_restart_call_prepare(task);
3041 goto out_release;
3042 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003043 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003046 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003047 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003048 goto out_release;
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003051 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3052 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003053out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003054 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003055 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003056 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003059static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003061 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003062 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003063 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003064 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003065 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003066
Chuck Levera3ca5652012-03-01 17:00:40 -05003067 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003068 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003069 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003070
Trond Myklebust88069f72009-12-08 08:33:16 -05003071 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003072 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003073 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3074 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3075 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003076 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003077 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003078 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003079 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003080 if (state->n_rdonly == 0)
3081 call_close |= is_rdonly;
3082 else if (is_rdonly)
3083 calldata->arg.fmode |= FMODE_READ;
3084 if (state->n_wronly == 0)
3085 call_close |= is_wronly;
3086 else if (is_wronly)
3087 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003088 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3089 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003090 } else if (is_rdwr)
3091 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3092
Trond Myklebust5d422302013-03-14 16:57:48 -04003093 if (!nfs4_valid_open_stateid(state))
3094 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003095 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003096
3097 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003098 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003099 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003100 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003101
Peng Tao500d7012015-09-22 11:35:22 +08003102 if (nfs4_wait_on_layoutreturn(inode, task)) {
3103 nfs_release_seqid(calldata->arg.seqid);
3104 goto out_wait;
3105 }
3106
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003107 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003108 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003109 if (calldata->roc)
3110 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3111
Trond Myklebust6ae37332015-01-30 14:21:14 -05003112 calldata->arg.share_access =
3113 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3114 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003115
Trond Myklebust516a6af2005-10-27 22:12:41 -04003116 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003117 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003118 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003119 &calldata->arg.seq_args,
3120 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003121 task) != 0)
3122 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003123 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003124 return;
3125out_no_action:
3126 task->tk_action = NULL;
3127out_wait:
3128 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003132 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003133 .rpc_call_done = nfs4_close_done,
3134 .rpc_release = nfs4_free_closedata,
3135};
3136
Peng Taofe08c542014-07-03 13:05:01 +08003137static bool nfs4_roc(struct inode *inode)
3138{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003139 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003140 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003141 return pnfs_roc(inode);
3142}
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144/*
3145 * It is possible for data to be read/written from a mem-mapped file
3146 * after the sys_close call (which hits the vfs layer as a flush).
3147 * This means that we can't safely call nfsv4 close on a file until
3148 * the inode is cleared. This in turn means that we are not good
3149 * NFSv4 citizens - we do not indicate to the server to update the file's
3150 * share state even when we are done with one of the three share
3151 * stateid's in the inode.
3152 *
3153 * NOTE: Caller must be holding the sp->so_owner semaphore!
3154 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003155int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003157 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003158 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003160 struct nfs4_state_owner *sp = state->owner;
3161 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003162 struct rpc_message msg = {
3163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3164 .rpc_cred = state->owner->so_cred,
3165 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003166 struct rpc_task_setup task_setup_data = {
3167 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003168 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003169 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003170 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003171 .flags = RPC_TASK_ASYNC,
3172 };
Trond Myklebust95121352005-10-18 14:20:12 -07003173 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003175 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3176 &task_setup_data.rpc_client, &msg);
3177
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003178 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003180 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003181 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003182 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003184 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003186 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3187 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003188 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003189 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003190 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003191 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003192 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003193 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003194 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003195 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003196 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003197
Trond Myklebust1174dd12010-12-21 10:52:24 -05003198 msg.rpc_argp = &calldata->arg;
3199 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003200 task_setup_data.callback_data = calldata;
3201 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003202 if (IS_ERR(task))
3203 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003204 status = 0;
3205 if (wait)
3206 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003207 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003208 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003209out_free_calldata:
3210 kfree(calldata);
3211out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003212 nfs4_put_open_state(state);
3213 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003214 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Trond Myklebust2b484292010-09-17 10:56:51 -04003217static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003218nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3219 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003222 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3223
3224 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Trond Myklebust565277f2007-10-15 18:17:53 -04003226 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003227 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003228
3229 nfs4_label_release_security(label);
3230
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003231 if (IS_ERR(state))
3232 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003233 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Trond Myklebust1185a552009-12-03 15:54:02 -05003236static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003237{
3238 if (ctx->state == NULL)
3239 return;
3240 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003241 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003242 else
Al Viro643168c2011-06-22 18:20:23 -04003243 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Trond Myklebustb944dba2013-11-04 15:20:20 -05003246#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3247#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003248#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3251{
Kinglong Mee8c612822015-08-26 21:12:58 +08003252 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003253 struct nfs4_server_caps_arg args = {
3254 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003255 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003256 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct nfs4_server_caps_res res = {};
3258 struct rpc_message msg = {
3259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003260 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 .rpc_resp = &res,
3262 };
3263 int status;
3264
Kinglong Mee8c612822015-08-26 21:12:58 +08003265 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3266 FATTR4_WORD0_FH_EXPIRE_TYPE |
3267 FATTR4_WORD0_LINK_SUPPORT |
3268 FATTR4_WORD0_SYMLINK_SUPPORT |
3269 FATTR4_WORD0_ACLSUPPORT;
3270 if (minorversion)
3271 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3272
Bryan Schumaker7c513052011-03-24 17:12:24 +00003273 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003275 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003276 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003277 case 0:
3278 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3279 res.attr_bitmask[2] = 0;
3280 break;
3281 case 1:
3282 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3283 break;
3284 case 2:
3285 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003288 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3289 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3290 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3291 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003292 NFS_CAP_CTIME|NFS_CAP_MTIME|
3293 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003294 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3295 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 server->caps |= NFS_CAP_ACLS;
3297 if (res.has_links != 0)
3298 server->caps |= NFS_CAP_HARDLINKS;
3299 if (res.has_symlinks != 0)
3300 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003301 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3302 server->caps |= NFS_CAP_FILEID;
3303 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3304 server->caps |= NFS_CAP_MODE;
3305 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3306 server->caps |= NFS_CAP_NLINK;
3307 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3308 server->caps |= NFS_CAP_OWNER;
3309 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3310 server->caps |= NFS_CAP_OWNER_GROUP;
3311 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3312 server->caps |= NFS_CAP_ATIME;
3313 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3314 server->caps |= NFS_CAP_CTIME;
3315 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3316 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003317#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3318 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3319 server->caps |= NFS_CAP_SECURITY_LABEL;
3320#endif
3321 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3322 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003323 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003324
Trond Myklebusta65318b2009-03-11 14:10:28 -04003325 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3326 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3327 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003328 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003329 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3330 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003332 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return status;
3336}
3337
Trond Myklebust55a97592006-06-09 09:34:19 -04003338int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 struct nfs4_exception exception = { };
3341 int err;
3342 do {
3343 err = nfs4_handle_exception(server,
3344 _nfs4_server_capabilities(server, fhandle),
3345 &exception);
3346 } while (exception.retry);
3347 return err;
3348}
3349
3350static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3351 struct nfs_fsinfo *info)
3352{
David Quigleyaa9c2662013-05-22 12:50:44 -04003353 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003355 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 };
3357 struct nfs4_lookup_res res = {
3358 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003359 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 .fh = fhandle,
3361 };
3362 struct rpc_message msg = {
3363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3364 .rpc_argp = &args,
3365 .rpc_resp = &res,
3366 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003367
David Quigleyaa9c2662013-05-22 12:50:44 -04003368 bitmask[0] = nfs4_fattr_bitmap[0];
3369 bitmask[1] = nfs4_fattr_bitmap[1];
3370 /*
3371 * Process the label in the upcoming getfattr
3372 */
3373 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3374
Trond Myklebust0e574af2005-10-27 22:12:38 -04003375 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003376 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
3379static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3380 struct nfs_fsinfo *info)
3381{
3382 struct nfs4_exception exception = { };
3383 int err;
3384 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003385 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003386 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003387 switch (err) {
3388 case 0:
3389 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003390 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003391 default:
3392 err = nfs4_handle_exception(server, err, &exception);
3393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003395out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return err;
3397}
3398
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003399static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3401{
Trond Myklebustc2190662013-08-26 19:23:04 -04003402 struct rpc_auth_create_args auth_args = {
3403 .pseudoflavor = flavor,
3404 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003405 struct rpc_auth *auth;
3406 int ret;
3407
Trond Myklebustc2190662013-08-26 19:23:04 -04003408 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003409 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003410 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003411 goto out;
3412 }
3413 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003414out:
3415 return ret;
3416}
3417
Chuck Lever9a744ba2013-03-16 15:56:02 -04003418/*
3419 * Retry pseudoroot lookup with various security flavors. We do this when:
3420 *
3421 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3422 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3423 *
3424 * Returns zero on success, or a negative NFS4ERR value, or a
3425 * negative errno value.
3426 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003427static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003428 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003430 /* Per 3530bis 15.33.5 */
3431 static const rpc_authflavor_t flav_array[] = {
3432 RPC_AUTH_GSS_KRB5P,
3433 RPC_AUTH_GSS_KRB5I,
3434 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003435 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003436 RPC_AUTH_NULL,
3437 };
3438 int status = -EPERM;
3439 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003441 if (server->auth_info.flavor_len > 0) {
3442 /* try each flavor specified by user */
3443 for (i = 0; i < server->auth_info.flavor_len; i++) {
3444 status = nfs4_lookup_root_sec(server, fhandle, info,
3445 server->auth_info.flavors[i]);
3446 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3447 continue;
3448 break;
3449 }
3450 } else {
3451 /* no flavors specified by user, try default list */
3452 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3453 status = nfs4_lookup_root_sec(server, fhandle, info,
3454 flav_array[i]);
3455 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3456 continue;
3457 break;
3458 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003459 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003460
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003461 /*
3462 * -EACCESS could mean that the user doesn't have correct permissions
3463 * to access the mount. It could also mean that we tried to mount
3464 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3465 * existing mount programs don't handle -EACCES very well so it should
3466 * be mapped to -EPERM instead.
3467 */
3468 if (status == -EACCES)
3469 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003470 return status;
3471}
3472
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003473/**
3474 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3475 * @server: initialized nfs_server handle
3476 * @fhandle: we fill in the pseudo-fs root file handle
3477 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003478 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003479 *
3480 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003481 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003482int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003483 struct nfs_fsinfo *info,
3484 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003485{
Andre Przywarac7757072015-04-23 17:17:40 +01003486 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003487
Andre Przywarac7757072015-04-23 17:17:40 +01003488 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003489 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003490
3491 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003492 status = server->nfs_client->cl_mvops->find_root_sec(server,
3493 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 if (status == 0)
3496 status = nfs4_server_capabilities(server, fhandle);
3497 if (status == 0)
3498 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003499
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003500 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
3502
Bryan Schumakerbae36242012-05-10 15:07:31 -04003503static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3504 struct nfs_fsinfo *info)
3505{
3506 int error;
3507 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003508 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003509
3510 error = nfs4_server_capabilities(server, mntfh);
3511 if (error < 0) {
3512 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3513 return error;
3514 }
3515
David Quigley14c43f72013-05-22 12:50:43 -04003516 label = nfs4_label_alloc(server, GFP_KERNEL);
3517 if (IS_ERR(label))
3518 return PTR_ERR(label);
3519
David Quigley1775fd32013-05-22 12:50:42 -04003520 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003521 if (error < 0) {
3522 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003523 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003524 }
3525
3526 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3527 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3528 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3529
David Quigley14c43f72013-05-22 12:50:43 -04003530err_free_label:
3531 nfs4_label_free(label);
3532
Bryan Schumakerbae36242012-05-10 15:07:31 -04003533 return error;
3534}
3535
Manoj Naik6b97fd32006-06-09 09:34:29 -04003536/*
3537 * Get locations and (maybe) other attributes of a referral.
3538 * Note that we'll actually follow the referral later when
3539 * we detect fsid mismatch in inode revalidation
3540 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003541static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3542 const struct qstr *name, struct nfs_fattr *fattr,
3543 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003544{
3545 int status = -ENOMEM;
3546 struct page *page = NULL;
3547 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003548
3549 page = alloc_page(GFP_KERNEL);
3550 if (page == NULL)
3551 goto out;
3552 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3553 if (locations == NULL)
3554 goto out;
3555
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003556 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003557 if (status != 0)
3558 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003559
3560 /*
3561 * If the fsid didn't change, this is a migration event, not a
3562 * referral. Cause us to drop into the exception handler, which
3563 * will kick off migration recovery.
3564 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003565 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003566 dprintk("%s: server did not return a different fsid for"
3567 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003568 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003569 goto out;
3570 }
Andy Adamson533eb462011-06-13 18:25:56 -04003571 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3572 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003573
Andy Adamson533eb462011-06-13 18:25:56 -04003574 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003575 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003576 memset(fhandle, 0, sizeof(struct nfs_fh));
3577out:
3578 if (page)
3579 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003580 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003581 return status;
3582}
3583
David Quigley1775fd32013-05-22 12:50:42 -04003584static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3585 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 struct nfs4_getattr_arg args = {
3588 .fh = fhandle,
3589 .bitmask = server->attr_bitmask,
3590 };
3591 struct nfs4_getattr_res res = {
3592 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003593 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 .server = server,
3595 };
3596 struct rpc_message msg = {
3597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3598 .rpc_argp = &args,
3599 .rpc_resp = &res,
3600 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003601
3602 args.bitmask = nfs4_bitmask(server, label);
3603
Trond Myklebust0e574af2005-10-27 22:12:38 -04003604 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003605 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
David Quigley1775fd32013-05-22 12:50:42 -04003608static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3609 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 struct nfs4_exception exception = { };
3612 int err;
3613 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003614 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3615 trace_nfs4_getattr(server, fhandle, fattr, err);
3616 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 &exception);
3618 } while (exception.retry);
3619 return err;
3620}
3621
3622/*
3623 * The file is not closed if it is opened due to the a request to change
3624 * the size of the file. The open call will not be needed once the
3625 * VFS layer lookup-intents are implemented.
3626 *
3627 * Close is called when the inode is destroyed.
3628 * If we haven't opened the file for O_WRONLY, we
3629 * need to in the size_change case to obtain a stateid.
3630 *
3631 * Got race?
3632 * Because OPEN is always done by name in nfsv4, it is
3633 * possible that we opened a different file by the same
3634 * name. We can recognize this race condition, but we
3635 * can't do anything about it besides returning an error.
3636 *
3637 * This will be fixed with VFS changes (lookup-intent).
3638 */
3639static int
3640nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3641 struct iattr *sattr)
3642{
David Howells2b0143b2015-03-17 22:25:59 +00003643 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003644 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003645 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003646 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 int status;
3648
Peng Tao88ac8152014-09-12 11:04:10 +08003649 if (pnfs_ld_layoutret_on_setattr(inode) &&
3650 sattr->ia_valid & ATTR_SIZE &&
3651 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003652 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003653
Trond Myklebust0e574af2005-10-27 22:12:38 -04003654 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Andy Adamson26699402012-05-30 16:12:24 -04003656 /* Deal with open(O_TRUNC) */
3657 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003658 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003659
3660 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003661 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003662 return 0;
3663
Trond Myklebustd5308382005-11-04 15:33:38 -05003664 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003665 if (sattr->ia_valid & ATTR_FILE) {
3666 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003667
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003668 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003669 if (ctx) {
3670 cred = ctx->cred;
3671 state = ctx->state;
3672 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003673 }
3674
David Quigley14c43f72013-05-22 12:50:43 -04003675 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3676 if (IS_ERR(label))
3677 return PTR_ERR(label);
3678
3679 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003680 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003681 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003682 nfs_setsecurity(inode, fattr, label);
3683 }
David Quigley14c43f72013-05-22 12:50:43 -04003684 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return status;
3686}
3687
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003688static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3689 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003690 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003691{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003692 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003693 int status;
3694 struct nfs4_lookup_arg args = {
3695 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003696 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003697 .name = name,
3698 };
3699 struct nfs4_lookup_res res = {
3700 .server = server,
3701 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003702 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003703 .fh = fhandle,
3704 };
3705 struct rpc_message msg = {
3706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3707 .rpc_argp = &args,
3708 .rpc_resp = &res,
3709 };
3710
David Quigleyaa9c2662013-05-22 12:50:44 -04003711 args.bitmask = nfs4_bitmask(server, label);
3712
David Howells2b3de442006-08-22 20:06:09 -04003713 nfs_fattr_init(fattr);
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003716 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 dprintk("NFS reply lookup: %d\n", status);
3718 return status;
3719}
3720
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003721static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003722{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003723 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003724 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003725 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3726 fattr->nlink = 2;
3727}
3728
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003729static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003730 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003731 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003732{
3733 struct nfs4_exception exception = { };
3734 struct rpc_clnt *client = *clnt;
3735 int err;
3736 do {
David Quigley1775fd32013-05-22 12:50:42 -04003737 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003738 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003739 switch (err) {
3740 case -NFS4ERR_BADNAME:
3741 err = -ENOENT;
3742 goto out;
3743 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003744 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003745 if (err == -NFS4ERR_MOVED)
3746 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003747 goto out;
3748 case -NFS4ERR_WRONGSEC:
3749 err = -EPERM;
3750 if (client != *clnt)
3751 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003752 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003753 if (IS_ERR(client))
3754 return PTR_ERR(client);
3755
3756 exception.retry = 1;
3757 break;
3758 default:
3759 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3760 }
3761 } while (exception.retry);
3762
3763out:
3764 if (err == 0)
3765 *clnt = client;
3766 else if (client != *clnt)
3767 rpc_shutdown_client(client);
3768
3769 return err;
3770}
3771
Al Virobeffb8f2016-07-20 16:34:42 -04003772static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003773 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3774 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003776 int status;
3777 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003778
David Quigley1775fd32013-05-22 12:50:42 -04003779 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003780 if (client != NFS_CLIENT(dir)) {
3781 rpc_shutdown_client(client);
3782 nfs_fixup_secinfo_attributes(fattr);
3783 }
3784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785}
3786
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003787struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003788nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003789 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3790{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003791 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003792 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003793
David Quigley1775fd32013-05-22 12:50:42 -04003794 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003795 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003796 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003797 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003798}
3799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3801{
Trond Myklebust76b32992007-08-10 17:45:11 -04003802 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 struct nfs4_accessargs args = {
3804 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003805 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003807 struct nfs4_accessres res = {
3808 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003809 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 struct rpc_message msg = {
3811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3812 .rpc_argp = &args,
3813 .rpc_resp = &res,
3814 .rpc_cred = entry->cred,
3815 };
3816 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003817 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 /*
3820 * Determine which access bits we want to ask for...
3821 */
3822 if (mode & MAY_READ)
3823 args.access |= NFS4_ACCESS_READ;
3824 if (S_ISDIR(inode->i_mode)) {
3825 if (mode & MAY_WRITE)
3826 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3827 if (mode & MAY_EXEC)
3828 args.access |= NFS4_ACCESS_LOOKUP;
3829 } else {
3830 if (mode & MAY_WRITE)
3831 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3832 if (mode & MAY_EXEC)
3833 args.access |= NFS4_ACCESS_EXECUTE;
3834 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003835
3836 res.fattr = nfs_alloc_fattr();
3837 if (res.fattr == NULL)
3838 return -ENOMEM;
3839
Bryan Schumaker7c513052011-03-24 17:12:24 +00003840 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003842 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003843 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003845 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 return status;
3847}
3848
3849static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3850{
3851 struct nfs4_exception exception = { };
3852 int err;
3853 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003854 err = _nfs4_proc_access(inode, entry);
3855 trace_nfs4_access(inode, err);
3856 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 &exception);
3858 } while (exception.retry);
3859 return err;
3860}
3861
3862/*
3863 * TODO: For the time being, we don't try to get any attributes
3864 * along with any of the zero-copy operations READ, READDIR,
3865 * READLINK, WRITE.
3866 *
3867 * In the case of the first three, we want to put the GETATTR
3868 * after the read-type operation -- this is because it is hard
3869 * to predict the length of a GETATTR response in v4, and thus
3870 * align the READ data correctly. This means that the GETATTR
3871 * may end up partially falling into the page cache, and we should
3872 * shift it into the 'tail' of the xdr_buf before processing.
3873 * To do this efficiently, we need to know the total length
3874 * of data received, which doesn't seem to be available outside
3875 * of the RPC layer.
3876 *
3877 * In the case of WRITE, we also want to put the GETATTR after
3878 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003879 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 *
3881 * Both of these changes to the XDR layer would in fact be quite
3882 * minor, but I decided to leave them for a subsequent patch.
3883 */
3884static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3885 unsigned int pgbase, unsigned int pglen)
3886{
3887 struct nfs4_readlink args = {
3888 .fh = NFS_FH(inode),
3889 .pgbase = pgbase,
3890 .pglen = pglen,
3891 .pages = &page,
3892 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003893 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 struct rpc_message msg = {
3895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3896 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003897 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 };
3899
Bryan Schumaker7c513052011-03-24 17:12:24 +00003900 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 -07003901}
3902
3903static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3904 unsigned int pgbase, unsigned int pglen)
3905{
3906 struct nfs4_exception exception = { };
3907 int err;
3908 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003909 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3910 trace_nfs4_readlink(inode, err);
3911 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 &exception);
3913 } while (exception.retry);
3914 return err;
3915}
3916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003918 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920static int
3921nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003922 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
David Quigleyaa9c2662013-05-22 12:50:44 -04003924 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003925 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 int status = 0;
3928
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003929 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3930 if (IS_ERR(ctx))
3931 return PTR_ERR(ctx);
3932
David Quigleyaa9c2662013-05-22 12:50:44 -04003933 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3934
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003935 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003936 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (IS_ERR(state)) {
3938 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003942 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003943 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return status;
3945}
3946
Al Virobeffb8f2016-07-20 16:34:42 -04003947static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Trond Myklebust16e42952005-10-27 22:12:44 -04003949 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003950 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003952 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003954 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003955 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3959 .rpc_argp = &args,
3960 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003962 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003963
Bryan Schumaker7c513052011-03-24 17:12:24 +00003964 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003965 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003966 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return status;
3968}
3969
Al Virobeffb8f2016-07-20 16:34:42 -04003970static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 struct nfs4_exception exception = { };
3973 int err;
3974 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003975 err = _nfs4_proc_remove(dir, name);
3976 trace_nfs4_remove(dir, name, err);
3977 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 &exception);
3979 } while (exception.retry);
3980 return err;
3981}
3982
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003983static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003985 struct nfs_server *server = NFS_SERVER(dir);
3986 struct nfs_removeargs *args = msg->rpc_argp;
3987 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003989 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003991 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003992
3993 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003996static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3997{
Al Viro884be172016-04-28 23:56:31 -04003998 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003999 &data->args.seq_args,
4000 &data->res.seq_res,
4001 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004004static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004006 struct nfs_unlinkdata *data = task->tk_calldata;
4007 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004008
Trond Myklebust14516c32010-07-31 14:29:06 -04004009 if (!nfs4_sequence_done(task, &res->seq_res))
4010 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004011 if (nfs4_async_handle_error(task, res->server, NULL,
4012 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004013 return 0;
4014 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
Jeff Laytond3d41522010-09-17 17:31:57 -04004018static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4019{
4020 struct nfs_server *server = NFS_SERVER(dir);
4021 struct nfs_renameargs *arg = msg->rpc_argp;
4022 struct nfs_renameres *res = msg->rpc_resp;
4023
4024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004025 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004026 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004027}
4028
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004029static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4030{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004031 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4032 &data->args.seq_args,
4033 &data->res.seq_res,
4034 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004035}
4036
4037static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4038 struct inode *new_dir)
4039{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004040 struct nfs_renamedata *data = task->tk_calldata;
4041 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004042
4043 if (!nfs4_sequence_done(task, &res->seq_res))
4044 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004045 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004046 return 0;
4047
4048 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004049 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004050 return 1;
4051}
4052
Al Virobeffb8f2016-07-20 16:34:42 -04004053static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004055 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 struct nfs4_link_arg arg = {
4057 .fh = NFS_FH(inode),
4058 .dir_fh = NFS_FH(dir),
4059 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004060 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004062 struct nfs4_link_res res = {
4063 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004064 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004065 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 struct rpc_message msg = {
4067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4068 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004069 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004071 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Trond Myklebust136f2622010-04-16 16:22:49 -04004073 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004074 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004075 goto out;
4076
David Quigley14c43f72013-05-22 12:50:43 -04004077 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4078 if (IS_ERR(res.label)) {
4079 status = PTR_ERR(res.label);
4080 goto out;
4081 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004082 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004083
Bryan Schumaker7c513052011-03-24 17:12:24 +00004084 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004085 if (!status) {
4086 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004087 status = nfs_post_op_update_inode(inode, res.fattr);
4088 if (!status)
4089 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004090 }
David Quigley14c43f72013-05-22 12:50:43 -04004091
4092
4093 nfs4_label_free(res.label);
4094
Trond Myklebust136f2622010-04-16 16:22:49 -04004095out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004096 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return status;
4098}
4099
Al Virobeffb8f2016-07-20 16:34:42 -04004100static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
4102 struct nfs4_exception exception = { };
4103 int err;
4104 do {
4105 err = nfs4_handle_exception(NFS_SERVER(inode),
4106 _nfs4_proc_link(inode, dir, name),
4107 &exception);
4108 } while (exception.retry);
4109 return err;
4110}
4111
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004112struct nfs4_createdata {
4113 struct rpc_message msg;
4114 struct nfs4_create_arg arg;
4115 struct nfs4_create_res res;
4116 struct nfs_fh fh;
4117 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004118 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004119};
4120
4121static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004122 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004123{
4124 struct nfs4_createdata *data;
4125
4126 data = kzalloc(sizeof(*data), GFP_KERNEL);
4127 if (data != NULL) {
4128 struct nfs_server *server = NFS_SERVER(dir);
4129
David Quigley14c43f72013-05-22 12:50:43 -04004130 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4131 if (IS_ERR(data->label))
4132 goto out_free;
4133
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004134 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4135 data->msg.rpc_argp = &data->arg;
4136 data->msg.rpc_resp = &data->res;
4137 data->arg.dir_fh = NFS_FH(dir);
4138 data->arg.server = server;
4139 data->arg.name = name;
4140 data->arg.attrs = sattr;
4141 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004142 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004143 data->res.server = server;
4144 data->res.fh = &data->fh;
4145 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004146 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004147 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004148 }
4149 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004150out_free:
4151 kfree(data);
4152 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004153}
4154
4155static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4156{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004157 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004158 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 if (status == 0) {
4160 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004161 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004162 }
4163 return status;
4164}
4165
4166static void nfs4_free_createdata(struct nfs4_createdata *data)
4167{
David Quigley14c43f72013-05-22 12:50:43 -04004168 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004169 kfree(data);
4170}
4171
Chuck Lever4f390c12006-08-22 20:06:22 -04004172static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004173 struct page *page, unsigned int len, struct iattr *sattr,
4174 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004176 struct nfs4_createdata *data;
4177 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Chuck Lever94a6d752006-08-22 20:06:23 -04004179 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004181
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004182 status = -ENOMEM;
4183 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4184 if (data == NULL)
4185 goto out;
4186
4187 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4188 data->arg.u.symlink.pages = &page;
4189 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004190 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192 status = nfs4_do_create(dir, dentry, data);
4193
4194 nfs4_free_createdata(data);
4195out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return status;
4197}
4198
Chuck Lever4f390c12006-08-22 20:06:22 -04004199static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004200 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
4202 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004203 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004205
4206 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004209 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4210 trace_nfs4_symlink(dir, &dentry->d_name, err);
4211 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 &exception);
4213 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004214
4215 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return err;
4217}
4218
4219static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004220 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004222 struct nfs4_createdata *data;
4223 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004225 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4226 if (data == NULL)
4227 goto out;
4228
David Quigley1775fd32013-05-22 12:50:42 -04004229 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230 status = nfs4_do_create(dir, dentry, data);
4231
4232 nfs4_free_createdata(data);
4233out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 return status;
4235}
4236
4237static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4238 struct iattr *sattr)
4239{
4240 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004241 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004243
David Quigleyaa9c2662013-05-22 12:50:44 -04004244 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4245
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004246 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004248 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4249 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4250 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 &exception);
4252 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004253 nfs4_label_release_security(label);
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return err;
4256}
4257
4258static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004259 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
David Howells2b0143b2015-03-17 22:25:59 +00004261 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 struct nfs4_readdir_arg args = {
4263 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004264 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 .pgbase = 0,
4266 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004267 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004268 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 };
4270 struct nfs4_readdir_res res;
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4273 .rpc_argp = &args,
4274 .rpc_resp = &res,
4275 .rpc_cred = cred,
4276 };
4277 int status;
4278
Al Viro6de14722013-09-16 10:53:17 -04004279 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4280 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004281 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004282 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004284 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 -05004285 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004286 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004287 status += args.pgbase;
4288 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004289
4290 nfs_invalidate_atime(dir);
4291
Harvey Harrison3110ff82008-05-02 13:42:44 -07004292 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return status;
4294}
4295
4296static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004297 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
4299 struct nfs4_exception exception = { };
4300 int err;
4301 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004302 err = _nfs4_proc_readdir(dentry, cred, cookie,
4303 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004304 trace_nfs4_readdir(d_inode(dentry), err);
4305 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 &exception);
4307 } while (exception.retry);
4308 return err;
4309}
4310
4311static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004312 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004314 struct nfs4_createdata *data;
4315 int mode = sattr->ia_mode;
4316 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004318 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4319 if (data == NULL)
4320 goto out;
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004323 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004325 data->arg.ftype = NF4BLK;
4326 data->arg.u.device.specdata1 = MAJOR(rdev);
4327 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
4329 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004330 data->arg.ftype = NF4CHR;
4331 data->arg.u.device.specdata1 = MAJOR(rdev);
4332 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004333 } else if (!S_ISSOCK(mode)) {
4334 status = -EINVAL;
4335 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
David Quigley1775fd32013-05-22 12:50:42 -04004337
David Quigleyaa9c2662013-05-22 12:50:44 -04004338 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004340out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004341 nfs4_free_createdata(data);
4342out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return status;
4344}
4345
4346static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4347 struct iattr *sattr, dev_t rdev)
4348{
4349 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004350 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004352
David Quigleyaa9c2662013-05-22 12:50:44 -04004353 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4354
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004355 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004357 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4358 trace_nfs4_mknod(dir, &dentry->d_name, err);
4359 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 &exception);
4361 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004362
4363 nfs4_label_release_security(label);
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return err;
4366}
4367
4368static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4369 struct nfs_fsstat *fsstat)
4370{
4371 struct nfs4_statfs_arg args = {
4372 .fh = fhandle,
4373 .bitmask = server->attr_bitmask,
4374 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004375 struct nfs4_statfs_res res = {
4376 .fsstat = fsstat,
4377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 struct rpc_message msg = {
4379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4380 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004381 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 };
4383
Trond Myklebust0e574af2005-10-27 22:12:38 -04004384 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004385 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
4387
4388static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4389{
4390 struct nfs4_exception exception = { };
4391 int err;
4392 do {
4393 err = nfs4_handle_exception(server,
4394 _nfs4_proc_statfs(server, fhandle, fsstat),
4395 &exception);
4396 } while (exception.retry);
4397 return err;
4398}
4399
4400static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4401 struct nfs_fsinfo *fsinfo)
4402{
4403 struct nfs4_fsinfo_arg args = {
4404 .fh = fhandle,
4405 .bitmask = server->attr_bitmask,
4406 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004407 struct nfs4_fsinfo_res res = {
4408 .fsinfo = fsinfo,
4409 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4412 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004413 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 };
4415
Bryan Schumaker7c513052011-03-24 17:12:24 +00004416 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
4419static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4420{
4421 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004422 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 int err;
4424
4425 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004426 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004427 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004428 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004429 nfs4_set_lease_period(server->nfs_client,
4430 fsinfo->lease_time * HZ,
4431 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004432 break;
4433 }
4434 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 } while (exception.retry);
4436 return err;
4437}
4438
4439static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4440{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004441 int error;
4442
Trond Myklebust0e574af2005-10-27 22:12:38 -04004443 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004444 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004445 if (error == 0) {
4446 /* block layout checks this! */
4447 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004448 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004449 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004450
4451 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
4454static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4455 struct nfs_pathconf *pathconf)
4456{
4457 struct nfs4_pathconf_arg args = {
4458 .fh = fhandle,
4459 .bitmask = server->attr_bitmask,
4460 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004461 struct nfs4_pathconf_res res = {
4462 .pathconf = pathconf,
4463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 struct rpc_message msg = {
4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4466 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004467 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 };
4469
4470 /* None of the pathconf attributes are mandatory to implement */
4471 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4472 memset(pathconf, 0, sizeof(*pathconf));
4473 return 0;
4474 }
4475
Trond Myklebust0e574af2005-10-27 22:12:38 -04004476 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004477 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
4480static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4481 struct nfs_pathconf *pathconf)
4482{
4483 struct nfs4_exception exception = { };
4484 int err;
4485
4486 do {
4487 err = nfs4_handle_exception(server,
4488 _nfs4_proc_pathconf(server, fhandle, pathconf),
4489 &exception);
4490 } while (exception.retry);
4491 return err;
4492}
4493
Trond Myklebust5521abf2013-03-16 20:54:34 -04004494int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004495 const struct nfs_open_context *ctx,
4496 const struct nfs_lock_context *l_ctx,
4497 fmode_t fmode)
4498{
4499 const struct nfs_lockowner *lockowner = NULL;
4500
4501 if (l_ctx != NULL)
4502 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004503 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004504}
4505EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4506
Trond Myklebust5521abf2013-03-16 20:54:34 -04004507static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4508 const struct nfs_open_context *ctx,
4509 const struct nfs_lock_context *l_ctx,
4510 fmode_t fmode)
4511{
4512 nfs4_stateid current_stateid;
4513
Trond Myklebuste1253be2014-03-05 08:44:23 -05004514 /* If the current stateid represents a lost lock, then exit */
4515 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4516 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004517 return nfs4_stateid_match(stateid, &current_stateid);
4518}
4519
4520static bool nfs4_error_stateid_expired(int err)
4521{
4522 switch (err) {
4523 case -NFS4ERR_DELEG_REVOKED:
4524 case -NFS4ERR_ADMIN_REVOKED:
4525 case -NFS4ERR_BAD_STATEID:
4526 case -NFS4ERR_STALE_STATEID:
4527 case -NFS4ERR_OLD_STATEID:
4528 case -NFS4ERR_OPENMODE:
4529 case -NFS4ERR_EXPIRED:
4530 return true;
4531 }
4532 return false;
4533}
4534
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004535void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004536{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004537 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004538}
4539
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004540static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004542 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004545 if (task->tk_status < 0) {
4546 struct nfs4_exception exception = {
4547 .inode = hdr->inode,
4548 .state = hdr->args.context->state,
4549 .stateid = &hdr->args.stateid,
4550 };
4551 task->tk_status = nfs4_async_handle_exception(task,
4552 server, task->tk_status, &exception);
4553 if (exception.retry) {
4554 rpc_restart_call_prepare(task);
4555 return -EAGAIN;
4556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004558
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004559 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004561 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
Trond Myklebust5521abf2013-03-16 20:54:34 -04004565static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004566 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004567{
4568
4569 if (!nfs4_error_stateid_expired(task->tk_status) ||
4570 nfs4_stateid_is_current(&args->stateid,
4571 args->context,
4572 args->lock_context,
4573 FMODE_READ))
4574 return false;
4575 rpc_restart_call_prepare(task);
4576 return true;
4577}
4578
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004579static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004580{
4581
4582 dprintk("--> %s\n", __func__);
4583
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004584 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004585 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004586 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004587 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004588 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4589 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004590}
4591
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004592static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4593 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004595 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004596 if (!hdr->pgio_done_cb)
4597 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004598 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004599 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004602static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4603 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004604{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4606 &hdr->args.seq_args,
4607 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004608 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004609 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4611 hdr->args.lock_context,
4612 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004613 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004614 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004615 return -EIO;
4616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004619static int nfs4_write_done_cb(struct rpc_task *task,
4620 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004622 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004623
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004624 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004625 if (task->tk_status < 0) {
4626 struct nfs4_exception exception = {
4627 .inode = hdr->inode,
4628 .state = hdr->args.context->state,
4629 .stateid = &hdr->args.stateid,
4630 };
4631 task->tk_status = nfs4_async_handle_exception(task,
4632 NFS_SERVER(inode), task->tk_status,
4633 &exception);
4634 if (exception.retry) {
4635 rpc_restart_call_prepare(task);
4636 return -EAGAIN;
4637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004639 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004641 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004642 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644}
4645
Trond Myklebust5521abf2013-03-16 20:54:34 -04004646static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004647 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004648{
4649
4650 if (!nfs4_error_stateid_expired(task->tk_status) ||
4651 nfs4_stateid_is_current(&args->stateid,
4652 args->context,
4653 args->lock_context,
4654 FMODE_WRITE))
4655 return false;
4656 rpc_restart_call_prepare(task);
4657 return true;
4658}
4659
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004661{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004662 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004663 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004665 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004666 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4667 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004668}
4669
Trond Myklebust5a37f852012-04-28 14:55:16 -04004670static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004672{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004673 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004675 return false;
4676 /* Otherwise, request attributes if and only if we don't hold
4677 * a delegation
4678 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004679 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004680}
Fred Isamana69aef12011-03-03 15:13:47 +00004681
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004682static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4683 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004686
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4688 hdr->args.bitmask = NULL;
4689 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004690 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004692
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 if (!hdr->pgio_done_cb)
4694 hdr->pgio_done_cb = nfs4_write_done_cb;
4695 hdr->res.server = server;
4696 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004698 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
Fred Isaman0b7c0152012-04-20 14:47:39 -04004702static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4703{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004704 nfs4_setup_sequence(NFS_SERVER(data->inode),
4705 &data->args.seq_args,
4706 &data->res.seq_res,
4707 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
Fred Isaman0b7c0152012-04-20 14:47:39 -04004710static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004713
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004714 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004715 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4716 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004717 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004718 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Fred Isaman0b7c0152012-04-20 14:47:39 -04004723static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004724{
4725 if (!nfs4_sequence_done(task, &data->res.seq_res))
4726 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004727 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004728}
4729
Fred Isaman0b7c0152012-04-20 14:47:39 -04004730static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004732 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004733
Fred Isaman0b7c0152012-04-20 14:47:39 -04004734 if (data->commit_done_cb == NULL)
4735 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004736 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004737 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004738 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004741struct nfs4_renewdata {
4742 struct nfs_client *client;
4743 unsigned long timestamp;
4744};
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746/*
4747 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4748 * standalone procedure for queueing an asynchronous RENEW.
4749 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004750static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004751{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004752 struct nfs4_renewdata *data = calldata;
4753 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004754
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004755 if (atomic_read(&clp->cl_count) > 1)
4756 nfs4_schedule_state_renewal(clp);
4757 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004758 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004759}
4760
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004761static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004763 struct nfs4_renewdata *data = calldata;
4764 struct nfs_client *clp = data->client;
4765 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004767 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004768 switch (task->tk_status) {
4769 case 0:
4770 break;
4771 case -NFS4ERR_LEASE_MOVED:
4772 nfs4_schedule_lease_moved_recovery(clp);
4773 break;
4774 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004775 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004776 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4777 return;
4778 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004779 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004780 return;
4781 }
4782 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004784 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004787static const struct rpc_call_ops nfs4_renew_ops = {
4788 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004789 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004790};
4791
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004792static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
4794 struct rpc_message msg = {
4795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4796 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004797 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004799 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004801 if (renew_flags == 0)
4802 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004803 if (!atomic_inc_not_zero(&clp->cl_count))
4804 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004805 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004806 if (data == NULL)
4807 return -ENOMEM;
4808 data->client = clp;
4809 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004810 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004811 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
4813
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004814static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
4816 struct rpc_message msg = {
4817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4818 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004819 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 };
4821 unsigned long now = jiffies;
4822 int status;
4823
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004824 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (status < 0)
4826 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004827 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 return 0;
4829}
4830
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004831static inline int nfs4_server_supports_acls(struct nfs_server *server)
4832{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004833 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004834}
4835
Trond Myklebust21f498c2012-08-24 10:59:25 -04004836/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4837 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004838 * the stack.
4839 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004840#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004841
Neil Hormane9e3d722011-03-04 19:26:03 -05004842static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004843 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004844{
4845 struct page *newpage, **spages;
4846 int rc = 0;
4847 size_t len;
4848 spages = pages;
4849
4850 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004851 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004852 newpage = alloc_page(GFP_KERNEL);
4853
4854 if (newpage == NULL)
4855 goto unwind;
4856 memcpy(page_address(newpage), buf, len);
4857 buf += len;
4858 buflen -= len;
4859 *pages++ = newpage;
4860 rc++;
4861 } while (buflen != 0);
4862
4863 return rc;
4864
4865unwind:
4866 for(; rc > 0; rc--)
4867 __free_page(spages[rc-1]);
4868 return -ENOMEM;
4869}
4870
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004871struct nfs4_cached_acl {
4872 int cached;
4873 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004874 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004875};
4876
4877static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004878{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004879 struct nfs_inode *nfsi = NFS_I(inode);
4880
4881 spin_lock(&inode->i_lock);
4882 kfree(nfsi->nfs4_acl);
4883 nfsi->nfs4_acl = acl;
4884 spin_unlock(&inode->i_lock);
4885}
4886
4887static void nfs4_zap_acl_attr(struct inode *inode)
4888{
4889 nfs4_set_cached_acl(inode, NULL);
4890}
4891
4892static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4893{
4894 struct nfs_inode *nfsi = NFS_I(inode);
4895 struct nfs4_cached_acl *acl;
4896 int ret = -ENOENT;
4897
4898 spin_lock(&inode->i_lock);
4899 acl = nfsi->nfs4_acl;
4900 if (acl == NULL)
4901 goto out;
4902 if (buf == NULL) /* user is just asking for length */
4903 goto out_len;
4904 if (acl->cached == 0)
4905 goto out;
4906 ret = -ERANGE; /* see getxattr(2) man page */
4907 if (acl->len > buflen)
4908 goto out;
4909 memcpy(buf, acl->data, acl->len);
4910out_len:
4911 ret = acl->len;
4912out:
4913 spin_unlock(&inode->i_lock);
4914 return ret;
4915}
4916
Sachin Prabhu5794d212012-04-17 14:36:40 +01004917static 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 +00004918{
4919 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004920 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004921
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004922 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004923 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004924 if (acl == NULL)
4925 goto out;
4926 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004927 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004928 } else {
4929 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4930 if (acl == NULL)
4931 goto out;
4932 acl->cached = 0;
4933 }
4934 acl->len = acl_len;
4935out:
4936 nfs4_set_cached_acl(inode, acl);
4937}
4938
Andy Adamsonbf118a32011-12-07 11:55:27 -05004939/*
4940 * The getxattr API returns the required buffer length when called with a
4941 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4942 * the required buf. On a NULL buf, we send a page of data to the server
4943 * guessing that the ACL request can be serviced by a page. If so, we cache
4944 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4945 * the cache. If not so, we throw away the page, and cache the required
4946 * length. The next getxattr call will then produce another round trip to
4947 * the server, this time with the input buf of the required size.
4948 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004949static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004950{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004951 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004952 struct nfs_getaclargs args = {
4953 .fh = NFS_FH(inode),
4954 .acl_pages = pages,
4955 .acl_len = buflen,
4956 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004957 struct nfs_getaclres res = {
4958 .acl_len = buflen,
4959 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004960 struct rpc_message msg = {
4961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4962 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004963 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004964 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004965 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4966 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004967
Andy Adamsonbf118a32011-12-07 11:55:27 -05004968 /* As long as we're doing a round trip to the server anyway,
4969 * let's be prepared for a page of acl data. */
4970 if (npages == 0)
4971 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004972 if (npages > ARRAY_SIZE(pages))
4973 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004974
Andy Adamsonbf118a32011-12-07 11:55:27 -05004975 for (i = 0; i < npages; i++) {
4976 pages[i] = alloc_page(GFP_KERNEL);
4977 if (!pages[i])
4978 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004979 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004980
4981 /* for decoding across pages */
4982 res.acl_scratch = alloc_page(GFP_KERNEL);
4983 if (!res.acl_scratch)
4984 goto out_free;
4985
Andy Adamsonbf118a32011-12-07 11:55:27 -05004986 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004987
Peng Taode040be2012-01-10 22:42:47 +08004988 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004989 __func__, buf, buflen, npages, args.acl_len);
4990 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4991 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004992 if (ret)
4993 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004994
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004995 /* Handle the case where the passed-in buffer is too short */
4996 if (res.acl_flags & NFS4_ACL_TRUNC) {
4997 /* Did the user only issue a request for the acl length? */
4998 if (buf == NULL)
4999 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005000 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005001 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005002 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005003 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005004 if (buf) {
5005 if (res.acl_len > buflen) {
5006 ret = -ERANGE;
5007 goto out_free;
5008 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005009 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005010 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005011out_ok:
5012 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005013out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005014 for (i = 0; i < npages; i++)
5015 if (pages[i])
5016 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005017 if (res.acl_scratch)
5018 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005019 return ret;
5020}
5021
Trond Myklebust16b42892006-08-24 12:27:15 -04005022static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5023{
5024 struct nfs4_exception exception = { };
5025 ssize_t ret;
5026 do {
5027 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005028 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005029 if (ret >= 0)
5030 break;
5031 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5032 } while (exception.retry);
5033 return ret;
5034}
5035
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005036static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5037{
5038 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005039 int ret;
5040
5041 if (!nfs4_server_supports_acls(server))
5042 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005043 ret = nfs_revalidate_inode(server, inode);
5044 if (ret < 0)
5045 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005046 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5047 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005048 ret = nfs4_read_cached_acl(inode, buf, buflen);
5049 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005050 /* -ENOENT is returned if there is no ACL or if there is an ACL
5051 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005052 return ret;
5053 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054}
5055
Trond Myklebust16b42892006-08-24 12:27:15 -04005056static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005057{
5058 struct nfs_server *server = NFS_SERVER(inode);
5059 struct page *pages[NFS4ACL_MAXPAGES];
5060 struct nfs_setaclargs arg = {
5061 .fh = NFS_FH(inode),
5062 .acl_pages = pages,
5063 .acl_len = buflen,
5064 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005065 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005066 struct rpc_message msg = {
5067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5068 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005069 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005070 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005071 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005072 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005073
5074 if (!nfs4_server_supports_acls(server))
5075 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005076 if (npages > ARRAY_SIZE(pages))
5077 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005078 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005079 if (i < 0)
5080 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005081 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005082 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005083
5084 /*
5085 * Free each page after tx, so the only ref left is
5086 * held by the network stack
5087 */
5088 for (; i > 0; i--)
5089 put_page(pages[i-1]);
5090
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005091 /*
5092 * Acl update can result in inode attribute update.
5093 * so mark the attribute cache invalid.
5094 */
5095 spin_lock(&inode->i_lock);
5096 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5097 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005098 nfs_access_zap_cache(inode);
5099 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005100 return ret;
5101}
5102
Trond Myklebust16b42892006-08-24 12:27:15 -04005103static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5104{
5105 struct nfs4_exception exception = { };
5106 int err;
5107 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005108 err = __nfs4_proc_set_acl(inode, buf, buflen);
5109 trace_nfs4_set_acl(inode, err);
5110 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005111 &exception);
5112 } while (exception.retry);
5113 return err;
5114}
5115
David Quigleyaa9c2662013-05-22 12:50:44 -04005116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5117static int _nfs4_get_security_label(struct inode *inode, void *buf,
5118 size_t buflen)
5119{
5120 struct nfs_server *server = NFS_SERVER(inode);
5121 struct nfs_fattr fattr;
5122 struct nfs4_label label = {0, 0, buflen, buf};
5123
5124 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005125 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005126 .fh = NFS_FH(inode),
5127 .bitmask = bitmask,
5128 };
5129 struct nfs4_getattr_res res = {
5130 .fattr = &fattr,
5131 .label = &label,
5132 .server = server,
5133 };
5134 struct rpc_message msg = {
5135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005136 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005137 .rpc_resp = &res,
5138 };
5139 int ret;
5140
5141 nfs_fattr_init(&fattr);
5142
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005143 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005144 if (ret)
5145 return ret;
5146 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5147 return -ENOENT;
5148 if (buflen < label.len)
5149 return -ERANGE;
5150 return 0;
5151}
5152
5153static int nfs4_get_security_label(struct inode *inode, void *buf,
5154 size_t buflen)
5155{
5156 struct nfs4_exception exception = { };
5157 int err;
5158
5159 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5160 return -EOPNOTSUPP;
5161
5162 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005163 err = _nfs4_get_security_label(inode, buf, buflen);
5164 trace_nfs4_get_security_label(inode, err);
5165 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005166 &exception);
5167 } while (exception.retry);
5168 return err;
5169}
5170
5171static int _nfs4_do_set_security_label(struct inode *inode,
5172 struct nfs4_label *ilabel,
5173 struct nfs_fattr *fattr,
5174 struct nfs4_label *olabel)
5175{
5176
5177 struct iattr sattr = {0};
5178 struct nfs_server *server = NFS_SERVER(inode);
5179 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005180 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005181 .fh = NFS_FH(inode),
5182 .iap = &sattr,
5183 .server = server,
5184 .bitmask = bitmask,
5185 .label = ilabel,
5186 };
5187 struct nfs_setattrres res = {
5188 .fattr = fattr,
5189 .label = olabel,
5190 .server = server,
5191 };
5192 struct rpc_message msg = {
5193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005194 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005195 .rpc_resp = &res,
5196 };
5197 int status;
5198
Jeff Layton12207f62013-11-01 10:49:32 -04005199 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005200
Jeff Layton12207f62013-11-01 10:49:32 -04005201 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005202 if (status)
5203 dprintk("%s failed: %d\n", __func__, status);
5204
5205 return status;
5206}
5207
5208static int nfs4_do_set_security_label(struct inode *inode,
5209 struct nfs4_label *ilabel,
5210 struct nfs_fattr *fattr,
5211 struct nfs4_label *olabel)
5212{
5213 struct nfs4_exception exception = { };
5214 int err;
5215
5216 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005217 err = _nfs4_do_set_security_label(inode, ilabel,
5218 fattr, olabel);
5219 trace_nfs4_set_security_label(inode, err);
5220 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005221 &exception);
5222 } while (exception.retry);
5223 return err;
5224}
5225
5226static int
Al Viro59301222016-05-27 10:19:30 -04005227nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005228{
5229 struct nfs4_label ilabel, *olabel = NULL;
5230 struct nfs_fattr fattr;
5231 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005232 int status;
5233
5234 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5235 return -EOPNOTSUPP;
5236
5237 nfs_fattr_init(&fattr);
5238
5239 ilabel.pi = 0;
5240 ilabel.lfs = 0;
5241 ilabel.label = (char *)buf;
5242 ilabel.len = buflen;
5243
5244 cred = rpc_lookup_cred();
5245 if (IS_ERR(cred))
5246 return PTR_ERR(cred);
5247
5248 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5249 if (IS_ERR(olabel)) {
5250 status = -PTR_ERR(olabel);
5251 goto out;
5252 }
5253
5254 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5255 if (status == 0)
5256 nfs_setsecurity(inode, &fattr, olabel);
5257
5258 nfs4_label_free(olabel);
5259out:
5260 put_rpccred(cred);
5261 return status;
5262}
5263#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5264
5265
Chuck Leverf0920752012-05-21 22:45:41 -04005266static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5267 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005268{
5269 __be32 verf[2];
5270
Chuck Lever2c820d92012-05-21 22:45:33 -04005271 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5272 /* An impossible timestamp guarantees this value
5273 * will never match a generated boot time. */
5274 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005275 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005276 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005277 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005278 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5279 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005280 }
Chuck Levercd937102012-03-02 17:14:31 -05005281 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5282}
5283
Jeff Laytona3192682015-06-09 19:43:59 -04005284static int
5285nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005286{
Jeff Laytona3192682015-06-09 19:43:59 -04005287 size_t len;
5288 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005289
Trond Myklebustceb3a162015-01-03 15:16:04 -05005290 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005291 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005292
Jeff Laytona3192682015-06-09 19:43:59 -04005293 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005294 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005295 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5296 1 +
5297 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5298 1;
5299 rcu_read_unlock();
5300
5301 if (len > NFS4_OPAQUE_LIMIT + 1)
5302 return -EINVAL;
5303
5304 /*
5305 * Since this string is allocated at mount time, and held until the
5306 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5307 * about a memory-reclaim deadlock.
5308 */
5309 str = kmalloc(len, GFP_KERNEL);
5310 if (!str)
5311 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005312
Chuck Levere984a552012-09-14 17:24:21 -04005313 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005314 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005315 clp->cl_ipaddr,
5316 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5317 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005318 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005319
Jeff Laytona3192682015-06-09 19:43:59 -04005320 clp->cl_owner_id = str;
5321 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005322}
5323
Jeff Layton873e3852015-06-09 19:44:00 -04005324static int
5325nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005326{
Jeff Layton873e3852015-06-09 19:44:00 -04005327 size_t len;
5328 char *str;
5329
5330 len = 10 + 10 + 1 + 10 + 1 +
5331 strlen(nfs4_client_id_uniquifier) + 1 +
5332 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5333
5334 if (len > NFS4_OPAQUE_LIMIT + 1)
5335 return -EINVAL;
5336
5337 /*
5338 * Since this string is allocated at mount time, and held until the
5339 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5340 * about a memory-reclaim deadlock.
5341 */
5342 str = kmalloc(len, GFP_KERNEL);
5343 if (!str)
5344 return -ENOMEM;
5345
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005346 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005347 clp->rpc_ops->version, clp->cl_minorversion,
5348 nfs4_client_id_uniquifier,
5349 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005350 clp->cl_owner_id = str;
5351 return 0;
5352}
5353
5354static int
5355nfs4_init_uniform_client_string(struct nfs_client *clp)
5356{
Jeff Layton873e3852015-06-09 19:44:00 -04005357 size_t len;
5358 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005359
5360 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005361 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005362
5363 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005364 return nfs4_init_uniquifier_client_string(clp);
5365
5366 len = 10 + 10 + 1 + 10 + 1 +
5367 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5368
5369 if (len > NFS4_OPAQUE_LIMIT + 1)
5370 return -EINVAL;
5371
5372 /*
5373 * Since this string is allocated at mount time, and held until the
5374 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5375 * about a memory-reclaim deadlock.
5376 */
5377 str = kmalloc(len, GFP_KERNEL);
5378 if (!str)
5379 return -ENOMEM;
5380
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005381 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005382 clp->rpc_ops->version, clp->cl_minorversion,
5383 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005384 clp->cl_owner_id = str;
5385 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005386}
5387
Chuck Lever706cb8d2014-03-12 12:51:47 -04005388/*
5389 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5390 * services. Advertise one based on the address family of the
5391 * clientaddr.
5392 */
5393static unsigned int
5394nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5395{
5396 if (strchr(clp->cl_ipaddr, ':') != NULL)
5397 return scnprintf(buf, len, "tcp6");
5398 else
5399 return scnprintf(buf, len, "tcp");
5400}
5401
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005402static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5403{
5404 struct nfs4_setclientid *sc = calldata;
5405
5406 if (task->tk_status == 0)
5407 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5408}
5409
5410static const struct rpc_call_ops nfs4_setclientid_ops = {
5411 .rpc_call_done = nfs4_setclientid_done,
5412};
5413
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005414/**
5415 * nfs4_proc_setclientid - Negotiate client ID
5416 * @clp: state data structure
5417 * @program: RPC program for NFSv4 callback service
5418 * @port: IP port number for NFS4 callback service
5419 * @cred: RPC credential to use for this call
5420 * @res: where to place the result
5421 *
5422 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5423 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005424int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5425 unsigned short port, struct rpc_cred *cred,
5426 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
5428 nfs4_verifier sc_verifier;
5429 struct nfs4_setclientid setclientid = {
5430 .sc_verifier = &sc_verifier,
5431 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005432 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 };
5434 struct rpc_message msg = {
5435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5436 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005437 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005438 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005440 struct rpc_task *task;
5441 struct rpc_task_setup task_setup_data = {
5442 .rpc_client = clp->cl_rpcclient,
5443 .rpc_message = &msg,
5444 .callback_ops = &nfs4_setclientid_ops,
5445 .callback_data = &setclientid,
5446 .flags = RPC_TASK_TIMEOUT,
5447 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005448 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Chuck Leverde734832012-07-11 16:30:50 -04005450 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005451 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005452
5453 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5454 status = nfs4_init_uniform_client_string(clp);
5455 else
Jeff Laytona3192682015-06-09 19:43:59 -04005456 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005457
5458 if (status)
5459 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005460
Chuck Leverde734832012-07-11 16:30:50 -04005461 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005462 setclientid.sc_netid_len =
5463 nfs4_init_callback_netid(clp,
5464 setclientid.sc_netid,
5465 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005466 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005467 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 clp->cl_ipaddr, port >> 8, port & 255);
5469
Jeff Layton3a6bb732015-06-09 19:43:57 -04005470 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005471 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005472 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005473 task = rpc_run_task(&task_setup_data);
5474 if (IS_ERR(task)) {
5475 status = PTR_ERR(task);
5476 goto out;
5477 }
5478 status = task->tk_status;
5479 if (setclientid.sc_cred) {
5480 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5481 put_rpccred(setclientid.sc_cred);
5482 }
5483 rpc_put_task(task);
5484out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005485 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005486 dprintk("NFS reply setclientid: %d\n", status);
5487 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488}
5489
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005490/**
5491 * nfs4_proc_setclientid_confirm - Confirm client ID
5492 * @clp: state data structure
5493 * @res: result of a previous SETCLIENTID
5494 * @cred: RPC credential to use for this call
5495 *
5496 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5497 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005498int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005499 struct nfs4_setclientid_res *arg,
5500 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 struct rpc_message msg = {
5503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005504 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005505 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 int status;
5508
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005509 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5510 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5511 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005512 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005513 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005514 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 return status;
5516}
5517
Trond Myklebustfe650402006-01-03 09:55:18 +01005518struct nfs4_delegreturndata {
5519 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005520 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005521 struct nfs_fh fh;
5522 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005523 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005524 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005525 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005526 struct inode *inode;
5527 bool roc;
5528 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005529};
5530
Trond Myklebustfe650402006-01-03 09:55:18 +01005531static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5532{
5533 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005534
Trond Myklebust14516c32010-07-31 14:29:06 -04005535 if (!nfs4_sequence_done(task, &data->res.seq_res))
5536 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005537
Trond Myklebustca8acf82013-08-13 10:36:56 -04005538 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005539 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005540 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005541 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005542 case -NFS4ERR_ADMIN_REVOKED:
5543 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005544 case -NFS4ERR_EXPIRED:
5545 nfs4_free_revoked_stateid(data->res.server,
5546 data->args.stateid,
5547 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005548 case -NFS4ERR_BAD_STATEID:
5549 case -NFS4ERR_OLD_STATEID:
5550 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005551 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005552 if (data->roc)
5553 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005554 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005555 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005556 if (nfs4_async_handle_error(task, data->res.server,
5557 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005558 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005559 return;
5560 }
5561 }
5562 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005563}
5564
5565static void nfs4_delegreturn_release(void *calldata)
5566{
Peng Tao039b7562014-07-03 13:05:02 +08005567 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005568 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005569
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005570 if (inode) {
5571 if (data->roc)
5572 pnfs_roc_release(inode);
5573 nfs_iput_and_deactive(inode);
5574 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005575 kfree(calldata);
5576}
5577
Andy Adamson938e1012009-04-01 09:22:28 -04005578static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5579{
5580 struct nfs4_delegreturndata *d_data;
5581
5582 d_data = (struct nfs4_delegreturndata *)data;
5583
Peng Tao500d7012015-09-22 11:35:22 +08005584 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5585 return;
5586
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005587 if (d_data->roc)
5588 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005589
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005590 nfs4_setup_sequence(d_data->res.server,
5591 &d_data->args.seq_args,
5592 &d_data->res.seq_res,
5593 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005594}
Andy Adamson938e1012009-04-01 09:22:28 -04005595
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005596static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005597 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005598 .rpc_call_done = nfs4_delegreturn_done,
5599 .rpc_release = nfs4_delegreturn_release,
5600};
5601
Trond Myklebuste6f81072008-01-24 18:14:34 -05005602static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005603{
5604 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005605 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005606 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005607 struct rpc_message msg = {
5608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5609 .rpc_cred = cred,
5610 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005611 struct rpc_task_setup task_setup_data = {
5612 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005613 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005614 .callback_ops = &nfs4_delegreturn_ops,
5615 .flags = RPC_TASK_ASYNC,
5616 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005617 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005618
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005619 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005620 if (data == NULL)
5621 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005622 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005623
5624 nfs4_state_protect(server->nfs_client,
5625 NFS_SP4_MACH_CRED_CLEANUP,
5626 &task_setup_data.rpc_client, &msg);
5627
Trond Myklebustfe650402006-01-03 09:55:18 +01005628 data->args.fhandle = &data->fh;
5629 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005630 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005631 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005632 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005633 data->res.fattr = &data->fattr;
5634 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005635 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005636 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005637 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005638 data->inode = nfs_igrab_and_active(inode);
5639 if (data->inode)
5640 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005641
Trond Myklebustc970aa82007-07-14 15:39:59 -04005642 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005643 msg.rpc_argp = &data->args;
5644 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005645 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005646 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005647 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005648 if (!issync)
5649 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005650 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005651 if (status != 0)
5652 goto out;
5653 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005654 if (status == 0)
5655 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5656 else
5657 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005658out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005659 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005660 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
5662
Trond Myklebuste6f81072008-01-24 18:14:34 -05005663int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664{
5665 struct nfs_server *server = NFS_SERVER(inode);
5666 struct nfs4_exception exception = { };
5667 int err;
5668 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005669 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005670 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 switch (err) {
5672 case -NFS4ERR_STALE_STATEID:
5673 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 case 0:
5675 return 0;
5676 }
5677 err = nfs4_handle_exception(server, err, &exception);
5678 } while (exception.retry);
5679 return err;
5680}
5681
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5683{
5684 struct inode *inode = state->inode;
5685 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005686 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005687 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005689 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005691 struct nfs_lockt_res res = {
5692 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 };
5694 struct rpc_message msg = {
5695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5696 .rpc_argp = &arg,
5697 .rpc_resp = &res,
5698 .rpc_cred = state->owner->so_cred,
5699 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 struct nfs4_lock_state *lsp;
5701 int status;
5702
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005703 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005704 status = nfs4_set_lock_state(state, request);
5705 if (status != 0)
5706 goto out;
5707 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005708 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005709 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005710 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005711 switch (status) {
5712 case 0:
5713 request->fl_type = F_UNLCK;
5714 break;
5715 case -NFS4ERR_DENIED:
5716 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005718 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005719 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 return status;
5722}
5723
5724static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5725{
5726 struct nfs4_exception exception = { };
5727 int err;
5728
5729 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005730 err = _nfs4_proc_getlk(state, cmd, request);
5731 trace_nfs4_get_lock(request, state, cmd, err);
5732 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 &exception);
5734 } while (exception.retry);
5735 return err;
5736}
5737
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005738struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005739 struct nfs_locku_args arg;
5740 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005741 struct nfs4_lock_state *lsp;
5742 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005744 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005745 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005746};
5747
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005748static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5749 struct nfs_open_context *ctx,
5750 struct nfs4_lock_state *lsp,
5751 struct nfs_seqid *seqid)
5752{
5753 struct nfs4_unlockdata *p;
5754 struct inode *inode = lsp->ls_state->inode;
5755
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005756 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005757 if (p == NULL)
5758 return NULL;
5759 p->arg.fh = NFS_FH(inode);
5760 p->arg.fl = &p->fl;
5761 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005762 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005763 p->lsp = lsp;
5764 atomic_inc(&lsp->ls_count);
5765 /* Ensure we don't close file until we're done freeing locks! */
5766 p->ctx = get_nfs_open_context(ctx);
5767 memcpy(&p->fl, fl, sizeof(p->fl));
5768 p->server = NFS_SERVER(inode);
5769 return p;
5770}
5771
Trond Myklebust06f814a2006-01-03 09:55:07 +01005772static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005773{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005774 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005775 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005776 nfs4_put_lock_state(calldata->lsp);
5777 put_nfs_open_context(calldata->ctx);
5778 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005779}
5780
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005781static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005782{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005783 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005784
Trond Myklebust14516c32010-07-31 14:29:06 -04005785 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5786 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005787 switch (task->tk_status) {
5788 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005789 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005790 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005791 if (nfs4_update_lock_stateid(calldata->lsp,
5792 &calldata->res.stateid))
5793 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005794 case -NFS4ERR_ADMIN_REVOKED:
5795 case -NFS4ERR_EXPIRED:
5796 nfs4_free_revoked_stateid(calldata->server,
5797 &calldata->arg.stateid,
5798 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005799 case -NFS4ERR_BAD_STATEID:
5800 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005801 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005802 if (!nfs4_stateid_match(&calldata->arg.stateid,
5803 &calldata->lsp->ls_stateid))
5804 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005805 break;
5806 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005807 if (nfs4_async_handle_error(task, calldata->server,
5808 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005809 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005810 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005811 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005812}
5813
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005814static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005815{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005816 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005818 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005819 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005820 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005821 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005822 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005823 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005824 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005825 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005826 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005827 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005828 &calldata->res.seq_res,
5829 task) != 0)
5830 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005831 return;
5832out_no_action:
5833 task->tk_action = NULL;
5834out_wait:
5835 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836}
5837
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005838static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005839 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005840 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005841 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005842};
5843
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005844static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5845 struct nfs_open_context *ctx,
5846 struct nfs4_lock_state *lsp,
5847 struct nfs_seqid *seqid)
5848{
5849 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005850 struct rpc_message msg = {
5851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5852 .rpc_cred = ctx->cred,
5853 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005854 struct rpc_task_setup task_setup_data = {
5855 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005856 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005857 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005858 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005859 .flags = RPC_TASK_ASYNC,
5860 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005861
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005862 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5863 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5864
Frank Filz137d6ac2007-07-09 15:32:29 -07005865 /* Ensure this is an unlock - when canceling a lock, the
5866 * canceled lock is passed in, and it won't be an unlock.
5867 */
5868 fl->fl_type = F_UNLCK;
5869
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005870 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5871 if (data == NULL) {
5872 nfs_free_seqid(seqid);
5873 return ERR_PTR(-ENOMEM);
5874 }
5875
Chuck Levera9c92d62013-08-09 12:48:18 -04005876 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005877 msg.rpc_argp = &data->arg;
5878 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005879 task_setup_data.callback_data = data;
5880 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881}
5882
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5884{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005885 struct inode *inode = state->inode;
5886 struct nfs4_state_owner *sp = state->owner;
5887 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005888 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005889 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005890 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005891 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005892 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005893 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Trond Myklebust9b073572006-06-29 16:38:34 -04005895 status = nfs4_set_lock_state(state, request);
5896 /* Unlock _before_ we do the RPC call */
5897 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005898 /* Exclude nfs_delegation_claim_locks() */
5899 mutex_lock(&sp->so_delegreturn_mutex);
5900 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005901 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005902 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005903 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005904 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005905 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005906 }
5907 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005908 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005909 if (status != 0)
5910 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005911 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005912 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005913 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5914 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005915 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5916 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005917 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005918 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005919 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005920 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005921 status = PTR_ERR(task);
5922 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005923 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005924 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005925 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005926out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005927 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005928 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005929 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930}
5931
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005932struct nfs4_lockdata {
5933 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005934 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935 struct nfs4_lock_state *lsp;
5936 struct nfs_open_context *ctx;
5937 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005938 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005939 int rpc_status;
5940 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005941 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005942};
5943
5944static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005945 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5946 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005947{
5948 struct nfs4_lockdata *p;
5949 struct inode *inode = lsp->ls_state->inode;
5950 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005951 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005952
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005953 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005954 if (p == NULL)
5955 return NULL;
5956
5957 p->arg.fh = NFS_FH(inode);
5958 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005959 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005960 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005961 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005962 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5963 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005964 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005965 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005966 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005967 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005968 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005969 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005970 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005971 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005972 atomic_inc(&lsp->ls_count);
5973 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005974 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005975 memcpy(&p->fl, fl, sizeof(p->fl));
5976 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005977out_free_seqid:
5978 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979out_free:
5980 kfree(p);
5981 return NULL;
5982}
5983
5984static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5985{
5986 struct nfs4_lockdata *data = calldata;
5987 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
Harvey Harrison3110ff82008-05-02 13:42:44 -07005989 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005990 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005991 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005993 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005994 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005995 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005996 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005997 nfs4_stateid_copy(&data->arg.open_stateid,
5998 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006000 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006001 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006002 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006003 nfs4_stateid_copy(&data->arg.lock_stateid,
6004 &data->lsp->ls_stateid);
6005 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006006 if (!nfs4_valid_open_stateid(state)) {
6007 data->rpc_status = -EBADF;
6008 task->tk_action = NULL;
6009 goto out_release_open_seqid;
6010 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006011 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006012 if (nfs4_setup_sequence(data->server,
6013 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006014 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006015 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006016 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006017out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006018 nfs_release_seqid(data->arg.open_seqid);
6019out_release_lock_seqid:
6020 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006021out_wait:
6022 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006023 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006024}
6025
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006026static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6027{
6028 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006029 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030
Harvey Harrison3110ff82008-05-02 13:42:44 -07006031 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006032
Trond Myklebust14516c32010-07-31 14:29:06 -04006033 if (!nfs4_sequence_done(task, &data->res.seq_res))
6034 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006035
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006036 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006037 switch (task->tk_status) {
6038 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006039 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006040 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006041 if (data->arg.new_lock) {
6042 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006043 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006044 rpc_restart_call_prepare(task);
6045 break;
6046 }
6047 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006048 if (data->arg.new_lock_owner != 0) {
6049 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6050 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6051 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6052 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6053 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006054 break;
6055 case -NFS4ERR_BAD_STATEID:
6056 case -NFS4ERR_OLD_STATEID:
6057 case -NFS4ERR_STALE_STATEID:
6058 case -NFS4ERR_EXPIRED:
6059 if (data->arg.new_lock_owner != 0) {
6060 if (!nfs4_stateid_match(&data->arg.open_stateid,
6061 &lsp->ls_state->open_stateid))
6062 rpc_restart_call_prepare(task);
6063 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6064 &lsp->ls_stateid))
6065 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006067 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006068}
6069
6070static void nfs4_lock_release(void *calldata)
6071{
6072 struct nfs4_lockdata *data = calldata;
6073
Harvey Harrison3110ff82008-05-02 13:42:44 -07006074 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006075 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006076 if (data->cancelled != 0) {
6077 struct rpc_task *task;
6078 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6079 data->arg.lock_seqid);
6080 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006081 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006082 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006083 } else
6084 nfs_free_seqid(data->arg.lock_seqid);
6085 nfs4_put_lock_state(data->lsp);
6086 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006087 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006088 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006089 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090}
6091
6092static const struct rpc_call_ops nfs4_lock_ops = {
6093 .rpc_call_prepare = nfs4_lock_prepare,
6094 .rpc_call_done = nfs4_lock_done,
6095 .rpc_release = nfs4_lock_release,
6096};
6097
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006098static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6099{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006100 switch (error) {
6101 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006102 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006103 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006104 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006105 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006106 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006107 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006108 break;
6109 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006110 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006111 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006112 };
6113}
6114
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006115static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006116{
6117 struct nfs4_lockdata *data;
6118 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006119 struct rpc_message msg = {
6120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6121 .rpc_cred = state->owner->so_cred,
6122 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006123 struct rpc_task_setup task_setup_data = {
6124 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006125 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006126 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006127 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006128 .flags = RPC_TASK_ASYNC,
6129 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006130 int ret;
6131
Harvey Harrison3110ff82008-05-02 13:42:44 -07006132 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006133 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006134 fl->fl_u.nfs4_fl.owner,
6135 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006136 if (data == NULL)
6137 return -ENOMEM;
6138 if (IS_SETLKW(cmd))
6139 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006140 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006141 msg.rpc_argp = &data->arg;
6142 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006143 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006144 if (recovery_type > NFS_LOCK_NEW) {
6145 if (recovery_type == NFS_LOCK_RECLAIM)
6146 data->arg.reclaim = NFS_LOCK_RECLAIM;
6147 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006148 } else
6149 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006150 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006151 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006152 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153 ret = nfs4_wait_for_completion_rpc_task(task);
6154 if (ret == 0) {
6155 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006156 if (ret)
6157 nfs4_handle_setlk_error(data->server, data->lsp,
6158 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006159 } else
6160 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006161 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006162 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006163 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165}
6166
6167static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6168{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006169 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006170 struct nfs4_exception exception = {
6171 .inode = state->inode,
6172 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006173 int err;
6174
6175 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006176 /* Cache the lock if possible... */
6177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6178 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006179 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006180 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006181 break;
6182 nfs4_handle_exception(server, err, &exception);
6183 } while (exception.retry);
6184 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185}
6186
6187static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6188{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006189 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006190 struct nfs4_exception exception = {
6191 .inode = state->inode,
6192 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006193 int err;
6194
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006195 err = nfs4_set_lock_state(state, request);
6196 if (err != 0)
6197 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006198 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006199 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6200 return 0;
6201 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006202 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006203 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6204 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006205 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006206 switch (err) {
6207 default:
6208 goto out;
6209 case -NFS4ERR_GRACE:
6210 case -NFS4ERR_DELAY:
6211 nfs4_handle_exception(server, err, &exception);
6212 err = 0;
6213 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006214 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006215out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006219#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006220static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6221{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006222 struct nfs4_lock_state *lsp;
6223 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006224
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006225 status = nfs4_set_lock_state(state, request);
6226 if (status != 0)
6227 return status;
6228 lsp = request->fl_u.nfs4_fl.owner;
6229 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6230 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6231 return 0;
6232 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006233 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006234}
6235#endif
6236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6238{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006239 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006240 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006241 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006242 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
Trond Myklebust01c3b862006-06-29 16:38:39 -04006244 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006245 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006246 if (status < 0)
6247 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006248 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006249 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006250 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006251 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006252 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006253 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006254 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006255 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006256 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006257 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006258 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006259 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006260 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006261 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006262out:
6263 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 return status;
6265}
6266
6267static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6268{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006269 struct nfs4_exception exception = {
6270 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006271 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006272 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 int err;
6274
6275 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006276 err = _nfs4_proc_setlk(state, cmd, request);
6277 if (err == -NFS4ERR_DENIED)
6278 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006280 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 } while (exception.retry);
6282 return err;
6283}
6284
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006285#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6286#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6287
6288static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006289nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6290 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006291{
6292 int status = -ERESTARTSYS;
6293 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6294
6295 while(!signalled()) {
6296 status = nfs4_proc_setlk(state, cmd, request);
6297 if ((status != -EAGAIN) || IS_SETLK(cmd))
6298 break;
6299 freezable_schedule_timeout_interruptible(timeout);
6300 timeout *= 2;
6301 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6302 status = -ERESTARTSYS;
6303 }
6304 return status;
6305}
6306
Jeff Laytona1d617d82016-09-17 18:17:39 -04006307#ifdef CONFIG_NFS_V4_1
6308struct nfs4_lock_waiter {
6309 struct task_struct *task;
6310 struct inode *inode;
6311 struct nfs_lowner *owner;
6312 bool notified;
6313};
6314
6315static int
6316nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6317{
6318 int ret;
6319 struct cb_notify_lock_args *cbnl = key;
6320 struct nfs4_lock_waiter *waiter = wait->private;
6321 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6322 *wowner = waiter->owner;
6323
6324 /* Only wake if the callback was for the same owner */
6325 if (lowner->clientid != wowner->clientid ||
6326 lowner->id != wowner->id ||
6327 lowner->s_dev != wowner->s_dev)
6328 return 0;
6329
6330 /* Make sure it's for the right inode */
6331 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6332 return 0;
6333
6334 waiter->notified = true;
6335
6336 /* override "private" so we can use default_wake_function */
6337 wait->private = waiter->task;
6338 ret = autoremove_wake_function(wait, mode, flags, key);
6339 wait->private = waiter;
6340 return ret;
6341}
6342
6343static int
6344nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6345{
6346 int status = -ERESTARTSYS;
6347 unsigned long flags;
6348 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6349 struct nfs_server *server = NFS_SERVER(state->inode);
6350 struct nfs_client *clp = server->nfs_client;
6351 wait_queue_head_t *q = &clp->cl_lock_waitq;
6352 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6353 .id = lsp->ls_seqid.owner_id,
6354 .s_dev = server->s_dev };
6355 struct nfs4_lock_waiter waiter = { .task = current,
6356 .inode = state->inode,
6357 .owner = &owner,
6358 .notified = false };
6359 wait_queue_t wait;
6360
6361 /* Don't bother with waitqueue if we don't expect a callback */
6362 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6363 return nfs4_retry_setlk_simple(state, cmd, request);
6364
6365 init_wait(&wait);
6366 wait.private = &waiter;
6367 wait.func = nfs4_wake_lock_waiter;
6368 add_wait_queue(q, &wait);
6369
6370 while(!signalled()) {
6371 status = nfs4_proc_setlk(state, cmd, request);
6372 if ((status != -EAGAIN) || IS_SETLK(cmd))
6373 break;
6374
6375 status = -ERESTARTSYS;
6376 spin_lock_irqsave(&q->lock, flags);
6377 if (waiter.notified) {
6378 spin_unlock_irqrestore(&q->lock, flags);
6379 continue;
6380 }
6381 set_current_state(TASK_INTERRUPTIBLE);
6382 spin_unlock_irqrestore(&q->lock, flags);
6383
6384 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6385 }
6386
6387 finish_wait(q, &wait);
6388 return status;
6389}
6390#else /* !CONFIG_NFS_V4_1 */
6391static inline int
6392nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6393{
6394 return nfs4_retry_setlk_simple(state, cmd, request);
6395}
6396#endif
6397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398static int
6399nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6400{
6401 struct nfs_open_context *ctx;
6402 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 int status;
6404
6405 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006406 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 state = ctx->state;
6408
6409 if (request->fl_start < 0 || request->fl_end < 0)
6410 return -EINVAL;
6411
Trond Myklebustd9531262009-07-21 19:22:38 -04006412 if (IS_GETLK(cmd)) {
6413 if (state != NULL)
6414 return nfs4_proc_getlk(state, F_GETLK, request);
6415 return 0;
6416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
6418 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6419 return -EINVAL;
6420
Trond Myklebustd9531262009-07-21 19:22:38 -04006421 if (request->fl_type == F_UNLCK) {
6422 if (state != NULL)
6423 return nfs4_proc_unlck(state, cmd, request);
6424 return 0;
6425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Trond Myklebustd9531262009-07-21 19:22:38 -04006427 if (state == NULL)
6428 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006429
6430 if ((request->fl_flags & FL_POSIX) &&
6431 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6432 return -ENOLCK;
6433
Trond Myklebust55725512012-04-18 12:48:35 -04006434 /*
6435 * Don't rely on the VFS having checked the file open mode,
6436 * since it won't do this for flock() locks.
6437 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006438 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006439 case F_RDLCK:
6440 if (!(filp->f_mode & FMODE_READ))
6441 return -EBADF;
6442 break;
6443 case F_WRLCK:
6444 if (!(filp->f_mode & FMODE_WRITE))
6445 return -EBADF;
6446 }
6447
Jeff Layton1ea67db2016-09-17 18:17:37 -04006448 status = nfs4_set_lock_state(state, request);
6449 if (status != 0)
6450 return status;
6451
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006452 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453}
6454
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006455int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006456{
6457 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006458 int err;
6459
6460 err = nfs4_set_lock_state(state, fl);
6461 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006462 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006463 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006464 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006465}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006466
Trond Myklebustcf470c32012-03-07 13:49:12 -05006467struct nfs_release_lockowner_data {
6468 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006469 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006470 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006471 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006472 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006473};
6474
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006475static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6476{
6477 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006478 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006479 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6480 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006481 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006482 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006483}
6484
6485static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6486{
6487 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006488 struct nfs_server *server = data->server;
6489
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006490 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006491
6492 switch (task->tk_status) {
6493 case 0:
6494 renew_lease(server, data->timestamp);
6495 break;
6496 case -NFS4ERR_STALE_CLIENTID:
6497 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006498 nfs4_schedule_lease_recovery(server->nfs_client);
6499 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006500 case -NFS4ERR_LEASE_MOVED:
6501 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006502 if (nfs4_async_handle_error(task, server,
6503 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006504 rpc_restart_call_prepare(task);
6505 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006506}
6507
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006508static void nfs4_release_lockowner_release(void *calldata)
6509{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006510 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006511 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006512 kfree(calldata);
6513}
6514
Trond Myklebust17280172012-03-11 13:11:00 -04006515static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006516 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6517 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006518 .rpc_release = nfs4_release_lockowner_release,
6519};
6520
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006521static void
6522nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006523{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006524 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006525 struct rpc_message msg = {
6526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6527 };
6528
6529 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006530 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006531
Trond Myklebustcf470c32012-03-07 13:49:12 -05006532 data = kmalloc(sizeof(*data), GFP_NOFS);
6533 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006534 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006535 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006536 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006537 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6538 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6539 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006540
Trond Myklebustcf470c32012-03-07 13:49:12 -05006541 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006542 msg.rpc_resp = &data->res;
6543 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006544 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006545}
6546
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006547#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6548
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006549static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006550 struct dentry *unused, struct inode *inode,
6551 const char *key, const void *buf,
6552 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006553{
Al Viro59301222016-05-27 10:19:30 -04006554 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006555}
6556
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006557static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006558 struct dentry *unused, struct inode *inode,
6559 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006560{
Al Virob2968212016-04-10 20:48:24 -04006561 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006562}
6563
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006564static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006565{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006566 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006567}
6568
David Quigleyc9bccef2013-05-22 12:50:45 -04006569#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006570
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006571static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006572 struct dentry *unused, struct inode *inode,
6573 const char *key, const void *buf,
6574 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006575{
6576 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006577 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006578
6579 return -EOPNOTSUPP;
6580}
6581
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006582static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006583 struct dentry *unused, struct inode *inode,
6584 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006585{
6586 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006587 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006588 return -EOPNOTSUPP;
6589}
6590
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006591static ssize_t
6592nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006593{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006594 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006595
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006596 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6597 len = security_inode_listsecurity(inode, list, list_len);
6598 if (list_len && len > list_len)
6599 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006600 }
6601 return len;
6602}
6603
6604static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6605 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006606 .get = nfs4_xattr_get_nfs4_label,
6607 .set = nfs4_xattr_set_nfs4_label,
6608};
David Quigleyc9bccef2013-05-22 12:50:45 -04006609
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006610#else
6611
6612static ssize_t
6613nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6614{
6615 return 0;
6616}
6617
6618#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006619
Andy Adamson533eb462011-06-13 18:25:56 -04006620/*
6621 * nfs_fhget will use either the mounted_on_fileid or the fileid
6622 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006623static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6624{
Andy Adamson533eb462011-06-13 18:25:56 -04006625 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6626 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6627 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006628 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006629 return;
6630
6631 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006632 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006633 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6634 fattr->nlink = 2;
6635}
6636
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006637static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6638 const struct qstr *name,
6639 struct nfs4_fs_locations *fs_locations,
6640 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006641{
6642 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006643 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006644 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006645 };
6646 struct nfs4_fs_locations_arg args = {
6647 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006648 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006649 .page = page,
6650 .bitmask = bitmask,
6651 };
Benny Halevy22958462009-04-01 09:22:02 -04006652 struct nfs4_fs_locations_res res = {
6653 .fs_locations = fs_locations,
6654 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006655 struct rpc_message msg = {
6656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6657 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006658 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006659 };
6660 int status;
6661
Harvey Harrison3110ff82008-05-02 13:42:44 -07006662 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006663
6664 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6665 * is not supported */
6666 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6667 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6668 else
6669 bitmask[0] |= FATTR4_WORD0_FILEID;
6670
Trond Myklebustc228fd32007-01-13 02:28:11 -05006671 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006672 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006673 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006674 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006675 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006676 return status;
6677}
6678
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006679int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6680 const struct qstr *name,
6681 struct nfs4_fs_locations *fs_locations,
6682 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006683{
6684 struct nfs4_exception exception = { };
6685 int err;
6686 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006687 err = _nfs4_proc_fs_locations(client, dir, name,
6688 fs_locations, page);
6689 trace_nfs4_get_fs_locations(dir, name, err);
6690 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006691 &exception);
6692 } while (exception.retry);
6693 return err;
6694}
6695
Chuck Leverb03d7352013-10-17 14:12:50 -04006696/*
6697 * This operation also signals the server that this client is
6698 * performing migration recovery. The server can stop returning
6699 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6700 * appended to this compound to identify the client ID which is
6701 * performing recovery.
6702 */
6703static int _nfs40_proc_get_locations(struct inode *inode,
6704 struct nfs4_fs_locations *locations,
6705 struct page *page, struct rpc_cred *cred)
6706{
6707 struct nfs_server *server = NFS_SERVER(inode);
6708 struct rpc_clnt *clnt = server->client;
6709 u32 bitmask[2] = {
6710 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6711 };
6712 struct nfs4_fs_locations_arg args = {
6713 .clientid = server->nfs_client->cl_clientid,
6714 .fh = NFS_FH(inode),
6715 .page = page,
6716 .bitmask = bitmask,
6717 .migration = 1, /* skip LOOKUP */
6718 .renew = 1, /* append RENEW */
6719 };
6720 struct nfs4_fs_locations_res res = {
6721 .fs_locations = locations,
6722 .migration = 1,
6723 .renew = 1,
6724 };
6725 struct rpc_message msg = {
6726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6727 .rpc_argp = &args,
6728 .rpc_resp = &res,
6729 .rpc_cred = cred,
6730 };
6731 unsigned long now = jiffies;
6732 int status;
6733
6734 nfs_fattr_init(&locations->fattr);
6735 locations->server = server;
6736 locations->nlocations = 0;
6737
6738 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6739 nfs4_set_sequence_privileged(&args.seq_args);
6740 status = nfs4_call_sync_sequence(clnt, server, &msg,
6741 &args.seq_args, &res.seq_res);
6742 if (status)
6743 return status;
6744
6745 renew_lease(server, now);
6746 return 0;
6747}
6748
6749#ifdef CONFIG_NFS_V4_1
6750
6751/*
6752 * This operation also signals the server that this client is
6753 * performing migration recovery. The server can stop asserting
6754 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6755 * performing this operation is identified in the SEQUENCE
6756 * operation in this compound.
6757 *
6758 * When the client supports GETATTR(fs_locations_info), it can
6759 * be plumbed in here.
6760 */
6761static int _nfs41_proc_get_locations(struct inode *inode,
6762 struct nfs4_fs_locations *locations,
6763 struct page *page, struct rpc_cred *cred)
6764{
6765 struct nfs_server *server = NFS_SERVER(inode);
6766 struct rpc_clnt *clnt = server->client;
6767 u32 bitmask[2] = {
6768 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6769 };
6770 struct nfs4_fs_locations_arg args = {
6771 .fh = NFS_FH(inode),
6772 .page = page,
6773 .bitmask = bitmask,
6774 .migration = 1, /* skip LOOKUP */
6775 };
6776 struct nfs4_fs_locations_res res = {
6777 .fs_locations = locations,
6778 .migration = 1,
6779 };
6780 struct rpc_message msg = {
6781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6782 .rpc_argp = &args,
6783 .rpc_resp = &res,
6784 .rpc_cred = cred,
6785 };
6786 int status;
6787
6788 nfs_fattr_init(&locations->fattr);
6789 locations->server = server;
6790 locations->nlocations = 0;
6791
6792 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6793 nfs4_set_sequence_privileged(&args.seq_args);
6794 status = nfs4_call_sync_sequence(clnt, server, &msg,
6795 &args.seq_args, &res.seq_res);
6796 if (status == NFS4_OK &&
6797 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6798 status = -NFS4ERR_LEASE_MOVED;
6799 return status;
6800}
6801
6802#endif /* CONFIG_NFS_V4_1 */
6803
6804/**
6805 * nfs4_proc_get_locations - discover locations for a migrated FSID
6806 * @inode: inode on FSID that is migrating
6807 * @locations: result of query
6808 * @page: buffer
6809 * @cred: credential to use for this operation
6810 *
6811 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6812 * operation failed, or a negative errno if a local error occurred.
6813 *
6814 * On success, "locations" is filled in, but if the server has
6815 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6816 * asserted.
6817 *
6818 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6819 * from this client that require migration recovery.
6820 */
6821int nfs4_proc_get_locations(struct inode *inode,
6822 struct nfs4_fs_locations *locations,
6823 struct page *page, struct rpc_cred *cred)
6824{
6825 struct nfs_server *server = NFS_SERVER(inode);
6826 struct nfs_client *clp = server->nfs_client;
6827 const struct nfs4_mig_recovery_ops *ops =
6828 clp->cl_mvops->mig_recovery_ops;
6829 struct nfs4_exception exception = { };
6830 int status;
6831
6832 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6833 (unsigned long long)server->fsid.major,
6834 (unsigned long long)server->fsid.minor,
6835 clp->cl_hostname);
6836 nfs_display_fhandle(NFS_FH(inode), __func__);
6837
6838 do {
6839 status = ops->get_locations(inode, locations, page, cred);
6840 if (status != -NFS4ERR_DELAY)
6841 break;
6842 nfs4_handle_exception(server, status, &exception);
6843 } while (exception.retry);
6844 return status;
6845}
6846
Chuck Lever44c99932013-10-17 14:13:30 -04006847/*
6848 * This operation also signals the server that this client is
6849 * performing "lease moved" recovery. The server can stop
6850 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6851 * is appended to this compound to identify the client ID which is
6852 * performing recovery.
6853 */
6854static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6855{
6856 struct nfs_server *server = NFS_SERVER(inode);
6857 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6858 struct rpc_clnt *clnt = server->client;
6859 struct nfs4_fsid_present_arg args = {
6860 .fh = NFS_FH(inode),
6861 .clientid = clp->cl_clientid,
6862 .renew = 1, /* append RENEW */
6863 };
6864 struct nfs4_fsid_present_res res = {
6865 .renew = 1,
6866 };
6867 struct rpc_message msg = {
6868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6869 .rpc_argp = &args,
6870 .rpc_resp = &res,
6871 .rpc_cred = cred,
6872 };
6873 unsigned long now = jiffies;
6874 int status;
6875
6876 res.fh = nfs_alloc_fhandle();
6877 if (res.fh == NULL)
6878 return -ENOMEM;
6879
6880 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6881 nfs4_set_sequence_privileged(&args.seq_args);
6882 status = nfs4_call_sync_sequence(clnt, server, &msg,
6883 &args.seq_args, &res.seq_res);
6884 nfs_free_fhandle(res.fh);
6885 if (status)
6886 return status;
6887
6888 do_renew_lease(clp, now);
6889 return 0;
6890}
6891
6892#ifdef CONFIG_NFS_V4_1
6893
6894/*
6895 * This operation also signals the server that this client is
6896 * performing "lease moved" recovery. The server can stop asserting
6897 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6898 * this operation is identified in the SEQUENCE operation in this
6899 * compound.
6900 */
6901static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6902{
6903 struct nfs_server *server = NFS_SERVER(inode);
6904 struct rpc_clnt *clnt = server->client;
6905 struct nfs4_fsid_present_arg args = {
6906 .fh = NFS_FH(inode),
6907 };
6908 struct nfs4_fsid_present_res res = {
6909 };
6910 struct rpc_message msg = {
6911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6912 .rpc_argp = &args,
6913 .rpc_resp = &res,
6914 .rpc_cred = cred,
6915 };
6916 int status;
6917
6918 res.fh = nfs_alloc_fhandle();
6919 if (res.fh == NULL)
6920 return -ENOMEM;
6921
6922 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6923 nfs4_set_sequence_privileged(&args.seq_args);
6924 status = nfs4_call_sync_sequence(clnt, server, &msg,
6925 &args.seq_args, &res.seq_res);
6926 nfs_free_fhandle(res.fh);
6927 if (status == NFS4_OK &&
6928 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6929 status = -NFS4ERR_LEASE_MOVED;
6930 return status;
6931}
6932
6933#endif /* CONFIG_NFS_V4_1 */
6934
6935/**
6936 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6937 * @inode: inode on FSID to check
6938 * @cred: credential to use for this operation
6939 *
6940 * Server indicates whether the FSID is present, moved, or not
6941 * recognized. This operation is necessary to clear a LEASE_MOVED
6942 * condition for this client ID.
6943 *
6944 * Returns NFS4_OK if the FSID is present on this server,
6945 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6946 * NFS4ERR code if some error occurred on the server, or a
6947 * negative errno if a local failure occurred.
6948 */
6949int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6950{
6951 struct nfs_server *server = NFS_SERVER(inode);
6952 struct nfs_client *clp = server->nfs_client;
6953 const struct nfs4_mig_recovery_ops *ops =
6954 clp->cl_mvops->mig_recovery_ops;
6955 struct nfs4_exception exception = { };
6956 int status;
6957
6958 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6959 (unsigned long long)server->fsid.major,
6960 (unsigned long long)server->fsid.minor,
6961 clp->cl_hostname);
6962 nfs_display_fhandle(NFS_FH(inode), __func__);
6963
6964 do {
6965 status = ops->fsid_present(inode, cred);
6966 if (status != -NFS4ERR_DELAY)
6967 break;
6968 nfs4_handle_exception(server, status, &exception);
6969 } while (exception.retry);
6970 return status;
6971}
6972
Andy Adamson5ec16a82013-08-08 10:57:55 -04006973/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006974 * If 'use_integrity' is true and the state managment nfs_client
6975 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6976 * and the machine credential as per RFC3530bis and RFC5661 Security
6977 * Considerations sections. Otherwise, just use the user cred with the
6978 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006979 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006980static 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 +00006981{
6982 int status;
6983 struct nfs4_secinfo_arg args = {
6984 .dir_fh = NFS_FH(dir),
6985 .name = name,
6986 };
6987 struct nfs4_secinfo_res res = {
6988 .flavors = flavors,
6989 };
6990 struct rpc_message msg = {
6991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6992 .rpc_argp = &args,
6993 .rpc_resp = &res,
6994 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006995 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006996 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006997
6998 if (use_integrity) {
6999 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007000 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7001 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007002 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007003
7004 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007005
7006 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7007 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7008
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007009 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7010 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007011 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007012
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007013 if (cred)
7014 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007015
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007016 return status;
7017}
7018
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007019int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7020 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007021{
7022 struct nfs4_exception exception = { };
7023 int err;
7024 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007025 err = -NFS4ERR_WRONGSEC;
7026
7027 /* try to use integrity protection with machine cred */
7028 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7029 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7030
7031 /*
7032 * if unable to use integrity protection, or SECINFO with
7033 * integrity protection returns NFS4ERR_WRONGSEC (which is
7034 * disallowed by spec, but exists in deployed servers) use
7035 * the current filesystem's rpc_client and the user cred.
7036 */
7037 if (err == -NFS4ERR_WRONGSEC)
7038 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7039
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007040 trace_nfs4_secinfo(dir, name, err);
7041 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007042 &exception);
7043 } while (exception.retry);
7044 return err;
7045}
7046
Andy Adamson557134a2009-04-01 09:21:53 -04007047#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007048/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007049 * Check the exchange flags returned by the server for invalid flags, having
7050 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7051 * DS flags set.
7052 */
7053static int nfs4_check_cl_exchange_flags(u32 flags)
7054{
7055 if (flags & ~EXCHGID4_FLAG_MASK_R)
7056 goto out_inval;
7057 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7058 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7059 goto out_inval;
7060 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7061 goto out_inval;
7062 return NFS_OK;
7063out_inval:
7064 return -NFS4ERR_INVAL;
7065}
7066
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007067static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007068nfs41_same_server_scope(struct nfs41_server_scope *a,
7069 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007070{
7071 if (a->server_scope_sz == b->server_scope_sz &&
7072 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7073 return true;
7074
7075 return false;
7076}
7077
Andy Adamson02a95de2016-02-05 16:08:37 -05007078static void
7079nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7080{
7081}
7082
7083static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7084 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7085};
7086
Andy Adamson357f54d2010-12-14 10:11:57 -05007087/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007088 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007089 *
7090 * The 4.1 client currently uses the same TCP connection for the
7091 * fore and backchannel.
7092 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007093static
7094int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7095 struct rpc_xprt *xprt,
7096 struct nfs_client *clp,
7097 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007098{
7099 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007100 struct nfs41_bind_conn_to_session_args args = {
7101 .client = clp,
7102 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7103 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007104 struct nfs41_bind_conn_to_session_res res;
7105 struct rpc_message msg = {
7106 .rpc_proc =
7107 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007108 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007109 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007110 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007111 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007112 struct rpc_task_setup task_setup_data = {
7113 .rpc_client = clnt,
7114 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007115 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007116 .rpc_message = &msg,
7117 .flags = RPC_TASK_TIMEOUT,
7118 };
7119 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007120
7121 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007122
Trond Myklebust71a097c2015-02-18 09:27:18 -08007123 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7124 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7125 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007126
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007127 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7128 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7129 args.dir = NFS4_CDFC4_FORE;
7130
7131 task = rpc_run_task(&task_setup_data);
7132 if (!IS_ERR(task)) {
7133 status = task->tk_status;
7134 rpc_put_task(task);
7135 } else
7136 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007137 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007138 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007139 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007140 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7141 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7142 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007143 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007144 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007145 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007146 dprintk("NFS: %s: Unexpected direction from server\n",
7147 __func__);
7148 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007149 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007150 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007151 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007152 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7153 __func__);
7154 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007155 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007156 }
7157 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007158out:
7159 dprintk("<-- %s status= %d\n", __func__, status);
7160 return status;
7161}
7162
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007163struct rpc_bind_conn_calldata {
7164 struct nfs_client *clp;
7165 struct rpc_cred *cred;
7166};
7167
7168static int
7169nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7170 struct rpc_xprt *xprt,
7171 void *calldata)
7172{
7173 struct rpc_bind_conn_calldata *p = calldata;
7174
7175 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7176}
7177
7178int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7179{
7180 struct rpc_bind_conn_calldata data = {
7181 .clp = clp,
7182 .cred = cred,
7183 };
7184 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7185 nfs4_proc_bind_conn_to_session_callback, &data);
7186}
7187
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007188/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007189 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7190 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007191 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007192static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7193 .how = SP4_MACH_CRED,
7194 .enforce.u.words = {
7195 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7196 1 << (OP_EXCHANGE_ID - 32) |
7197 1 << (OP_CREATE_SESSION - 32) |
7198 1 << (OP_DESTROY_SESSION - 32) |
7199 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007200 },
7201 .allow.u.words = {
7202 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007203 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007204 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007205 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007206 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007207 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007208 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007209 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007210 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007211 1 << (OP_FREE_STATEID - 32) |
7212 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007213 }
7214};
7215
7216/*
7217 * Select the state protection mode for client `clp' given the server results
7218 * from exchange_id in `sp'.
7219 *
7220 * Returns 0 on success, negative errno otherwise.
7221 */
7222static int nfs4_sp4_select_mode(struct nfs_client *clp,
7223 struct nfs41_state_protection *sp)
7224{
7225 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7226 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7227 1 << (OP_EXCHANGE_ID - 32) |
7228 1 << (OP_CREATE_SESSION - 32) |
7229 1 << (OP_DESTROY_SESSION - 32) |
7230 1 << (OP_DESTROY_CLIENTID - 32)
7231 };
7232 unsigned int i;
7233
7234 if (sp->how == SP4_MACH_CRED) {
7235 /* Print state protect result */
7236 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7237 for (i = 0; i <= LAST_NFS4_OP; i++) {
7238 if (test_bit(i, sp->enforce.u.longs))
7239 dfprintk(MOUNT, " enforce op %d\n", i);
7240 if (test_bit(i, sp->allow.u.longs))
7241 dfprintk(MOUNT, " allow op %d\n", i);
7242 }
7243
7244 /* make sure nothing is on enforce list that isn't supported */
7245 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7246 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7247 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7248 return -EINVAL;
7249 }
7250 }
7251
7252 /*
7253 * Minimal mode - state operations are allowed to use machine
7254 * credential. Note this already happens by default, so the
7255 * client doesn't have to do anything more than the negotiation.
7256 *
7257 * NOTE: we don't care if EXCHANGE_ID is in the list -
7258 * we're already using the machine cred for exchange_id
7259 * and will never use a different cred.
7260 */
7261 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7262 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7263 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7264 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7265 dfprintk(MOUNT, "sp4_mach_cred:\n");
7266 dfprintk(MOUNT, " minimal mode enabled\n");
7267 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7268 } else {
7269 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7270 return -EINVAL;
7271 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007272
7273 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007274 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7275 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007276 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7277 dfprintk(MOUNT, " cleanup mode enabled\n");
7278 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7279 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007280
Andrew Elble99ade3c2015-12-02 09:39:51 -05007281 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7282 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7283 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7284 &clp->cl_sp4_flags);
7285 }
7286
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007287 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7288 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7289 dfprintk(MOUNT, " secinfo mode enabled\n");
7290 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7291 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007292
7293 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7294 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7295 dfprintk(MOUNT, " stateid mode enabled\n");
7296 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7297 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007298
7299 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7300 dfprintk(MOUNT, " write mode enabled\n");
7301 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7302 }
7303
7304 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7305 dfprintk(MOUNT, " commit mode enabled\n");
7306 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7307 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007308 }
7309
7310 return 0;
7311}
7312
Andy Adamson8d89bd72016-09-09 09:22:18 -04007313struct nfs41_exchange_id_data {
7314 struct nfs41_exchange_id_res res;
7315 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007316 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007317 int rpc_status;
7318};
7319
7320static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7321{
7322 struct nfs41_exchange_id_data *cdata =
7323 (struct nfs41_exchange_id_data *)data;
7324 struct nfs_client *clp = cdata->args.client;
7325 int status = task->tk_status;
7326
7327 trace_nfs4_exchange_id(clp, status);
7328
7329 if (status == 0)
7330 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007331
7332 if (cdata->xprt && status == 0) {
7333 status = nfs4_detect_session_trunking(clp, &cdata->res,
7334 cdata->xprt);
7335 goto out;
7336 }
7337
Andy Adamson8d89bd72016-09-09 09:22:18 -04007338 if (status == 0)
7339 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7340
7341 if (status == 0) {
7342 clp->cl_clientid = cdata->res.clientid;
7343 clp->cl_exchange_flags = cdata->res.flags;
7344 /* Client ID is not confirmed */
7345 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7346 clear_bit(NFS4_SESSION_ESTABLISHED,
7347 &clp->cl_session->session_state);
7348 clp->cl_seqid = cdata->res.seqid;
7349 }
7350
7351 kfree(clp->cl_serverowner);
7352 clp->cl_serverowner = cdata->res.server_owner;
7353 cdata->res.server_owner = NULL;
7354
7355 /* use the most recent implementation id */
7356 kfree(clp->cl_implid);
7357 clp->cl_implid = cdata->res.impl_id;
7358 cdata->res.impl_id = NULL;
7359
7360 if (clp->cl_serverscope != NULL &&
7361 !nfs41_same_server_scope(clp->cl_serverscope,
7362 cdata->res.server_scope)) {
7363 dprintk("%s: server_scope mismatch detected\n",
7364 __func__);
7365 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7366 kfree(clp->cl_serverscope);
7367 clp->cl_serverscope = NULL;
7368 }
7369
7370 if (clp->cl_serverscope == NULL) {
7371 clp->cl_serverscope = cdata->res.server_scope;
7372 cdata->res.server_scope = NULL;
7373 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007374 /* Save the EXCHANGE_ID verifier session trunk tests */
7375 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7376 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007377 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007378out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007379 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007380 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007381}
7382
7383static void nfs4_exchange_id_release(void *data)
7384{
7385 struct nfs41_exchange_id_data *cdata =
7386 (struct nfs41_exchange_id_data *)data;
7387
7388 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007389 if (cdata->xprt) {
7390 xprt_put(cdata->xprt);
7391 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7392 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 kfree(cdata->res.impl_id);
7394 kfree(cdata->res.server_scope);
7395 kfree(cdata->res.server_owner);
7396 kfree(cdata);
7397}
7398
7399static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7400 .rpc_call_done = nfs4_exchange_id_done,
7401 .rpc_release = nfs4_exchange_id_release,
7402};
7403
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007404/*
7405 * _nfs4_proc_exchange_id()
7406 *
7407 * Wrapper for EXCHANGE_ID operation.
7408 */
7409static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007410 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007411{
7412 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007413 struct rpc_message msg = {
7414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007415 .rpc_cred = cred,
7416 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417 struct rpc_task_setup task_setup_data = {
7418 .rpc_client = clp->cl_rpcclient,
7419 .callback_ops = &nfs4_exchange_id_call_ops,
7420 .rpc_message = &msg,
7421 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7422 };
7423 struct nfs41_exchange_id_data *calldata;
7424 struct rpc_task *task;
7425 int status = -EIO;
7426
7427 if (!atomic_inc_not_zero(&clp->cl_count))
7428 goto out;
7429
7430 status = -ENOMEM;
7431 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7432 if (!calldata)
7433 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007434
Andy Adamsonad0849a2016-09-09 09:22:28 -04007435 if (!xprt)
7436 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007437
7438 status = nfs4_init_uniform_client_string(clp);
7439 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007440 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007441
7442 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007443 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007444 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007445
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7447 GFP_NOFS);
7448 status = -ENOMEM;
7449 if (unlikely(calldata->res.server_owner == NULL))
7450 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007451
Andy Adamson8d89bd72016-09-09 09:22:18 -04007452 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007453 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007454 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007455 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007456
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7458 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007459 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007460
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007461 switch (sp4_how) {
7462 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007464 break;
7465
7466 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007468 break;
7469
7470 default:
7471 /* unsupported! */
7472 WARN_ON_ONCE(1);
7473 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007474 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007475 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007476 if (xprt) {
7477 calldata->xprt = xprt;
7478 task_setup_data.rpc_xprt = xprt;
7479 task_setup_data.flags =
7480 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7481 calldata->args.verifier = &clp->cl_confirm;
7482 } else {
7483 calldata->args.verifier = &verifier;
7484 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 calldata->args.client = clp;
7486#ifdef CONFIG_NFS_V4_1_MIGRATION
7487 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7488 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7489 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7490#else
7491 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7492 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7493#endif
7494 msg.rpc_argp = &calldata->args;
7495 msg.rpc_resp = &calldata->res;
7496 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007497
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 task = rpc_run_task(&task_setup_data);
7499 if (IS_ERR(task)) {
7500 status = PTR_ERR(task);
7501 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007502 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007503
Andy Adamsonad0849a2016-09-09 09:22:28 -04007504 if (!xprt) {
7505 status = rpc_wait_for_completion_task(task);
7506 if (!status)
7507 status = calldata->rpc_status;
7508 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007510
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007512out:
Chuck Lever177313f2012-05-21 22:44:58 -04007513 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007514 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007515 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007516 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007517 clp->cl_implid->date.seconds,
7518 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007519 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007520 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007521
7522out_impl_id:
7523 kfree(calldata->res.impl_id);
7524out_server_scope:
7525 kfree(calldata->res.server_scope);
7526out_server_owner:
7527 kfree(calldata->res.server_owner);
7528out_calldata:
7529 kfree(calldata);
7530 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007531}
7532
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007533/*
7534 * nfs4_proc_exchange_id()
7535 *
7536 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7537 *
7538 * Since the clientid has expired, all compounds using sessions
7539 * associated with the stale clientid will be returning
7540 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7541 * be in some phase of session reset.
7542 *
7543 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7544 */
7545int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7546{
7547 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7548 int status;
7549
7550 /* try SP4_MACH_CRED if krb5i/p */
7551 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7552 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007553 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007554 if (!status)
7555 return 0;
7556 }
7557
7558 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007559 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007560}
7561
Andy Adamson04fa2c62016-09-09 09:22:29 -04007562/**
7563 * nfs4_test_session_trunk
7564 *
7565 * This is an add_xprt_test() test function called from
7566 * rpc_clnt_setup_test_and_add_xprt.
7567 *
7568 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7569 * and is dereferrenced in nfs4_exchange_id_release
7570 *
7571 * Upon success, add the new transport to the rpc_clnt
7572 *
7573 * @clnt: struct rpc_clnt to get new transport
7574 * @xprt: the rpc_xprt to test
7575 * @data: call data for _nfs4_proc_exchange_id.
7576 */
7577int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7578 void *data)
7579{
7580 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7581 u32 sp4_how;
7582
7583 dprintk("--> %s try %s\n", __func__,
7584 xprt->address_strings[RPC_DISPLAY_ADDR]);
7585
7586 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7587
7588 /* Test connection for session trunking. Async exchange_id call */
7589 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7590}
7591EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7592
Trond Myklebust66245532012-05-25 17:18:09 -04007593static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7594 struct rpc_cred *cred)
7595{
7596 struct rpc_message msg = {
7597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7598 .rpc_argp = clp,
7599 .rpc_cred = cred,
7600 };
7601 int status;
7602
7603 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007604 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007605 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007606 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007607 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7608 return status;
7609}
7610
7611static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7612 struct rpc_cred *cred)
7613{
7614 unsigned int loop;
7615 int ret;
7616
7617 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7618 ret = _nfs4_proc_destroy_clientid(clp, cred);
7619 switch (ret) {
7620 case -NFS4ERR_DELAY:
7621 case -NFS4ERR_CLIENTID_BUSY:
7622 ssleep(1);
7623 break;
7624 default:
7625 return ret;
7626 }
7627 }
7628 return 0;
7629}
7630
7631int nfs4_destroy_clientid(struct nfs_client *clp)
7632{
7633 struct rpc_cred *cred;
7634 int ret = 0;
7635
7636 if (clp->cl_mvops->minor_version < 1)
7637 goto out;
7638 if (clp->cl_exchange_flags == 0)
7639 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007640 if (clp->cl_preserve_clid)
7641 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007642 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007643 ret = nfs4_proc_destroy_clientid(clp, cred);
7644 if (cred)
7645 put_rpccred(cred);
7646 switch (ret) {
7647 case 0:
7648 case -NFS4ERR_STALE_CLIENTID:
7649 clp->cl_exchange_flags = 0;
7650 }
7651out:
7652 return ret;
7653}
7654
Andy Adamson2050f0c2009-04-01 09:22:30 -04007655struct nfs4_get_lease_time_data {
7656 struct nfs4_get_lease_time_args *args;
7657 struct nfs4_get_lease_time_res *res;
7658 struct nfs_client *clp;
7659};
7660
7661static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7662 void *calldata)
7663{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007664 struct nfs4_get_lease_time_data *data =
7665 (struct nfs4_get_lease_time_data *)calldata;
7666
7667 dprintk("--> %s\n", __func__);
7668 /* just setup sequence, do not trigger session recovery
7669 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007670 nfs41_setup_sequence(data->clp->cl_session,
7671 &data->args->la_seq_args,
7672 &data->res->lr_seq_res,
7673 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007674 dprintk("<-- %s\n", __func__);
7675}
7676
7677/*
7678 * Called from nfs4_state_manager thread for session setup, so don't recover
7679 * from sequence operation or clientid errors.
7680 */
7681static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7682{
7683 struct nfs4_get_lease_time_data *data =
7684 (struct nfs4_get_lease_time_data *)calldata;
7685
7686 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007687 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7688 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007689 switch (task->tk_status) {
7690 case -NFS4ERR_DELAY:
7691 case -NFS4ERR_GRACE:
7692 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7693 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7694 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007695 /* fall through */
7696 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007697 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007698 return;
7699 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007700 dprintk("<-- %s\n", __func__);
7701}
7702
Trond Myklebust17280172012-03-11 13:11:00 -04007703static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007704 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7705 .rpc_call_done = nfs4_get_lease_time_done,
7706};
7707
7708int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7709{
7710 struct rpc_task *task;
7711 struct nfs4_get_lease_time_args args;
7712 struct nfs4_get_lease_time_res res = {
7713 .lr_fsinfo = fsinfo,
7714 };
7715 struct nfs4_get_lease_time_data data = {
7716 .args = &args,
7717 .res = &res,
7718 .clp = clp,
7719 };
7720 struct rpc_message msg = {
7721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7722 .rpc_argp = &args,
7723 .rpc_resp = &res,
7724 };
7725 struct rpc_task_setup task_setup = {
7726 .rpc_client = clp->cl_rpcclient,
7727 .rpc_message = &msg,
7728 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007729 .callback_data = &data,
7730 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007731 };
7732 int status;
7733
Chuck Levera9c92d62013-08-09 12:48:18 -04007734 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007735 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007736 dprintk("--> %s\n", __func__);
7737 task = rpc_run_task(&task_setup);
7738
7739 if (IS_ERR(task))
7740 status = PTR_ERR(task);
7741 else {
7742 status = task->tk_status;
7743 rpc_put_task(task);
7744 }
7745 dprintk("<-- %s return %d\n", __func__, status);
7746
7747 return status;
7748}
7749
Andy Adamsonfc931582009-04-01 09:22:31 -04007750/*
7751 * Initialize the values to be used by the client in CREATE_SESSION
7752 * If nfs4_init_session set the fore channel request and response sizes,
7753 * use them.
7754 *
7755 * Set the back channel max_resp_sz_cached to zero to force the client to
7756 * always set csa_cachethis to FALSE because the current implementation
7757 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7758 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007759static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7760 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007761{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007762 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007763 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007764
Andy Adamson18aad3d2013-06-26 12:21:49 -04007765 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7766 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7767
Andy Adamsonfc931582009-04-01 09:22:31 -04007768 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007769 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7770 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007771 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007772 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007773
7774 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007775 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007776 __func__,
7777 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007778 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007779
7780 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007781 args->bc_attrs.max_rqst_sz = max_bc_payload;
7782 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007783 args->bc_attrs.max_resp_sz_cached = 0;
7784 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007785 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007786
7787 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7788 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7789 __func__,
7790 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7791 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7792 args->bc_attrs.max_reqs);
7793}
7794
Trond Myklebust79969dd2015-02-18 11:30:18 -08007795static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7796 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007797{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007798 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007799 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007800
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007801 if (rcvd->max_resp_sz > sent->max_resp_sz)
7802 return -EINVAL;
7803 /*
7804 * Our requested max_ops is the minimum we need; we're not
7805 * prepared to break up compounds into smaller pieces than that.
7806 * So, no point even trying to continue if the server won't
7807 * cooperate:
7808 */
7809 if (rcvd->max_ops < sent->max_ops)
7810 return -EINVAL;
7811 if (rcvd->max_reqs == 0)
7812 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007813 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7814 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007815 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007816}
7817
Trond Myklebust79969dd2015-02-18 11:30:18 -08007818static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7819 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007820{
7821 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007822 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007823
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007824 if (!(res->flags & SESSION4_BACK_CHAN))
7825 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007826 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7827 return -EINVAL;
7828 if (rcvd->max_resp_sz < sent->max_resp_sz)
7829 return -EINVAL;
7830 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7831 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007832 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007833 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007834 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007835 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007836out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007837 return 0;
7838}
Andy Adamson8d353012009-04-01 09:22:32 -04007839
Andy Adamson8d353012009-04-01 09:22:32 -04007840static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007841 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007842{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007843 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007844
Trond Myklebust79969dd2015-02-18 11:30:18 -08007845 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007846 if (ret)
7847 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007848 return nfs4_verify_back_channel_attrs(args, res);
7849}
7850
7851static void nfs4_update_session(struct nfs4_session *session,
7852 struct nfs41_create_session_res *res)
7853{
7854 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007855 /* Mark client id and session as being confirmed */
7856 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7857 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007858 session->flags = res->flags;
7859 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007860 if (res->flags & SESSION4_BACK_CHAN)
7861 memcpy(&session->bc_attrs, &res->bc_attrs,
7862 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007863}
7864
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007865static int _nfs4_proc_create_session(struct nfs_client *clp,
7866 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007867{
7868 struct nfs4_session *session = clp->cl_session;
7869 struct nfs41_create_session_args args = {
7870 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007871 .clientid = clp->cl_clientid,
7872 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007873 .cb_program = NFS4_CALLBACK,
7874 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007875 struct nfs41_create_session_res res;
7876
Andy Adamsonfc931582009-04-01 09:22:31 -04007877 struct rpc_message msg = {
7878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7879 .rpc_argp = &args,
7880 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007881 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007882 };
7883 int status;
7884
Chuck Lever6b26cc82016-05-02 14:40:40 -04007885 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007886 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007887
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007888 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007889 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007890
Trond Myklebustb519d402016-09-11 14:50:01 -04007891 switch (status) {
7892 case -NFS4ERR_STALE_CLIENTID:
7893 case -NFS4ERR_DELAY:
7894 case -ETIMEDOUT:
7895 case -EACCES:
7896 case -EAGAIN:
7897 goto out;
7898 };
7899
7900 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007901 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007902 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007903 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007904 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007905 if (status)
7906 goto out;
7907 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007908 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007909out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007910 return status;
7911}
7912
7913/*
7914 * Issues a CREATE_SESSION operation to the server.
7915 * It is the responsibility of the caller to verify the session is
7916 * expired before calling this routine.
7917 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007918int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007919{
7920 int status;
7921 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007922 struct nfs4_session *session = clp->cl_session;
7923
7924 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7925
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007926 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007927 if (status)
7928 goto out;
7929
Andy Adamsonaacd5532011-11-09 13:58:21 -05007930 /* Init or reset the session slot tables */
7931 status = nfs4_setup_session_slot_tables(session);
7932 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007933 if (status)
7934 goto out;
7935
7936 ptr = (unsigned *)&session->sess_id.data[0];
7937 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7938 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007939out:
7940 dprintk("<-- %s\n", __func__);
7941 return status;
7942}
7943
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007944/*
7945 * Issue the over-the-wire RPC DESTROY_SESSION.
7946 * The caller must serialize access to this routine.
7947 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007948int nfs4_proc_destroy_session(struct nfs4_session *session,
7949 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007950{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007951 struct rpc_message msg = {
7952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7953 .rpc_argp = session,
7954 .rpc_cred = cred,
7955 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007956 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007957
7958 dprintk("--> nfs4_proc_destroy_session\n");
7959
7960 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007961 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7962 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007963
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007964 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007965 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007966
7967 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007968 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007969 "Session has been destroyed regardless...\n", status);
7970
7971 dprintk("<-- nfs4_proc_destroy_session\n");
7972 return status;
7973}
7974
Trond Myklebust7b38c362012-05-23 13:23:31 -04007975/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007976 * Renew the cl_session lease.
7977 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007978struct nfs4_sequence_data {
7979 struct nfs_client *clp;
7980 struct nfs4_sequence_args args;
7981 struct nfs4_sequence_res res;
7982};
7983
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007984static void nfs41_sequence_release(void *data)
7985{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007986 struct nfs4_sequence_data *calldata = data;
7987 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007988
Alexandros Batsakis71358402010-02-05 03:45:05 -08007989 if (atomic_read(&clp->cl_count) > 1)
7990 nfs4_schedule_state_renewal(clp);
7991 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007992 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007993}
7994
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007995static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7996{
7997 switch(task->tk_status) {
7998 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007999 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8000 return -EAGAIN;
8001 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008002 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008003 }
8004 return 0;
8005}
8006
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008007static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008008{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008009 struct nfs4_sequence_data *calldata = data;
8010 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008011
Trond Myklebust14516c32010-07-31 14:29:06 -04008012 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8013 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008014
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008015 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008016 if (task->tk_status < 0) {
8017 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008018 if (atomic_read(&clp->cl_count) == 1)
8019 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008020
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008021 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8022 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008023 return;
8024 }
8025 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008026 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008027out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008028 dprintk("<-- %s\n", __func__);
8029}
8030
8031static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8032{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008033 struct nfs4_sequence_data *calldata = data;
8034 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008035 struct nfs4_sequence_args *args;
8036 struct nfs4_sequence_res *res;
8037
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008038 args = task->tk_msg.rpc_argp;
8039 res = task->tk_msg.rpc_resp;
8040
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008041 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008042}
8043
8044static const struct rpc_call_ops nfs41_sequence_ops = {
8045 .rpc_call_done = nfs41_sequence_call_done,
8046 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008047 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008048};
8049
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008050static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8051 struct rpc_cred *cred,
8052 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008053{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008054 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008055 struct rpc_message msg = {
8056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8057 .rpc_cred = cred,
8058 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008059 struct rpc_task_setup task_setup_data = {
8060 .rpc_client = clp->cl_rpcclient,
8061 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008062 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008063 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008064 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008065
Alexandros Batsakis71358402010-02-05 03:45:05 -08008066 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008067 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008068 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008069 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008070 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008071 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008073 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008074 if (is_privileged)
8075 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008076 msg.rpc_argp = &calldata->args;
8077 msg.rpc_resp = &calldata->res;
8078 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008079 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008081 return rpc_run_task(&task_setup_data);
8082}
8083
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008084static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008085{
8086 struct rpc_task *task;
8087 int ret = 0;
8088
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008089 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008090 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008091 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 if (IS_ERR(task))
8093 ret = PTR_ERR(task);
8094 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008095 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008096 dprintk("<-- %s status=%d\n", __func__, ret);
8097 return ret;
8098}
8099
8100static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8101{
8102 struct rpc_task *task;
8103 int ret;
8104
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008105 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106 if (IS_ERR(task)) {
8107 ret = PTR_ERR(task);
8108 goto out;
8109 }
8110 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008111 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008112 ret = task->tk_status;
8113 rpc_put_task(task);
8114out:
8115 dprintk("<-- %s status=%d\n", __func__, ret);
8116 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008117}
8118
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008119struct nfs4_reclaim_complete_data {
8120 struct nfs_client *clp;
8121 struct nfs41_reclaim_complete_args arg;
8122 struct nfs41_reclaim_complete_res res;
8123};
8124
8125static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8126{
8127 struct nfs4_reclaim_complete_data *calldata = data;
8128
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008129 nfs41_setup_sequence(calldata->clp->cl_session,
8130 &calldata->arg.seq_args,
8131 &calldata->res.seq_res,
8132 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008133}
8134
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008135static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8136{
8137 switch(task->tk_status) {
8138 case 0:
8139 case -NFS4ERR_COMPLETE_ALREADY:
8140 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8141 break;
8142 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008143 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008144 /* fall through */
8145 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008146 return -EAGAIN;
8147 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008148 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008149 }
8150 return 0;
8151}
8152
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008153static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8154{
8155 struct nfs4_reclaim_complete_data *calldata = data;
8156 struct nfs_client *clp = calldata->clp;
8157 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8158
8159 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008160 if (!nfs41_sequence_done(task, res))
8161 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008162
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008163 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008164 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8165 rpc_restart_call_prepare(task);
8166 return;
8167 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008168 dprintk("<-- %s\n", __func__);
8169}
8170
8171static void nfs4_free_reclaim_complete_data(void *data)
8172{
8173 struct nfs4_reclaim_complete_data *calldata = data;
8174
8175 kfree(calldata);
8176}
8177
8178static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8179 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8180 .rpc_call_done = nfs4_reclaim_complete_done,
8181 .rpc_release = nfs4_free_reclaim_complete_data,
8182};
8183
8184/*
8185 * Issue a global reclaim complete.
8186 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008187static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8188 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008189{
8190 struct nfs4_reclaim_complete_data *calldata;
8191 struct rpc_task *task;
8192 struct rpc_message msg = {
8193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008194 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008195 };
8196 struct rpc_task_setup task_setup_data = {
8197 .rpc_client = clp->cl_rpcclient,
8198 .rpc_message = &msg,
8199 .callback_ops = &nfs4_reclaim_complete_call_ops,
8200 .flags = RPC_TASK_ASYNC,
8201 };
8202 int status = -ENOMEM;
8203
8204 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008205 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008206 if (calldata == NULL)
8207 goto out;
8208 calldata->clp = clp;
8209 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008210
Chuck Levera9c92d62013-08-09 12:48:18 -04008211 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008212 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008213 msg.rpc_argp = &calldata->arg;
8214 msg.rpc_resp = &calldata->res;
8215 task_setup_data.callback_data = calldata;
8216 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008217 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008218 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008219 goto out;
8220 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008221 status = nfs4_wait_for_completion_rpc_task(task);
8222 if (status == 0)
8223 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008224 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008225 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008226out:
8227 dprintk("<-- %s status=%d\n", __func__, status);
8228 return status;
8229}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008230
8231static void
8232nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8233{
8234 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008235 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008236 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008237
8238 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008239 nfs41_setup_sequence(session, &lgp->args.seq_args,
8240 &lgp->res.seq_res, task);
8241 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008242}
8243
8244static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8245{
8246 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008247
8248 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008249 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008250 dprintk("<-- %s\n", __func__);
8251}
8252
8253static int
8254nfs4_layoutget_handle_exception(struct rpc_task *task,
8255 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8256{
Trond Myklebustee314c22012-10-01 17:25:48 -07008257 struct inode *inode = lgp->args.inode;
8258 struct nfs_server *server = NFS_SERVER(inode);
8259 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008260 int nfs4err = task->tk_status;
8261 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008262 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008263
Boaz Harroshed7e5422014-01-22 20:34:54 +02008264 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008265
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008266 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008267 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008268 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008269
8270 /*
8271 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8272 * on the file. set tk_status to -ENODATA to tell upper layer to
8273 * retry go inband.
8274 */
8275 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008276 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008277 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008278 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008279 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8280 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8281 */
8282 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008283 status = -EOVERFLOW;
8284 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008285 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008286 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008287 * (or clients) writing to the same RAID stripe except when
8288 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008289 *
8290 * Treat it like we would RECALLCONFLICT -- we retry for a little
8291 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008292 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008293 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008294 if (lgp->args.minlength == 0) {
8295 status = -EOVERFLOW;
8296 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008297 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008298 status = -EBUSY;
8299 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008300 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008301 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008302 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008303 case -NFS4ERR_DELEG_REVOKED:
8304 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008305 case -NFS4ERR_EXPIRED:
8306 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008307 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008308 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008309 lo = NFS_I(inode)->layout;
8310 /* If the open stateid was bad, then recover it. */
8311 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8312 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008313 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008314 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008315 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008316 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008317 break;
8318 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008319
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008320 /*
8321 * Mark the bad layout state as invalid, then retry
8322 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008323 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008324 spin_unlock(&inode->i_lock);
8325 pnfs_free_lseg_list(&head);
8326 status = -EAGAIN;
8327 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008328 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008329
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008330 err = nfs4_handle_exception(server, nfs4err, exception);
8331 if (!status) {
8332 if (exception->retry)
8333 status = -EAGAIN;
8334 else
8335 status = err;
8336 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008337out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008338 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008339 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008340}
8341
Idan Kedar85541162012-08-02 11:47:10 +03008342static size_t max_response_pages(struct nfs_server *server)
8343{
8344 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8345 return nfs_page_array_len(0, max_resp_sz);
8346}
8347
8348static void nfs4_free_pages(struct page **pages, size_t size)
8349{
8350 int i;
8351
8352 if (!pages)
8353 return;
8354
8355 for (i = 0; i < size; i++) {
8356 if (!pages[i])
8357 break;
8358 __free_page(pages[i]);
8359 }
8360 kfree(pages);
8361}
8362
8363static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8364{
8365 struct page **pages;
8366 int i;
8367
8368 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8369 if (!pages) {
8370 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8371 return NULL;
8372 }
8373
8374 for (i = 0; i < size; i++) {
8375 pages[i] = alloc_page(gfp_flags);
8376 if (!pages[i]) {
8377 dprintk("%s: failed to allocate page\n", __func__);
8378 nfs4_free_pages(pages, size);
8379 return NULL;
8380 }
8381 }
8382
8383 return pages;
8384}
8385
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008386static void nfs4_layoutget_release(void *calldata)
8387{
8388 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008389 struct inode *inode = lgp->args.inode;
8390 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008391 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008392
8393 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008394 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008395 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396 put_nfs_open_context(lgp->args.ctx);
8397 kfree(calldata);
8398 dprintk("<-- %s\n", __func__);
8399}
8400
8401static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8402 .rpc_call_prepare = nfs4_layoutget_prepare,
8403 .rpc_call_done = nfs4_layoutget_done,
8404 .rpc_release = nfs4_layoutget_release,
8405};
8406
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008407struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008408nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008409{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008410 struct inode *inode = lgp->args.inode;
8411 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008412 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008413 struct rpc_task *task;
8414 struct rpc_message msg = {
8415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8416 .rpc_argp = &lgp->args,
8417 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008418 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008419 };
8420 struct rpc_task_setup task_setup_data = {
8421 .rpc_client = server->client,
8422 .rpc_message = &msg,
8423 .callback_ops = &nfs4_layoutget_call_ops,
8424 .callback_data = lgp,
8425 .flags = RPC_TASK_ASYNC,
8426 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008427 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008428 struct nfs4_exception exception = {
8429 .inode = inode,
8430 .timeout = *timeout,
8431 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008432 int status = 0;
8433
8434 dprintk("--> %s\n", __func__);
8435
Peng Tao4bd5a982014-11-17 11:05:17 +08008436 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8437 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8438
Idan Kedar85541162012-08-02 11:47:10 +03008439 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8440 if (!lgp->args.layout.pages) {
8441 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008442 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008443 }
8444 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8445
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008446 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008447 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008448 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008449
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008450 task = rpc_run_task(&task_setup_data);
8451 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008452 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008453 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008454 if (status == 0) {
8455 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8456 *timeout = exception.timeout;
8457 }
8458
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008459 trace_nfs4_layoutget(lgp->args.ctx,
8460 &lgp->args.range,
8461 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008462 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008463 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008464
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008465 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8466 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008467 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008468 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008469 rpc_put_task(task);
8470 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008471 if (status)
8472 return ERR_PTR(status);
8473 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008474}
8475
Benny Halevycbe82602011-05-22 19:52:37 +03008476static void
8477nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8478{
8479 struct nfs4_layoutreturn *lrp = calldata;
8480
8481 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008482 nfs41_setup_sequence(lrp->clp->cl_session,
8483 &lrp->args.seq_args,
8484 &lrp->res.seq_res,
8485 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008486}
8487
8488static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8489{
8490 struct nfs4_layoutreturn *lrp = calldata;
8491 struct nfs_server *server;
8492
8493 dprintk("--> %s\n", __func__);
8494
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008495 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008496 return;
8497
8498 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008499 switch (task->tk_status) {
8500 default:
8501 task->tk_status = 0;
8502 case 0:
8503 break;
8504 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008505 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008506 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008507 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008508 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008509 return;
8510 }
Benny Halevycbe82602011-05-22 19:52:37 +03008511 dprintk("<-- %s\n", __func__);
8512}
8513
8514static void nfs4_layoutreturn_release(void *calldata)
8515{
8516 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008517 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008518 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008519
8520 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008521 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008522 if (lrp->res.lrs_present) {
8523 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8524 &lrp->args.range,
8525 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008526 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008527 } else
8528 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008529 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008530 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008531 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008532 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008533 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008534 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008535 kfree(calldata);
8536 dprintk("<-- %s\n", __func__);
8537}
8538
8539static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8540 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8541 .rpc_call_done = nfs4_layoutreturn_done,
8542 .rpc_release = nfs4_layoutreturn_release,
8543};
8544
Peng Tao6c166052014-11-17 09:30:40 +08008545int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008546{
8547 struct rpc_task *task;
8548 struct rpc_message msg = {
8549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8550 .rpc_argp = &lrp->args,
8551 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008552 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008553 };
8554 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008555 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008556 .rpc_message = &msg,
8557 .callback_ops = &nfs4_layoutreturn_call_ops,
8558 .callback_data = lrp,
8559 };
Peng Tao6c166052014-11-17 09:30:40 +08008560 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008561
Andrew Elble99ade3c2015-12-02 09:39:51 -05008562 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8563 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8564 &task_setup_data.rpc_client, &msg);
8565
Benny Halevycbe82602011-05-22 19:52:37 +03008566 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008567 if (!sync) {
8568 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8569 if (!lrp->inode) {
8570 nfs4_layoutreturn_release(lrp);
8571 return -EAGAIN;
8572 }
8573 task_setup_data.flags |= RPC_TASK_ASYNC;
8574 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008575 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008576 task = rpc_run_task(&task_setup_data);
8577 if (IS_ERR(task))
8578 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008579 if (sync)
8580 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008581 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008582 dprintk("<-- %s status=%d\n", __func__, status);
8583 rpc_put_task(task);
8584 return status;
8585}
8586
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008587static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008588_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8589 struct pnfs_device *pdev,
8590 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008591{
8592 struct nfs4_getdeviceinfo_args args = {
8593 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008594 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8595 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008596 };
8597 struct nfs4_getdeviceinfo_res res = {
8598 .pdev = pdev,
8599 };
8600 struct rpc_message msg = {
8601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8602 .rpc_argp = &args,
8603 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008604 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008605 };
8606 int status;
8607
8608 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008609 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008610 if (res.notification & ~args.notify_types)
8611 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008612 if (res.notification != args.notify_types)
8613 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008614
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615 dprintk("<-- %s status=%d\n", __func__, status);
8616
8617 return status;
8618}
8619
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008620int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8621 struct pnfs_device *pdev,
8622 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008623{
8624 struct nfs4_exception exception = { };
8625 int err;
8626
8627 do {
8628 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008629 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008630 &exception);
8631 } while (exception.retry);
8632 return err;
8633}
8634EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8635
Andy Adamson863a3c62011-03-23 13:27:54 +00008636static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8637{
8638 struct nfs4_layoutcommit_data *data = calldata;
8639 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008640 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008641
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008642 nfs41_setup_sequence(session,
8643 &data->args.seq_args,
8644 &data->res.seq_res,
8645 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008646}
8647
8648static void
8649nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8650{
8651 struct nfs4_layoutcommit_data *data = calldata;
8652 struct nfs_server *server = NFS_SERVER(data->args.inode);
8653
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008654 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008655 return;
8656
8657 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008658 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8659 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8660 case -NFS4ERR_BADLAYOUT: /* no layout */
8661 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008662 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008663 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008664 break;
8665 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008666 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008667 rpc_restart_call_prepare(task);
8668 return;
8669 }
8670 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008671}
8672
8673static void nfs4_layoutcommit_release(void *calldata)
8674{
8675 struct nfs4_layoutcommit_data *data = calldata;
8676
Andy Adamsondb29c082011-07-30 20:52:38 -04008677 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008678 nfs_post_op_update_inode_force_wcc(data->args.inode,
8679 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008680 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008681 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008682 kfree(data);
8683}
8684
8685static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8686 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8687 .rpc_call_done = nfs4_layoutcommit_done,
8688 .rpc_release = nfs4_layoutcommit_release,
8689};
8690
8691int
Andy Adamsonef311532011-03-12 02:58:10 -05008692nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008693{
8694 struct rpc_message msg = {
8695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8696 .rpc_argp = &data->args,
8697 .rpc_resp = &data->res,
8698 .rpc_cred = data->cred,
8699 };
8700 struct rpc_task_setup task_setup_data = {
8701 .task = &data->task,
8702 .rpc_client = NFS_CLIENT(data->args.inode),
8703 .rpc_message = &msg,
8704 .callback_ops = &nfs4_layoutcommit_ops,
8705 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008706 };
8707 struct rpc_task *task;
8708 int status = 0;
8709
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008710 dprintk("NFS: initiating layoutcommit call. sync %d "
8711 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008712 data->args.lastbytewritten,
8713 data->args.inode->i_ino);
8714
Trond Myklebust472e2592015-02-05 16:50:30 -05008715 if (!sync) {
8716 data->inode = nfs_igrab_and_active(data->args.inode);
8717 if (data->inode == NULL) {
8718 nfs4_layoutcommit_release(data);
8719 return -EAGAIN;
8720 }
8721 task_setup_data.flags = RPC_TASK_ASYNC;
8722 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008723 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008724 task = rpc_run_task(&task_setup_data);
8725 if (IS_ERR(task))
8726 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008727 if (sync)
8728 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008729 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008730 dprintk("%s: status %d\n", __func__, status);
8731 rpc_put_task(task);
8732 return status;
8733}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008734
Andy Adamson97431202013-08-08 10:57:56 -04008735/**
8736 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8737 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8738 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008739static int
8740_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008741 struct nfs_fsinfo *info,
8742 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008743{
8744 struct nfs41_secinfo_no_name_args args = {
8745 .style = SECINFO_STYLE_CURRENT_FH,
8746 };
8747 struct nfs4_secinfo_res res = {
8748 .flavors = flavors,
8749 };
8750 struct rpc_message msg = {
8751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8752 .rpc_argp = &args,
8753 .rpc_resp = &res,
8754 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008755 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008756 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008757 int status;
8758
8759 if (use_integrity) {
8760 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008761 cred = nfs4_get_clid_cred(server->nfs_client);
8762 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008763 }
8764
8765 dprintk("--> %s\n", __func__);
8766 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8767 &res.seq_res, 0);
8768 dprintk("<-- %s status=%d\n", __func__, status);
8769
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008770 if (cred)
8771 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008772
8773 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008774}
8775
8776static int
8777nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8778 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8779{
8780 struct nfs4_exception exception = { };
8781 int err;
8782 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008783 /* first try using integrity protection */
8784 err = -NFS4ERR_WRONGSEC;
8785
8786 /* try to use integrity protection with machine cred */
8787 if (_nfs4_is_integrity_protected(server->nfs_client))
8788 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8789 flavors, true);
8790
8791 /*
8792 * if unable to use integrity protection, or SECINFO with
8793 * integrity protection returns NFS4ERR_WRONGSEC (which is
8794 * disallowed by spec, but exists in deployed servers) use
8795 * the current filesystem's rpc_client and the user cred.
8796 */
8797 if (err == -NFS4ERR_WRONGSEC)
8798 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8799 flavors, false);
8800
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801 switch (err) {
8802 case 0:
8803 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008804 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008805 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008806 default:
8807 err = nfs4_handle_exception(server, err, &exception);
8808 }
8809 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008810out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008811 return err;
8812}
8813
8814static int
8815nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8816 struct nfs_fsinfo *info)
8817{
8818 int err;
8819 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008820 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008821 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008822 struct nfs4_secinfo4 *secinfo;
8823 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008824
8825 page = alloc_page(GFP_KERNEL);
8826 if (!page) {
8827 err = -ENOMEM;
8828 goto out;
8829 }
8830
8831 flavors = page_address(page);
8832 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8833
8834 /*
8835 * Fall back on "guess and check" method if
8836 * the server doesn't support SECINFO_NO_NAME
8837 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008838 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008839 err = nfs4_find_root_sec(server, fhandle, info);
8840 goto out_freepage;
8841 }
8842 if (err)
8843 goto out_freepage;
8844
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008845 for (i = 0; i < flavors->num_flavors; i++) {
8846 secinfo = &flavors->flavors[i];
8847
8848 switch (secinfo->flavor) {
8849 case RPC_AUTH_NULL:
8850 case RPC_AUTH_UNIX:
8851 case RPC_AUTH_GSS:
8852 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8853 &secinfo->flavor_info);
8854 break;
8855 default:
8856 flavor = RPC_AUTH_MAXFLAVOR;
8857 break;
8858 }
8859
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008860 if (!nfs_auth_info_match(&server->auth_info, flavor))
8861 flavor = RPC_AUTH_MAXFLAVOR;
8862
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008863 if (flavor != RPC_AUTH_MAXFLAVOR) {
8864 err = nfs4_lookup_root_sec(server, fhandle,
8865 info, flavor);
8866 if (!err)
8867 break;
8868 }
8869 }
8870
8871 if (flavor == RPC_AUTH_MAXFLAVOR)
8872 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873
8874out_freepage:
8875 put_page(page);
8876 if (err == -EACCES)
8877 return -EPERM;
8878out:
8879 return err;
8880}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008881
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008882static int _nfs41_test_stateid(struct nfs_server *server,
8883 nfs4_stateid *stateid,
8884 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008885{
8886 int status;
8887 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008888 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008889 };
8890 struct nfs41_test_stateid_res res;
8891 struct rpc_message msg = {
8892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8893 .rpc_argp = &args,
8894 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008895 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008896 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008897 struct rpc_clnt *rpc_client = server->client;
8898
8899 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8900 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008901
Chuck Lever38527b12012-07-11 16:30:23 -04008902 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008903 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008904 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008905 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008906 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008907 if (status != NFS_OK) {
8908 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008909 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008910 }
8911 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008912 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008913}
8914
Trond Myklebust43912bb2016-09-22 13:38:56 -04008915static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8916 int err, struct nfs4_exception *exception)
8917{
8918 exception->retry = 0;
8919 switch(err) {
8920 case -NFS4ERR_DELAY:
8921 nfs4_handle_exception(server, err, exception);
8922 break;
8923 case -NFS4ERR_BADSESSION:
8924 case -NFS4ERR_BADSLOT:
8925 case -NFS4ERR_BAD_HIGH_SLOT:
8926 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8927 case -NFS4ERR_DEADSESSION:
8928 nfs4_do_handle_exception(server, err, exception);
8929 }
8930}
8931
Chuck Lever38527b12012-07-11 16:30:23 -04008932/**
8933 * nfs41_test_stateid - perform a TEST_STATEID operation
8934 *
8935 * @server: server / transport on which to perform the operation
8936 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008937 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008938 *
8939 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8940 * Otherwise a negative NFS4ERR value is returned if the operation
8941 * failed or the state ID is not currently valid.
8942 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008943static int nfs41_test_stateid(struct nfs_server *server,
8944 nfs4_stateid *stateid,
8945 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008946{
8947 struct nfs4_exception exception = { };
8948 int err;
8949 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008950 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008951 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008952 } while (exception.retry);
8953 return err;
8954}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008955
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008956struct nfs_free_stateid_data {
8957 struct nfs_server *server;
8958 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008959 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008960};
8961
8962static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8963{
8964 struct nfs_free_stateid_data *data = calldata;
8965 nfs41_setup_sequence(nfs4_get_session(data->server),
8966 &data->args.seq_args,
8967 &data->res.seq_res,
8968 task);
8969}
8970
8971static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8972{
8973 struct nfs_free_stateid_data *data = calldata;
8974
8975 nfs41_sequence_done(task, &data->res.seq_res);
8976
8977 switch (task->tk_status) {
8978 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008979 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008980 rpc_restart_call_prepare(task);
8981 }
8982}
8983
8984static void nfs41_free_stateid_release(void *calldata)
8985{
8986 kfree(calldata);
8987}
8988
Trond Myklebust17f26b12013-08-21 15:48:42 -04008989static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008990 .rpc_call_prepare = nfs41_free_stateid_prepare,
8991 .rpc_call_done = nfs41_free_stateid_done,
8992 .rpc_release = nfs41_free_stateid_release,
8993};
8994
8995static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04008996 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008997 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008998 bool privileged)
8999{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009000 struct rpc_message msg = {
9001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009002 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009003 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009004 struct rpc_task_setup task_setup = {
9005 .rpc_client = server->client,
9006 .rpc_message = &msg,
9007 .callback_ops = &nfs41_free_stateid_ops,
9008 .flags = RPC_TASK_ASYNC,
9009 };
9010 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009011
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009012 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9013 &task_setup.rpc_client, &msg);
9014
Chuck Lever38527b12012-07-11 16:30:23 -04009015 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009016 data = kmalloc(sizeof(*data), GFP_NOFS);
9017 if (!data)
9018 return ERR_PTR(-ENOMEM);
9019 data->server = server;
9020 nfs4_stateid_copy(&data->args.stateid, stateid);
9021
9022 task_setup.callback_data = data;
9023
9024 msg.rpc_argp = &data->args;
9025 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04009026 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009027 if (privileged)
9028 nfs4_set_sequence_privileged(&data->args.seq_args);
9029
9030 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009031}
9032
Chuck Lever38527b12012-07-11 16:30:23 -04009033/**
9034 * nfs41_free_stateid - perform a FREE_STATEID operation
9035 *
9036 * @server: server / transport on which to perform the operation
9037 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009038 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009039 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009040 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009041 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009042 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009043static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009044 const nfs4_stateid *stateid,
9045 struct rpc_cred *cred,
9046 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009047{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009050 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009051 if (IS_ERR(task))
9052 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009054 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009055}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009056
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009057static void
9058nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009059{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009060 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009061
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009062 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009063 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009064}
9065
Trond Myklebust36281ca2012-03-04 18:13:56 -05009066static bool nfs41_match_stateid(const nfs4_stateid *s1,
9067 const nfs4_stateid *s2)
9068{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009069 if (s1->type != s2->type)
9070 return false;
9071
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009072 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009073 return false;
9074
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009075 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009076 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009077 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009078 return true;
9079
9080 return false;
9081}
9082
Andy Adamson557134a2009-04-01 09:21:53 -04009083#endif /* CONFIG_NFS_V4_1 */
9084
Trond Myklebust36281ca2012-03-04 18:13:56 -05009085static bool nfs4_match_stateid(const nfs4_stateid *s1,
9086 const nfs4_stateid *s2)
9087{
Trond Myklebustf597c532012-03-04 18:13:56 -05009088 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009089}
9090
9091
Trond Myklebust17280172012-03-11 13:11:00 -04009092static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009093 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009094 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095 .recover_open = nfs4_open_reclaim,
9096 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009097 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009098 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099};
9100
Andy Adamson591d71c2009-04-01 09:22:47 -04009101#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009102static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009103 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9104 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9105 .recover_open = nfs4_open_reclaim,
9106 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009107 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009108 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009109 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009110};
9111#endif /* CONFIG_NFS_V4_1 */
9112
Trond Myklebust17280172012-03-11 13:11:00 -04009113static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009114 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009115 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009116 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009118 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119};
9120
Andy Adamson591d71c2009-04-01 09:22:47 -04009121#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009122static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009123 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9124 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009125 .recover_open = nfs41_open_expired,
9126 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009127 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009128};
9129#endif /* CONFIG_NFS_V4_1 */
9130
Trond Myklebust17280172012-03-11 13:11:00 -04009131static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009132 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009133 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009134 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009135};
9136
9137#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009138static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009139 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009140 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009141 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009142};
9143#endif
9144
Chuck Leverec011fe2013-10-17 14:12:39 -04009145static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009146 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009147 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009148};
9149
9150#if defined(CONFIG_NFS_V4_1)
9151static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009152 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009153 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009154};
9155#endif /* CONFIG_NFS_V4_1 */
9156
Trond Myklebust97dc1352010-06-16 09:52:26 -04009157static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9158 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009159 .init_caps = NFS_CAP_READDIRPLUS
9160 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009161 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009162 .init_client = nfs40_init_client,
9163 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009164 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009165 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009166 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009167 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009168 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009169 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009170 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9171 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9172 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009173 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009174};
9175
9176#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009177static struct nfs_seqid *
9178nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9179{
9180 return NULL;
9181}
9182
Trond Myklebust97dc1352010-06-16 09:52:26 -04009183static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9184 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009185 .init_caps = NFS_CAP_READDIRPLUS
9186 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009187 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009188 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009189 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009190 .init_client = nfs41_init_client,
9191 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009192 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009193 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009194 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009195 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009196 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009197 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009198 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009199 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9200 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9201 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009202 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009203};
9204#endif
9205
Steve Dickson42c2c422013-05-22 12:50:38 -04009206#if defined(CONFIG_NFS_V4_2)
9207static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9208 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009209 .init_caps = NFS_CAP_READDIRPLUS
9210 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009211 | NFS_CAP_POSIX_LOCK
9212 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009213 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009214 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009215 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009216 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009217 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009218 | NFS_CAP_LAYOUTSTATS
9219 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009220 .init_client = nfs41_init_client,
9221 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009222 .match_stateid = nfs41_match_stateid,
9223 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009224 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009225 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009226 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009227 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009228 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009229 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9230 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9231 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009232 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009233};
9234#endif
9235
Trond Myklebust97dc1352010-06-16 09:52:26 -04009236const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9237 [0] = &nfs_v4_0_minor_ops,
9238#if defined(CONFIG_NFS_V4_1)
9239 [1] = &nfs_v4_1_minor_ops,
9240#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009241#if defined(CONFIG_NFS_V4_2)
9242 [2] = &nfs_v4_2_minor_ops,
9243#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009244};
9245
Trond Myklebust13997822016-07-24 17:10:52 -04009246static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009247{
9248 ssize_t error, error2;
9249
9250 error = generic_listxattr(dentry, list, size);
9251 if (error < 0)
9252 return error;
9253 if (list) {
9254 list += error;
9255 size -= error;
9256 }
9257
9258 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9259 if (error2 < 0)
9260 return error2;
9261 return error + error2;
9262}
9263
Trond Myklebust17f26b12013-08-21 15:48:42 -04009264static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009265 .create = nfs_create,
9266 .lookup = nfs_lookup,
9267 .atomic_open = nfs_atomic_open,
9268 .link = nfs_link,
9269 .unlink = nfs_unlink,
9270 .symlink = nfs_symlink,
9271 .mkdir = nfs_mkdir,
9272 .rmdir = nfs_rmdir,
9273 .mknod = nfs_mknod,
9274 .rename = nfs_rename,
9275 .permission = nfs_permission,
9276 .getattr = nfs_getattr,
9277 .setattr = nfs_setattr,
9278 .getxattr = generic_getxattr,
9279 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009280 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009281 .removexattr = generic_removexattr,
9282};
9283
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009284static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009285 .permission = nfs_permission,
9286 .getattr = nfs_getattr,
9287 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009288 .getxattr = generic_getxattr,
9289 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009290 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009291 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009292};
9293
David Howells509de812006-08-22 20:06:11 -04009294const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 .version = 4, /* protocol version */
9296 .dentry_ops = &nfs4_dentry_operations,
9297 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009298 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009299 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009301 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009302 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303 .getattr = nfs4_proc_getattr,
9304 .setattr = nfs4_proc_setattr,
9305 .lookup = nfs4_proc_lookup,
9306 .access = nfs4_proc_access,
9307 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 .create = nfs4_proc_create,
9309 .remove = nfs4_proc_remove,
9310 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009311 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009313 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009314 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009315 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 .link = nfs4_proc_link,
9317 .symlink = nfs4_proc_symlink,
9318 .mkdir = nfs4_proc_mkdir,
9319 .rmdir = nfs4_proc_remove,
9320 .readdir = nfs4_proc_readdir,
9321 .mknod = nfs4_proc_mknod,
9322 .statfs = nfs4_proc_statfs,
9323 .fsinfo = nfs4_proc_fsinfo,
9324 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009325 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009327 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009329 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009331 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009333 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009334 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009336 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009337 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009338 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009339 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009340 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009341 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009342 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009343 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009344 .create_server = nfs4_create_server,
9345 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346};
9347
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009348static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009349 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009350 .list = nfs4_xattr_list_nfs4_acl,
9351 .get = nfs4_xattr_get_nfs4_acl,
9352 .set = nfs4_xattr_set_nfs4_acl,
9353};
9354
9355const struct xattr_handler *nfs4_xattr_handlers[] = {
9356 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009357#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9358 &nfs4_xattr_nfs4_label_handler,
9359#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009360 NULL
9361};
9362
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363/*
9364 * Local variables:
9365 * c-basic-offset: 8
9366 * End:
9367 */