blob: 8b30cdf41848a71e7b1eaceee003eb4b31bfdff1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust26d36302016-09-22 13:39:05 -0400331static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
334{
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336
337 ops->test_and_free_expired(server, stateid, cred);
338}
339
340static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
343{
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
346}
347
348static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
351{
352 nfs4_stateid tmp;
353
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
356}
357
NeilBrown8478eaa2014-09-18 16:09:27 +1000358static long nfs4_update_delay(long *timeout)
359{
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
370}
371
Trond Myklebust65de8722008-12-23 15:21:44 -0500372static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
373{
374 int res = 0;
375
376 might_sleep();
377
NeilBrown8478eaa2014-09-18 16:09:27 +1000378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 return res;
383}
384
385/* This is the error handling routine for processes that are allowed
386 * to sleep.
387 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400388static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500390{
391 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500392 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400393 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500394 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 int ret = errorcode;
396
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400397 exception->delay = 0;
398 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400400
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
403
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 switch(errorcode) {
405 case 0:
406 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
415 }
416 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400417 if (inode) {
418 int err;
419
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
427 }
428 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500429 if (state == NULL)
430 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500436 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500447#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400458 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500459#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
464 */
465 ret = -EBUSY;
466 break;
467 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400471 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400472 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400473 exception->delay = 1;
474 return 0;
475
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
491 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500492 }
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500495wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400496 exception->recovering = 1;
497 return 0;
498}
499
500/* This is the error handling routine for processes that are allowed
501 * to sleep.
502 */
503int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504{
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
507
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
512 }
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
518 }
519 return ret;
520out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500524}
525
Trond Myklebust037fc982015-09-20 15:51:00 -0400526static int
527nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
529{
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
532
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
537 }
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
543 }
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
551}
552
553static int
554nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
556{
557 struct nfs4_exception exception = {
558 .state = state,
559 };
560
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
573}
574
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400575/*
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
578 */
579static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580{
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
586
587 return false;
588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Peng Taocb04ad22014-06-11 05:24:15 +0800627int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400631{
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 struct nfs4_slot *slot;
633
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
637
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
641
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust0a014a42016-09-22 13:38:51 -0400650 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400651 args->sa_slot = slot;
652 res->sr_slot = slot;
653
654out_start:
655 rpc_call_start(task);
656 return 0;
657
658out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
666}
Peng Taocb04ad22014-06-11 05:24:15 +0800667EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400668
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400669static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400670{
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
673
Chuck Lever3bd23842013-08-09 12:49:19 -0400674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
679
680 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400681}
682
683static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
685{
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400688 return 1;
689}
690
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691#if defined(CONFIG_NFS_V4_1)
692
Trond Myklebustd185a332010-06-16 09:52:25 -0400693static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400696 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500698 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400699
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500702
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
707
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500708 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
711 */
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
714
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500716 send_new_highest_used_slotid = false;
717 goto out_unlock;
718 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500719 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500720
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500723out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500724 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500726 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400727 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400730}
731
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400732static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400734{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500738 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500739 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400740
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500741 if (slot == NULL)
742 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400745 goto out;
746
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500747 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500748
Trond Myklebustac20d162012-12-15 15:36:07 -0500749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
752 }
753
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500755 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400756 switch (res->sr_status) {
757 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400758 /* If previous op on slot was interrupted and we reused
759 * the seq# and got a reply from the cache, then retry
760 */
761 if (task->tk_status == -EREMOTEIO && interrupted) {
762 ++slot->seq_nr;
763 goto retry_nowait;
764 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400765 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400766 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500767 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500768 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500769 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400770 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
771 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500772 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400773 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500774 case 1:
775 /*
776 * sr_status remains 1 if an RPC level error occurred.
777 * The server may or may not have processed the sequence
778 * operation..
779 * Mark the slot as having hosted an interrupted RPC call.
780 */
781 slot->interrupted = 1;
782 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400783 case -NFS4ERR_DELAY:
784 /* The server detected a resend of the RPC call and
785 * returned NFS4ERR_DELAY as per Section 2.10.6.2
786 * of RFC5661.
787 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500788 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400789 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500790 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500791 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400792 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500793 case -NFS4ERR_BADSLOT:
794 /*
795 * The slot id we used was probably retired. Try again
796 * using a different slot id.
797 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500798 goto retry_nowait;
799 case -NFS4ERR_SEQ_MISORDERED:
800 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500801 * Was the last operation on this sequence interrupted?
802 * If so, retry after bumping the sequence number.
803 */
804 if (interrupted) {
805 ++slot->seq_nr;
806 goto retry_nowait;
807 }
808 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500809 * Could this slot have been previously retired?
810 * If so, then the server may be expecting seq_nr = 1!
811 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500812 if (slot->seq_nr != 1) {
813 slot->seq_nr = 1;
814 goto retry_nowait;
815 }
816 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500817 case -NFS4ERR_SEQ_FALSE_RETRY:
818 ++slot->seq_nr;
819 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400820 default:
821 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400822 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400823 }
824out:
825 /* The session may be reset by one of the error handlers. */
826 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500827out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500828 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500829retry_nowait:
830 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400831 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500832 task->tk_status = 0;
833 ret = 0;
834 }
835 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400836out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400837 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 goto out;
839 rpc_delay(task, NFS4_POLL_RETRY_MAX);
840 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400841}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400842
843int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
844{
845 if (!nfs41_sequence_process(task, res))
846 return 0;
847 if (res->sr_slot != NULL)
848 nfs41_sequence_free_slot(res);
849 return 1;
850
851}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500852EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400853
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400854static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
855{
856 if (res->sr_slot == NULL)
857 return 1;
858 if (res->sr_slot->table->session != NULL)
859 return nfs41_sequence_process(task, res);
860 return nfs40_sequence_done(task, res);
861}
862
863static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
864{
865 if (res->sr_slot != NULL) {
866 if (res->sr_slot->table->session != NULL)
867 nfs41_sequence_free_slot(res);
868 else
869 nfs40_sequence_free_slot(res);
870 }
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400874{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500875 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400876 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400877 if (!res->sr_slot->table->session)
878 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400880}
Peng Tao2c4b1312014-06-11 05:24:16 +0800881EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400882
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000883int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400884 struct nfs4_sequence_args *args,
885 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400886 struct rpc_task *task)
887{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400888 struct nfs4_slot *slot;
889 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400890
891 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400892 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400893 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400894 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400896 tbl = &session->fc_slot_table;
897
Trond Myklebust69d206b2012-11-22 13:21:02 -0500898 task->tk_timeout = 0;
899
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400900 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400901 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400902 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500903 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500904 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400905 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400906 }
907
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500908 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500909 if (IS_ERR(slot)) {
910 /* If out of memory, try again in 1/4 second */
911 if (slot == ERR_PTR(-ENOMEM))
912 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400913 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400914 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400915 }
916 spin_unlock(&tbl->slot_tbl_lock);
917
Trond Myklebust0a014a42016-09-22 13:38:51 -0400918 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500919 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400920
Chuck Levere8d92382013-08-09 12:47:51 -0400921 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500922 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Benny Halevydfb4f3092010-09-24 09:17:01 -0400924 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500925 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400926 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400927 /*
928 * sr_status is only set in decode_sequence, and so will remain
929 * set to 1 if an rpc level failure occurs.
930 */
931 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400932 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400933out_success:
934 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400935 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400936out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400937 /* Privileged tasks are queued with top priority */
938 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400939 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
940 NULL, RPC_PRIORITY_PRIVILEGED);
941 else
942 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400943 spin_unlock(&tbl->slot_tbl_lock);
944 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000946EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947
Chuck Lever5a580e02013-08-09 12:48:09 -0400948static int nfs4_setup_sequence(const struct nfs_server *server,
949 struct nfs4_sequence_args *args,
950 struct nfs4_sequence_res *res,
951 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400952{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400953 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400954 int ret = 0;
955
Chuck Lever3bd23842013-08-09 12:49:19 -0400956 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800957 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
958 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400959
Chuck Levere8d92382013-08-09 12:47:51 -0400960 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400961 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400962 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400963
Trond Myklebust9d12b212012-01-17 22:04:25 -0500964 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400965
Andy Adamsonce5039c2009-04-01 09:22:13 -0400966 dprintk("<-- %s status=%d\n", __func__, ret);
967 return ret;
968}
969
Andy Adamsonce5039c2009-04-01 09:22:13 -0400970static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
971{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400972 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400973 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400974
Trond Myklebust035168ab2010-06-16 09:52:26 -0400975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
976
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400977 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978}
979
Andy Adamson69ab40c2009-04-01 09:22:19 -0400980static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
981{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400982 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983
Trond Myklebust14516c32010-07-31 14:29:06 -0400984 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985}
986
Trond Myklebust17280172012-03-11 13:11:00 -0400987static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400988 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400989 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400990};
991
Chuck Lever3bd23842013-08-09 12:49:19 -0400992#else /* !CONFIG_NFS_V4_1 */
993
Chuck Lever5a580e02013-08-09 12:48:09 -0400994static int nfs4_setup_sequence(const struct nfs_server *server,
995 struct nfs4_sequence_args *args,
996 struct nfs4_sequence_res *res,
997 struct rpc_task *task)
998{
Peng Taocb04ad22014-06-11 05:24:15 +0800999 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1000 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001001}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001002
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001003static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1004{
1005 return nfs40_sequence_done(task, res);
1006}
1007
1008static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1009{
1010 if (res->sr_slot != NULL)
1011 nfs40_sequence_free_slot(res);
1012}
1013
Peng Tao2c4b1312014-06-11 05:24:16 +08001014int nfs4_sequence_done(struct rpc_task *task,
1015 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001016{
Chuck Lever3bd23842013-08-09 12:49:19 -04001017 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001018}
Peng Tao2c4b1312014-06-11 05:24:16 +08001019EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001020
1021#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001022
Chuck Lever9915ea72013-08-09 12:48:27 -04001023static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1024{
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_setup_sequence(data->seq_server,
1027 data->seq_args, data->seq_res, task);
1028}
1029
1030static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1031{
1032 struct nfs4_call_sync_data *data = calldata;
1033 nfs4_sequence_done(task, data->seq_res);
1034}
1035
1036static const struct rpc_call_ops nfs40_call_sync_ops = {
1037 .rpc_call_prepare = nfs40_call_sync_prepare,
1038 .rpc_call_done = nfs40_call_sync_done,
1039};
1040
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001041static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001042 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001044 struct nfs4_sequence_args *args,
1045 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001046{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 int ret;
1048 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001049 struct nfs_client *clp = server->nfs_client;
1050 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001051 .seq_server = server,
1052 .seq_args = args,
1053 .seq_res = res,
1054 };
1055 struct rpc_task_setup task_setup = {
1056 .rpc_client = clnt,
1057 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001058 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001059 .callback_data = &data
1060 };
1061
1062 task = rpc_run_task(&task_setup);
1063 if (IS_ERR(task))
1064 ret = PTR_ERR(task);
1065 else {
1066 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001067 rpc_put_task(task);
1068 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001069 return ret;
1070}
1071
Bryan Schumaker7c513052011-03-24 17:12:24 +00001072int nfs4_call_sync(struct rpc_clnt *clnt,
1073 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001074 struct rpc_message *msg,
1075 struct nfs4_sequence_args *args,
1076 struct nfs4_sequence_res *res,
1077 int cache_reply)
1078{
Chuck Levera9c92d62013-08-09 12:48:18 -04001079 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001080 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1084{
1085 struct nfs_inode *nfsi = NFS_I(dir);
1086
1087 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001088 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001089 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001091 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001092 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001093 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 spin_unlock(&dir->i_lock);
1095}
1096
1097struct nfs4_opendata {
1098 struct kref kref;
1099 struct nfs_openargs o_arg;
1100 struct nfs_openres o_res;
1101 struct nfs_open_confirmargs c_arg;
1102 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001103 struct nfs4_string owner_name;
1104 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001105 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001107 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001109 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct nfs4_state_owner *owner;
1111 struct nfs4_state *state;
1112 struct iattr attrs;
1113 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001114 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001115 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001116 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int cancelled;
1119};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001121static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1122 int err, struct nfs4_exception *exception)
1123{
1124 if (err != -EINVAL)
1125 return false;
1126 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1127 return false;
1128 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1129 exception->retry = 1;
1130 return true;
1131}
1132
Trond Myklebust6ae37332015-01-30 14:21:14 -05001133static u32
1134nfs4_map_atomic_open_share(struct nfs_server *server,
1135 fmode_t fmode, int openflags)
1136{
1137 u32 res = 0;
1138
1139 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1140 case FMODE_READ:
1141 res = NFS4_SHARE_ACCESS_READ;
1142 break;
1143 case FMODE_WRITE:
1144 res = NFS4_SHARE_ACCESS_WRITE;
1145 break;
1146 case FMODE_READ|FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_BOTH;
1148 }
1149 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1150 goto out;
1151 /* Want no delegation if we're using O_DIRECT */
1152 if (openflags & O_DIRECT)
1153 res |= NFS4_SHARE_WANT_NO_DELEG;
1154out:
1155 return res;
1156}
1157
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001158static enum open_claim_type4
1159nfs4_map_atomic_open_claim(struct nfs_server *server,
1160 enum open_claim_type4 claim)
1161{
1162 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1163 return claim;
1164 switch (claim) {
1165 default:
1166 return claim;
1167 case NFS4_OPEN_CLAIM_FH:
1168 return NFS4_OPEN_CLAIM_NULL;
1169 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1170 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1171 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1172 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1173 }
1174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177{
1178 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001179 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 p->o_res.seqid = p->o_arg.seqid;
1181 p->c_res.seqid = p->c_arg.seqid;
1182 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001183 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001184 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001185 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186}
1187
Al Viro82a2c1b2011-06-22 18:30:55 -04001188static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001190 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001191 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001192 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194{
Al Viro82a2c1b2011-06-22 18:30:55 -04001195 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001196 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001198 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 struct nfs4_opendata *p;
1200
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 if (p == NULL)
1203 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001204
1205 p->f_label = nfs4_label_alloc(server, gfp_mask);
1206 if (IS_ERR(p->f_label))
1207 goto err_free_p;
1208
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 p->a_label = nfs4_label_alloc(server, gfp_mask);
1210 if (IS_ERR(p->a_label))
1211 goto err_free_f;
1212
Trond Myklebust63f5f792015-01-23 19:19:25 -05001213 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1214 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001215 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001216 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001217 nfs_sb_active(dentry->d_sb);
1218 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001219 p->dir = parent;
1220 p->owner = sp;
1221 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222 p->o_arg.open_flags = flags;
1223 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001224 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1225 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1227 * will return permission denied for all bits until close */
1228 if (!(flags & O_EXCL)) {
1229 /* ask server to check for all possible rights as results
1230 * are cached */
1231 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1232 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001233 }
David Howells7539bba2006-08-22 20:06:09 -04001234 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001235 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1236 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001237 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001238 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001239 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001240 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001241 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001242 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1243 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001244 case NFS4_OPEN_CLAIM_NULL:
1245 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1246 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1247 p->o_arg.fh = NFS_FH(dir);
1248 break;
1249 case NFS4_OPEN_CLAIM_PREVIOUS:
1250 case NFS4_OPEN_CLAIM_FH:
1251 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1252 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001253 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001254 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001255 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001256 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001257
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 p->o_arg.u.attrs = &p->attrs;
1259 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001260
1261 verf[0] = jiffies;
1262 verf[1] = current->pid;
1263 memcpy(p->o_arg.u.verifier.data, verf,
1264 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001266 p->c_arg.fh = &p->o_res.fh;
1267 p->c_arg.stateid = &p->o_res.stateid;
1268 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001269 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001272
1273err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001274 nfs4_label_free(p->a_label);
1275err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001276 nfs4_label_free(p->f_label);
1277err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 kfree(p);
1279err:
1280 dput(parent);
1281 return NULL;
1282}
1283
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001286 struct nfs4_opendata *p = container_of(kref,
1287 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001288 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289
1290 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001291 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 if (p->state != NULL)
1293 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001295
Kinglong Meea49c2692015-07-27 15:31:38 +08001296 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001297 nfs4_label_free(p->f_label);
1298
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001300 dput(p->dentry);
1301 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001302 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001303 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 kfree(p);
1305}
1306
1307static void nfs4_opendata_put(struct nfs4_opendata *p)
1308{
1309 if (p != NULL)
1310 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311}
1312
Trond Myklebust06f814a2006-01-03 09:55:07 +01001313static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1314{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001315 int ret;
1316
Trond Myklebust06f814a2006-01-03 09:55:07 +01001317 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318 return ret;
1319}
1320
Trond Myklebust24311f82015-09-20 10:50:17 -04001321static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1322 fmode_t fmode)
1323{
1324 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1325 case FMODE_READ|FMODE_WRITE:
1326 return state->n_rdwr != 0;
1327 case FMODE_WRITE:
1328 return state->n_wronly != 0;
1329 case FMODE_READ:
1330 return state->n_rdonly != 0;
1331 }
1332 WARN_ON_ONCE(1);
1333 return false;
1334}
1335
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001336static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001337{
1338 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339
Trond Myklebust536e43d2012-01-17 22:04:26 -05001340 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341 goto out;
1342 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001344 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1345 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 break;
1347 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001348 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1349 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001350 break;
1351 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001352 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1353 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001356 return ret;
1357}
1358
Trond Myklebust2a606182015-08-19 22:30:00 -05001359static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1360 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001362 if (delegation == NULL)
1363 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001365 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001366 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1367 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001368 switch (claim) {
1369 case NFS4_OPEN_CLAIM_NULL:
1370 case NFS4_OPEN_CLAIM_FH:
1371 break;
1372 case NFS4_OPEN_CLAIM_PREVIOUS:
1373 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1374 break;
1375 default:
1376 return 0;
1377 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001378 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379 return 1;
1380}
1381
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001383{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001384 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001385 case FMODE_WRITE:
1386 state->n_wronly++;
1387 break;
1388 case FMODE_READ:
1389 state->n_rdonly++;
1390 break;
1391 case FMODE_READ|FMODE_WRITE:
1392 state->n_rdwr++;
1393 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001394 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001395}
1396
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001397#ifdef CONFIG_NFS_V4_1
1398static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1399{
1400 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1401 return true;
1402 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1403 return true;
1404 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1405 return true;
1406 return false;
1407}
1408#endif /* CONFIG_NFS_V4_1 */
1409
Trond Myklebust4f14c192014-02-12 19:15:06 -05001410static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001411{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001412 struct nfs_client *clp = state->owner->so_server->nfs_client;
1413 bool need_recover = false;
1414
1415 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1416 need_recover = true;
1417 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1418 need_recover = true;
1419 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1420 need_recover = true;
1421 if (need_recover)
1422 nfs4_state_mark_reclaim_nograce(clp, state);
1423}
1424
Trond Myklebuste999e802014-02-10 18:20:47 -05001425static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001426 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001427{
1428 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1429 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001430 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001431 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001432 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001434 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001435 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1436 return true;
1437 return false;
1438}
1439
Trond Myklebustf95549c2015-01-23 18:06:09 -05001440static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1441{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001442 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1443 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444 if (state->n_wronly)
1445 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1446 if (state->n_rdonly)
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1448 if (state->n_rdwr)
1449 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001450 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451}
1452
Trond Myklebust226056c2014-02-11 10:41:07 -05001453static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001454 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001455 nfs4_stateid *stateid, fmode_t fmode)
1456{
1457 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1458 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1459 case FMODE_WRITE:
1460 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1461 break;
1462 case FMODE_READ:
1463 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1464 break;
1465 case 0:
1466 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1468 clear_bit(NFS_OPEN_STATE, &state->flags);
1469 }
1470 if (stateid == NULL)
1471 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001472 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001473 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1474 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1475 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001476 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001477 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001478 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001479 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001480 nfs4_stateid_copy(&state->stateid, stateid);
1481 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001482}
1483
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001484static void nfs_clear_open_stateid(struct nfs4_state *state,
1485 nfs4_stateid *arg_stateid,
1486 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001487{
1488 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001489 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001490 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001491 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1492 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001493}
1494
Trond Myklebust1393d962016-09-22 13:39:13 -04001495static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1496 const nfs4_stateid *stateid, fmode_t fmode,
1497 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001498{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001499 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001500 case FMODE_READ:
1501 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1502 break;
1503 case FMODE_WRITE:
1504 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1505 break;
1506 case FMODE_READ|FMODE_WRITE:
1507 set_bit(NFS_O_RDWR_STATE, &state->flags);
1508 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001509 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001510 return;
1511 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1512 nfs4_stateid_copy(&state->stateid, stateid);
1513 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001514}
1515
Trond Myklebust1393d962016-09-22 13:39:13 -04001516static void __update_open_stateid(struct nfs4_state *state,
1517 const nfs4_stateid *open_stateid,
1518 const nfs4_stateid *deleg_stateid,
1519 fmode_t fmode,
1520 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001522 /*
1523 * Protect the call to nfs4_state_set_mode_locked and
1524 * serialise the stateid update
1525 */
Andrew Elble361cad32015-12-02 09:20:57 -05001526 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001527 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001528 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001529 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001530 set_bit(NFS_DELEGATED_STATE, &state->flags);
1531 }
1532 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001533 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001534 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001535 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001536 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Trond Myklebust1393d962016-09-22 13:39:13 -04001539static int update_open_stateid(struct nfs4_state *state,
1540 const nfs4_stateid *open_stateid,
1541 const nfs4_stateid *delegation,
1542 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001543{
Trond Myklebust1393d962016-09-22 13:39:13 -04001544 struct nfs_server *server = NFS_SERVER(state->inode);
1545 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001546 struct nfs_inode *nfsi = NFS_I(state->inode);
1547 struct nfs_delegation *deleg_cur;
Trond Myklebust1393d962016-09-22 13:39:13 -04001548 nfs4_stateid freeme = {0};
Trond Myklebust34310432008-12-23 15:21:38 -05001549 int ret = 0;
1550
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001551 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001552
1553 rcu_read_lock();
1554 deleg_cur = rcu_dereference(nfsi->delegation);
1555 if (deleg_cur == NULL)
1556 goto no_delegation;
1557
1558 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001559 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001560 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001562 goto no_delegation_unlock;
1563
1564 if (delegation == NULL)
1565 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001566 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001567 goto no_delegation_unlock;
1568
Trond Myklebustb7391f42008-12-23 15:21:52 -05001569 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001570 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1571 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001572 ret = 1;
1573no_delegation_unlock:
1574 spin_unlock(&deleg_cur->lock);
1575no_delegation:
1576 rcu_read_unlock();
1577
1578 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001579 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001580 ret = 1;
1581 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001582 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001583 nfs4_schedule_state_manager(clp);
1584 if (freeme.type != 0)
1585 nfs4_test_and_free_stateid(server, &freeme,
1586 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001587
1588 return ret;
1589}
1590
Trond Myklebust39071e62015-01-24 15:07:56 -05001591static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1592 const nfs4_stateid *stateid)
1593{
1594 struct nfs4_state *state = lsp->ls_state;
1595 bool ret = false;
1596
1597 spin_lock(&state->state_lock);
1598 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1599 goto out_noupdate;
1600 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1601 goto out_noupdate;
1602 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1603 ret = true;
1604out_noupdate:
1605 spin_unlock(&state->state_lock);
1606 return ret;
1607}
Trond Myklebust34310432008-12-23 15:21:38 -05001608
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001610{
1611 struct nfs_delegation *delegation;
1612
1613 rcu_read_lock();
1614 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001615 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 rcu_read_unlock();
1617 return;
1618 }
1619 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001620 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621}
1622
Trond Myklebust6ee41262007-07-08 14:11:36 -04001623static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624{
1625 struct nfs4_state *state = opendata->state;
1626 struct nfs_inode *nfsi = NFS_I(state->inode);
1627 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001628 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001630 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631 nfs4_stateid stateid;
1632 int ret = -EAGAIN;
1633
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001635 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001636 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001637 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001638 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001639 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001640 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001641 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001642 rcu_read_lock();
1643 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001644 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001645 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001646 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001647 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001648 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001649 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001651 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001652 if (!opendata->is_recover) {
1653 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1654 if (ret != 0)
1655 goto out;
1656 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001657 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001658
1659 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001660 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001661 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001662 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001663out:
1664 return ERR_PTR(ret);
1665out_return_state:
1666 atomic_inc(&state->count);
1667 return state;
1668}
1669
Andy Adamsone23008e2012-10-02 21:07:32 -04001670static void
1671nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1672{
1673 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1674 struct nfs_delegation *delegation;
1675 int delegation_flags = 0;
1676
1677 rcu_read_lock();
1678 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1679 if (delegation)
1680 delegation_flags = delegation->flags;
1681 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001682 switch (data->o_arg.claim) {
1683 default:
1684 break;
1685 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1686 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001687 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1688 "returning a delegation for "
1689 "OPEN(CLAIM_DELEGATE_CUR)\n",
1690 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001691 return;
1692 }
1693 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001694 nfs_inode_set_delegation(state->inode,
1695 data->owner->so_cred,
1696 &data->o_res);
1697 else
1698 nfs_inode_reclaim_delegation(state->inode,
1699 data->owner->so_cred,
1700 &data->o_res);
1701}
1702
1703/*
1704 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1705 * and update the nfs4_state.
1706 */
1707static struct nfs4_state *
1708_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1709{
1710 struct inode *inode = data->state->inode;
1711 struct nfs4_state *state = data->state;
1712 int ret;
1713
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001714 if (!data->rpc_done) {
1715 if (data->rpc_status) {
1716 ret = data->rpc_status;
1717 goto err;
1718 }
1719 /* cached opens have already been processed */
1720 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001721 }
1722
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 ret = nfs_refresh_inode(inode, &data->f_attr);
1724 if (ret)
1725 goto err;
1726
1727 if (data->o_res.delegation_type != 0)
1728 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001729update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001730 update_open_stateid(state, &data->o_res.stateid, NULL,
1731 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001732 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001733
1734 return state;
1735err:
1736 return ERR_PTR(ret);
1737
1738}
1739
1740static struct nfs4_state *
1741_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742{
1743 struct inode *inode;
1744 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001745 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001746
Trond Myklebustaac00a82007-07-05 19:02:21 -04001747 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001748 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001749 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750 goto out;
1751 }
1752
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001753 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001755 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001756 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001757 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001758 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001759 goto err;
1760 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 state = nfs4_get_open_state(inode, data->owner);
1762 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001763 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001764 if (data->o_res.delegation_type != 0)
1765 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001766 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001767 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001770 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001772err_put_inode:
1773 iput(inode);
1774err:
1775 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776}
1777
Andy Adamsone23008e2012-10-02 21:07:32 -04001778static struct nfs4_state *
1779nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1780{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001781 struct nfs4_state *ret;
1782
Andy Adamsone23008e2012-10-02 21:07:32 -04001783 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001784 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1785 else
1786 ret = _nfs4_opendata_to_nfs4_state(data);
1787 nfs4_sequence_free_slot(&data->o_res.seq_res);
1788 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001789}
1790
Trond Myklebust864472e2006-01-03 09:55:15 +01001791static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1792{
1793 struct nfs_inode *nfsi = NFS_I(state->inode);
1794 struct nfs_open_context *ctx;
1795
1796 spin_lock(&state->inode->i_lock);
1797 list_for_each_entry(ctx, &nfsi->open_files, list) {
1798 if (ctx->state != state)
1799 continue;
1800 get_nfs_open_context(ctx);
1801 spin_unlock(&state->inode->i_lock);
1802 return ctx;
1803 }
1804 spin_unlock(&state->inode->i_lock);
1805 return ERR_PTR(-ENOENT);
1806}
1807
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001808static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1809 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001810{
1811 struct nfs4_opendata *opendata;
1812
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001813 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001814 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001815 if (opendata == NULL)
1816 return ERR_PTR(-ENOMEM);
1817 opendata->state = state;
1818 atomic_inc(&state->count);
1819 return opendata;
1820}
1821
Trond Myklebust24311f82015-09-20 10:50:17 -04001822static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1823 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001824{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001825 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001826 int ret;
1827
Trond Myklebust24311f82015-09-20 10:50:17 -04001828 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001829 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001830 opendata->o_arg.open_flags = 0;
1831 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001832 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1833 NFS_SB(opendata->dentry->d_sb),
1834 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001835 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1836 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1837 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001838 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001839 if (ret != 0)
1840 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001841 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001842 if (IS_ERR(newstate))
1843 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001844 if (newstate != opendata->state)
1845 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001846 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001847 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001848}
1849
1850static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1851{
Trond Myklebust864472e2006-01-03 09:55:15 +01001852 int ret;
1853
Trond Myklebust4f14c192014-02-12 19:15:06 -05001854 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1855 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1856 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1857 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001858 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001859 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001860 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001862 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1863 if (ret != 0)
1864 return ret;
1865 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1866 if (ret != 0)
1867 return ret;
1868 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1869 if (ret != 0)
1870 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001871 /*
1872 * We may have performed cached opens for all three recoveries.
1873 * Check if we need to update the current stateid.
1874 */
1875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001876 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001877 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001879 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001880 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001881 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001882 return 0;
1883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/*
1886 * OPEN_RECLAIM:
1887 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001889static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001891 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001892 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001893 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 int status;
1895
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001896 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1897 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001898 if (IS_ERR(opendata))
1899 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001900 rcu_read_lock();
1901 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001902 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001903 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001904 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001905 opendata->o_arg.u.delegation_type = delegation_type;
1906 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001907 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return status;
1909}
1910
Trond Myklebust539cd032007-06-05 11:46:42 -04001911static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 struct nfs_server *server = NFS_SERVER(state->inode);
1914 struct nfs4_exception exception = { };
1915 int err;
1916 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001917 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001918 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001919 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1920 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001921 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001922 break;
1923 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 } while (exception.retry);
1925 return err;
1926}
1927
Trond Myklebust864472e2006-01-03 09:55:15 +01001928static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1929{
1930 struct nfs_open_context *ctx;
1931 int ret;
1932
1933 ctx = nfs4_state_find_open_context(state);
1934 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001935 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001936 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001937 put_nfs_open_context(ctx);
1938 return ret;
1939}
1940
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001941static 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 -07001942{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001943 switch (err) {
1944 default:
1945 printk(KERN_ERR "NFS: %s: unhandled error "
1946 "%d.\n", __func__, err);
1947 case 0:
1948 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001949 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001950 case -ESTALE:
1951 break;
1952 case -NFS4ERR_BADSESSION:
1953 case -NFS4ERR_BADSLOT:
1954 case -NFS4ERR_BAD_HIGH_SLOT:
1955 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1956 case -NFS4ERR_DEADSESSION:
1957 set_bit(NFS_DELEGATED_STATE, &state->flags);
1958 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1959 return -EAGAIN;
1960 case -NFS4ERR_STALE_CLIENTID:
1961 case -NFS4ERR_STALE_STATEID:
1962 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001963 /* Don't recall a delegation if it was lost */
1964 nfs4_schedule_lease_recovery(server->nfs_client);
1965 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001966 case -NFS4ERR_MOVED:
1967 nfs4_schedule_migration_recovery(server);
1968 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001969 case -NFS4ERR_LEASE_MOVED:
1970 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1971 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001972 case -NFS4ERR_DELEG_REVOKED:
1973 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001974 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001975 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001976 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001977 nfs_inode_find_state_and_recover(state->inode,
1978 stateid);
1979 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001980 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001981 case -NFS4ERR_DELAY:
1982 case -NFS4ERR_GRACE:
1983 set_bit(NFS_DELEGATED_STATE, &state->flags);
1984 ssleep(1);
1985 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001986 case -ENOMEM:
1987 case -NFS4ERR_DENIED:
1988 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1989 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return err;
1992}
1993
Trond Myklebust24311f82015-09-20 10:50:17 -04001994int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1995 struct nfs4_state *state, const nfs4_stateid *stateid,
1996 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001997{
1998 struct nfs_server *server = NFS_SERVER(state->inode);
1999 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002000 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002001
2002 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2003 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2004 if (IS_ERR(opendata))
2005 return PTR_ERR(opendata);
2006 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002007 write_seqlock(&state->seqlock);
2008 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2009 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002010 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2011 switch (type & (FMODE_READ|FMODE_WRITE)) {
2012 case FMODE_READ|FMODE_WRITE:
2013 case FMODE_WRITE:
2014 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2015 if (err)
2016 break;
2017 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2018 if (err)
2019 break;
2020 case FMODE_READ:
2021 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2022 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002023 nfs4_opendata_put(opendata);
2024 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2025}
2026
Chuck Leverbe05c862013-08-09 12:49:47 -04002027static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2028{
2029 struct nfs4_opendata *data = calldata;
2030
Peng Taocb04ad22014-06-11 05:24:15 +08002031 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2032 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002033}
2034
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2036{
2037 struct nfs4_opendata *data = calldata;
2038
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002039 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002040
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002042 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002043 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002044 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002045 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002046 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002047 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002048}
2049
2050static void nfs4_open_confirm_release(void *calldata)
2051{
2052 struct nfs4_opendata *data = calldata;
2053 struct nfs4_state *state = NULL;
2054
2055 /* If this request hasn't been cancelled, do nothing */
2056 if (data->cancelled == 0)
2057 goto out_free;
2058 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002059 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002061 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002062 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002063 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002064out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002065 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002066}
2067
2068static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002069 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002070 .rpc_call_done = nfs4_open_confirm_done,
2071 .rpc_release = nfs4_open_confirm_release,
2072};
2073
2074/*
2075 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2076 */
2077static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2078{
David Howells2b0143b2015-03-17 22:25:59 +00002079 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002080 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002081 struct rpc_message msg = {
2082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2083 .rpc_argp = &data->c_arg,
2084 .rpc_resp = &data->c_res,
2085 .rpc_cred = data->owner->so_cred,
2086 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002087 struct rpc_task_setup task_setup_data = {
2088 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002089 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002090 .callback_ops = &nfs4_open_confirm_ops,
2091 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002092 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002093 .flags = RPC_TASK_ASYNC,
2094 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int status;
2096
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002097 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002098 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002099 data->rpc_done = 0;
2100 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002101 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002102 if (data->is_recover)
2103 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002104 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002105 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002106 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002107 status = nfs4_wait_for_completion_rpc_task(task);
2108 if (status != 0) {
2109 data->cancelled = 1;
2110 smp_wmb();
2111 } else
2112 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002113 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return status;
2115}
2116
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002119 struct nfs4_opendata *data = calldata;
2120 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002121 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002122 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002125 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002126 /*
2127 * Check if we still need to send an OPEN call, or if we can use
2128 * a delegation instead.
2129 */
2130 if (data->state != NULL) {
2131 struct nfs_delegation *delegation;
2132
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002133 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002134 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002135 rcu_read_lock();
2136 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002137 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002138 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002139 rcu_read_unlock();
2140 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002141 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002142 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002143 switch (claim) {
2144 default:
2145 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002146 case NFS4_OPEN_CLAIM_PREVIOUS:
2147 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2148 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002149 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002150 case NFS4_OPEN_CLAIM_FH:
2151 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002152 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2153 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002154 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002155 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002156 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002157 &data->o_res.seq_res,
2158 task) != 0)
2159 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002160
2161 /* Set the create mode (note dependency on the session type) */
2162 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2163 if (data->o_arg.open_flags & O_EXCL) {
2164 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2165 if (nfs4_has_persistent_session(clp))
2166 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2167 else if (clp->cl_mvops->minor_version > 0)
2168 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2169 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002170 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002171unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002172 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002173 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002174out_no_action:
2175 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002176out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002177 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002178}
2179
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002180static void nfs4_open_done(struct rpc_task *task, void *calldata)
2181{
2182 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002184 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002185
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002186 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002187 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002188
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002190 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2191 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002192 case S_IFREG:
2193 break;
2194 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002196 break;
2197 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199 break;
2200 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002202 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002204 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002205 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2206 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002207 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002208 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002209}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002211static void nfs4_open_release(void *calldata)
2212{
2213 struct nfs4_opendata *data = calldata;
2214 struct nfs4_state *state = NULL;
2215
2216 /* If this request hasn't been cancelled, do nothing */
2217 if (data->cancelled == 0)
2218 goto out_free;
2219 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002220 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221 goto out_free;
2222 /* In case we need an open_confirm, no cleanup! */
2223 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2224 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002225 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002226 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002227 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002229 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230}
2231
2232static const struct rpc_call_ops nfs4_open_ops = {
2233 .rpc_call_prepare = nfs4_open_prepare,
2234 .rpc_call_done = nfs4_open_done,
2235 .rpc_release = nfs4_open_release,
2236};
2237
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002238static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239{
David Howells2b0143b2015-03-17 22:25:59 +00002240 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002241 struct nfs_server *server = NFS_SERVER(dir);
2242 struct nfs_openargs *o_arg = &data->o_arg;
2243 struct nfs_openres *o_res = &data->o_res;
2244 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002245 struct rpc_message msg = {
2246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2247 .rpc_argp = o_arg,
2248 .rpc_resp = o_res,
2249 .rpc_cred = data->owner->so_cred,
2250 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 struct rpc_task_setup task_setup_data = {
2252 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002253 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002254 .callback_ops = &nfs4_open_ops,
2255 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002256 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002257 .flags = RPC_TASK_ASYNC,
2258 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002259 int status;
2260
Chuck Levera9c92d62013-08-09 12:48:18 -04002261 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002262 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002263 data->rpc_done = 0;
2264 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002265 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002266 data->is_recover = 0;
2267 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002268 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002269 data->is_recover = 1;
2270 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002271 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002272 if (IS_ERR(task))
2273 return PTR_ERR(task);
2274 status = nfs4_wait_for_completion_rpc_task(task);
2275 if (status != 0) {
2276 data->cancelled = 1;
2277 smp_wmb();
2278 } else
2279 status = data->rpc_status;
2280 rpc_put_task(task);
2281
2282 return status;
2283}
2284
2285static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2286{
David Howells2b0143b2015-03-17 22:25:59 +00002287 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002288 struct nfs_openres *o_res = &data->o_res;
2289 int status;
2290
2291 status = nfs4_run_open_task(data, 1);
2292 if (status != 0 || !data->rpc_done)
2293 return status;
2294
Trond Myklebust6926afd12012-01-07 13:22:46 -05002295 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2296
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002297 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2298 status = _nfs4_proc_open_confirm(data);
2299 if (status != 0)
2300 return status;
2301 }
2302
2303 return status;
2304}
2305
Trond Myklebustf3792d62014-07-10 08:54:32 -04002306/*
2307 * Additional permission checks in order to distinguish between an
2308 * open for read, and an open for execute. This works around the
2309 * fact that NFSv4 OPEN treats read and execute permissions as being
2310 * the same.
2311 * Note that in the non-execute case, we want to turn off permission
2312 * checking if we just created a new file (POSIX open() semantics).
2313 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002314static int nfs4_opendata_access(struct rpc_cred *cred,
2315 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002316 struct nfs4_state *state, fmode_t fmode,
2317 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002318{
2319 struct nfs_access_entry cache;
2320 u32 mask;
2321
2322 /* access call failed or for some reason the server doesn't
2323 * support any access modes -- defer access call until later */
2324 if (opendata->o_res.access_supported == 0)
2325 return 0;
2326
2327 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002328 /*
2329 * Use openflags to check for exec, because fmode won't
2330 * always have FMODE_EXEC set when file open for exec.
2331 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002332 if (openflags & __FMODE_EXEC) {
2333 /* ONLY check for exec rights */
2334 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002335 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002336 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002337
2338 cache.cred = cred;
2339 cache.jiffies = jiffies;
2340 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2341 nfs_access_add_cache(state->inode, &cache);
2342
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002343 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002344 return 0;
2345
2346 /* even though OPEN succeeded, access is denied. Close the file */
2347 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002348 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002349}
2350
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002351/*
2352 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2353 */
2354static int _nfs4_proc_open(struct nfs4_opendata *data)
2355{
David Howells2b0143b2015-03-17 22:25:59 +00002356 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002357 struct nfs_server *server = NFS_SERVER(dir);
2358 struct nfs_openargs *o_arg = &data->o_arg;
2359 struct nfs_openres *o_res = &data->o_res;
2360 int status;
2361
2362 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002363 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002364 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002365 if (status != 0) {
2366 if (status == -NFS4ERR_BADNAME &&
2367 !(o_arg->open_flags & O_CREAT))
2368 return -ENOENT;
2369 return status;
2370 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371
Trond Myklebust6926afd12012-01-07 13:22:46 -05002372 nfs_fattr_map_and_free_names(server, &data->f_attr);
2373
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002374 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002375 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002376 if (o_arg->open_flags & O_EXCL)
2377 data->file_created = 1;
2378 else if (o_res->cinfo.before != o_res->cinfo.after)
2379 data->file_created = 1;
2380 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002381 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2382 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002384 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002386 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002389 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Andy Adamsond83217c2011-03-01 01:34:17 +00002393static int nfs4_recover_expired_lease(struct nfs_server *server)
2394{
2395 return nfs4_client_recover_expired_lease(server->nfs_client);
2396}
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398/*
2399 * OPEN_EXPIRED:
2400 * reclaim state on the server after a network partition.
2401 * Assumes caller holds the appropriate lock
2402 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002403static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002405 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002406 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002408 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002409 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002410 if (IS_ERR(opendata))
2411 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002412 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002413 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002414 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002415 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002419static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002420{
Trond Myklebust539cd032007-06-05 11:46:42 -04002421 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002422 struct nfs4_exception exception = { };
2423 int err;
2424
2425 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002426 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002427 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002428 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2429 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002430 switch (err) {
2431 default:
2432 goto out;
2433 case -NFS4ERR_GRACE:
2434 case -NFS4ERR_DELAY:
2435 nfs4_handle_exception(server, err, &exception);
2436 err = 0;
2437 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002438 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002439out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002440 return err;
2441}
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002446 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Trond Myklebust864472e2006-01-03 09:55:15 +01002448 ctx = nfs4_state_find_open_context(state);
2449 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002450 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002451 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002452 put_nfs_open_context(ctx);
2453 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
Trond Myklebust41020b62016-09-22 13:38:58 -04002456static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2457 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002458{
Trond Myklebust41020b62016-09-22 13:38:58 -04002459 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002460 write_seqlock(&state->seqlock);
2461 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2462 write_sequnlock(&state->seqlock);
2463 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2464}
2465
2466static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2467{
2468 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002469 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002470}
2471
2472static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2473{
2474 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2475 nfs40_clear_delegation_stateid(state);
2476 return nfs4_open_expired(sp, state);
2477}
2478
Trond Myklebust45870d62016-09-22 13:38:59 -04002479static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2480 nfs4_stateid *stateid,
2481 struct rpc_cred *cred)
2482{
2483 return -NFS4ERR_BAD_STATEID;
2484}
2485
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002486#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002487static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2488 nfs4_stateid *stateid,
2489 struct rpc_cred *cred)
2490{
2491 int status;
2492
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002493 switch (stateid->type) {
2494 default:
2495 break;
2496 case NFS4_INVALID_STATEID_TYPE:
2497 case NFS4_SPECIAL_STATEID_TYPE:
2498 return -NFS4ERR_BAD_STATEID;
2499 case NFS4_REVOKED_STATEID_TYPE:
2500 goto out_free;
2501 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002502
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002503 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002504 switch (status) {
2505 case -NFS4ERR_EXPIRED:
2506 case -NFS4ERR_ADMIN_REVOKED:
2507 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002508 break;
2509 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002510 return status;
2511 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002512out_free:
2513 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002514 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002516}
2517
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002518static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002519{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002520 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002521 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002522 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002523 struct rpc_cred *cred;
2524 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002525
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002526 /* Get the delegation credential for use by test/free_stateid */
2527 rcu_read_lock();
2528 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002530 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002531 return;
2532 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002533
2534 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002535 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2536 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002537 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002538 return;
2539 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002541 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2542 rcu_read_unlock();
2543 return;
2544 }
2545
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002546 cred = get_rpccred(delegation->cred);
2547 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002548 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002549 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002550 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002551 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002552
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002553 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002554}
2555
2556/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002557 * nfs41_check_expired_locks - possibly free a lock stateid
2558 *
2559 * @state: NFSv4 state for an inode
2560 *
2561 * Returns NFS_OK if recovery for this stateid is now finished.
2562 * Otherwise a negative NFS4ERR value is returned.
2563 */
2564static int nfs41_check_expired_locks(struct nfs4_state *state)
2565{
2566 int status, ret = NFS_OK;
2567 struct nfs4_lock_state *lsp;
2568 struct nfs_server *server = NFS_SERVER(state->inode);
2569
2570 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2571 goto out;
2572 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2573 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2574 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2575
2576 status = nfs41_test_and_free_expired_stateid(server,
2577 &lsp->ls_stateid,
2578 cred);
2579 trace_nfs4_test_lock_stateid(state, lsp, status);
2580 if (status == -NFS4ERR_EXPIRED ||
2581 status == -NFS4ERR_BAD_STATEID) {
2582 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002583 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002584 if (!recover_lost_locks)
2585 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2586 } else if (status != NFS_OK) {
2587 ret = status;
2588 break;
2589 }
2590 }
2591 };
2592out:
2593 return ret;
2594}
2595
2596/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002597 * nfs41_check_open_stateid - possibly free an open stateid
2598 *
2599 * @state: NFSv4 state for an inode
2600 *
2601 * Returns NFS_OK if recovery for this stateid is now finished.
2602 * Otherwise a negative NFS4ERR value is returned.
2603 */
2604static int nfs41_check_open_stateid(struct nfs4_state *state)
2605{
2606 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002607 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002608 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002609 int status;
2610
Trond Myklebustb134fc42016-09-22 13:39:16 -04002611 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002612 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2613 if (nfs4_have_delegation(state->inode, state->state))
2614 return NFS_OK;
2615 return -NFS4ERR_OPENMODE;
2616 }
Trond Myklebustb134fc42016-09-22 13:39:16 -04002617 return -NFS4ERR_BAD_STATEID;
2618 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002619 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002620 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002621 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002622 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2623 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2624 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002625 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002626 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002627 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002628 if (status != NFS_OK)
2629 return status;
2630 if (nfs_open_stateid_recover_openmode(state))
2631 return -NFS4ERR_OPENMODE;
2632 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002633}
2634
2635static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2636{
Chuck Levereb64cf92012-07-11 16:30:05 -04002637 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002638
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002639 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002640 status = nfs41_check_expired_locks(state);
2641 if (status != NFS_OK)
2642 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002643 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002644 if (status != NFS_OK)
2645 status = nfs4_open_expired(sp, state);
2646 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002647}
2648#endif
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002651 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2652 * fields corresponding to attributes that were used to store the verifier.
2653 * Make sure we clobber those fields in the later setattr call
2654 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002655static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2656 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002657{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002658 const u32 *attrset = opendata->o_res.attrset;
2659
2660 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002661 !(sattr->ia_valid & ATTR_ATIME_SET))
2662 sattr->ia_valid |= ATTR_ATIME;
2663
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002664 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002665 !(sattr->ia_valid & ATTR_MTIME_SET))
2666 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002667
2668 /* Except MODE, it seems harmless of setting twice. */
2669 if ((attrset[1] & FATTR4_WORD1_MODE))
2670 sattr->ia_valid &= ~ATTR_MODE;
2671
2672 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2673 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002674}
2675
Trond Myklebustc21443c2013-02-07 14:26:21 -05002676static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2677 fmode_t fmode,
2678 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002679 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002680{
2681 struct nfs4_state_owner *sp = opendata->owner;
2682 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002683 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002684 struct nfs4_state *state;
2685 unsigned int seq;
2686 int ret;
2687
2688 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2689
2690 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002691 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002692 goto out;
2693
2694 state = nfs4_opendata_to_nfs4_state(opendata);
2695 ret = PTR_ERR(state);
2696 if (IS_ERR(state))
2697 goto out;
2698 if (server->caps & NFS_CAP_POSIX_LOCK)
2699 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002700 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2701 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002702
Trond Myklebust275bb302013-05-29 13:11:28 -04002703 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002704 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002705 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002706 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002707 alias = d_exact_alias(dentry, state->inode);
2708 if (!alias)
2709 alias = d_splice_alias(igrab(state->inode), dentry);
2710 /* d_splice_alias() can't fail here - it's a non-directory */
2711 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002712 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002713 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002714 }
2715 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002716 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002717 }
2718
Trond Myklebustc21443c2013-02-07 14:26:21 -05002719 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2720 if (ret != 0)
2721 goto out;
2722
Trond Myklebust3efb9722013-05-29 13:17:04 -04002723 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002724 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002725 nfs_inode_attach_open_context(ctx);
2726 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2727 nfs4_schedule_stateid_recovery(server, state);
2728 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002729out:
2730 return ret;
2731}
2732
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002733/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002734 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Andy Adamson82be4172012-05-23 05:02:35 -04002736static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002737 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002738 int flags,
2739 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002740 struct nfs4_label *label,
2741 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
2743 struct nfs4_state_owner *sp;
2744 struct nfs4_state *state = NULL;
2745 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002746 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002747 struct dentry *dentry = ctx->dentry;
2748 struct rpc_cred *cred = ctx->cred;
2749 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2750 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002751 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002752 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002753 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002757 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2758 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2760 goto out_err;
2761 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002762 status = nfs4_recover_expired_lease(server);
2763 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002764 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002765 if (d_really_is_positive(dentry))
2766 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002767 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002768 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002769 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002770 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002771 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002772 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002773 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
David Quigley14c43f72013-05-22 12:50:43 -04002775 if (label) {
2776 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2777 if (IS_ERR(olabel)) {
2778 status = PTR_ERR(olabel);
2779 goto err_opendata_put;
2780 }
2781 }
2782
Trond Myklebuste911b812014-03-26 13:24:37 -07002783 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2784 if (!opendata->f_attr.mdsthreshold) {
2785 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2786 if (!opendata->f_attr.mdsthreshold)
2787 goto err_free_label;
2788 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002789 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002790 }
David Howells2b0143b2015-03-17 22:25:59 +00002791 if (d_really_is_positive(dentry))
2792 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002793
Trond Myklebust3efb9722013-05-29 13:17:04 -04002794 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002795 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002796 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002797 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002798
NeilBrownefcbc042015-07-30 13:00:56 +10002799 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002800 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002801 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002802 /*
2803 * send create attributes which was not set by open
2804 * with an extra setattr.
2805 */
2806 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2807 nfs_fattr_init(opendata->o_res.f_attr);
2808 status = nfs4_do_setattr(state->inode, cred,
2809 opendata->o_res.f_attr, sattr,
2810 state, label, olabel);
2811 if (status == 0) {
2812 nfs_setattr_update_inode(state->inode, sattr,
2813 opendata->o_res.f_attr);
2814 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2815 }
David Quigley1775fd32013-05-22 12:50:42 -04002816 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002817 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002818 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002819 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002820
Trond Myklebuste911b812014-03-26 13:24:37 -07002821 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002822 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002823 opendata->f_attr.mdsthreshold = NULL;
2824 }
Andy Adamson82be4172012-05-23 05:02:35 -04002825
David Quigley14c43f72013-05-22 12:50:43 -04002826 nfs4_label_free(olabel);
2827
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002828 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002831err_free_label:
2832 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002833err_opendata_put:
2834 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002835err_put_state_owner:
2836 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return status;
2839}
2840
2841
Andy Adamson82be4172012-05-23 05:02:35 -04002842static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002843 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002844 int flags,
2845 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002846 struct nfs4_label *label,
2847 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002849 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 struct nfs4_exception exception = { };
2851 struct nfs4_state *res;
2852 int status;
2853
2854 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002855 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002856 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002857 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (status == 0)
2859 break;
2860 /* NOTE: BAD_SEQID means the server and client disagree about the
2861 * book-keeping w.r.t. state-changing operations
2862 * (OPEN/CLOSE/LOCK/LOCKU...)
2863 * It is actually a sign of a bug on the client or on the server.
2864 *
2865 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002866 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 * have unhashed the old state_owner for us, and that we can
2868 * therefore safely retry using a new one. We should still warn
2869 * the user though...
2870 */
2871 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002872 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002873 " returned a bad sequence-id error!\n",
2874 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 exception.retry = 1;
2876 continue;
2877 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002878 /*
2879 * BAD_STATEID on OPEN means that the server cancelled our
2880 * state before it received the OPEN_CONFIRM.
2881 * Recover by retrying the request as per the discussion
2882 * on Page 181 of RFC3530.
2883 */
2884 if (status == -NFS4ERR_BAD_STATEID) {
2885 exception.retry = 1;
2886 continue;
2887 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002888 if (status == -EAGAIN) {
2889 /* We must have found a delegation */
2890 exception.retry = 1;
2891 continue;
2892 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002893 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2894 continue;
2895 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 status, &exception));
2897 } while (exception.retry);
2898 return res;
2899}
2900
Trond Myklebust8487c472016-06-26 08:44:35 -04002901static int _nfs4_do_setattr(struct inode *inode,
2902 struct nfs_setattrargs *arg,
2903 struct nfs_setattrres *res,
2904 struct rpc_cred *cred,
2905 struct nfs4_state *state)
2906{
2907 struct nfs_server *server = NFS_SERVER(inode);
2908 struct rpc_message msg = {
2909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2910 .rpc_argp = arg,
2911 .rpc_resp = res,
2912 .rpc_cred = cred,
2913 };
2914 struct rpc_cred *delegation_cred = NULL;
2915 unsigned long timestamp = jiffies;
2916 fmode_t fmode;
2917 bool truncate;
2918 int status;
2919
2920 nfs_fattr_init(res->fattr);
2921
2922 /* Servers should only apply open mode checks for file size changes */
2923 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2924 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2925
2926 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2927 /* Use that stateid */
2928 } else if (truncate && state != NULL) {
2929 struct nfs_lockowner lockowner = {
2930 .l_owner = current->files,
2931 .l_pid = current->tgid,
2932 };
2933 if (!nfs4_valid_open_stateid(state))
2934 return -EBADF;
2935 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2936 &arg->stateid, &delegation_cred) == -EIO)
2937 return -EBADF;
2938 } else
2939 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2940 if (delegation_cred)
2941 msg.rpc_cred = delegation_cred;
2942
2943 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2944
2945 put_rpccred(delegation_cred);
2946 if (status == 0 && state != NULL)
2947 renew_lease(server, timestamp);
2948 trace_nfs4_setattr(inode, &arg->stateid, status);
2949 return status;
2950}
2951
2952static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2953 struct nfs_fattr *fattr, struct iattr *sattr,
2954 struct nfs4_state *state, struct nfs4_label *ilabel,
2955 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002957 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002959 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 .iap = sattr,
2961 .server = server,
2962 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002963 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 };
2965 struct nfs_setattrres res = {
2966 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002967 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 .server = server,
2969 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002970 struct nfs4_exception exception = {
2971 .state = state,
2972 .inode = inode,
2973 .stateid = &arg.stateid,
2974 };
2975 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
David Quigleyaa9c2662013-05-22 12:50:44 -04002977 arg.bitmask = nfs4_bitmask(server, ilabel);
2978 if (ilabel)
2979 arg.bitmask = nfs4_bitmask(server, olabel);
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002982 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002983 switch (err) {
2984 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002985 if (!(sattr->ia_valid & ATTR_SIZE)) {
2986 pr_warn_once("NFSv4: server %s is incorrectly "
2987 "applying open mode checks to "
2988 "a SETATTR that is not "
2989 "changing file size.\n",
2990 server->nfs_client->cl_hostname);
2991 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002992 if (state && !(state->state & FMODE_WRITE)) {
2993 err = -EBADF;
2994 if (sattr->ia_valid & ATTR_OPEN)
2995 err = -EACCES;
2996 goto out;
2997 }
2998 }
2999 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return err;
3003}
3004
Peng Tao500d7012015-09-22 11:35:22 +08003005static bool
3006nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3007{
3008 if (inode == NULL || !nfs_have_layout(inode))
3009 return false;
3010
3011 return pnfs_wait_on_layoutreturn(inode, task);
3012}
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014struct nfs4_closedata {
3015 struct inode *inode;
3016 struct nfs4_state *state;
3017 struct nfs_closeargs arg;
3018 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003019 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003020 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003021 bool roc;
3022 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023};
3024
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003025static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003026{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003027 struct nfs4_closedata *calldata = data;
3028 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003029 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003030
Fred Isamanf7e89172011-01-06 11:36:32 +00003031 if (calldata->roc)
3032 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003033 nfs4_put_open_state(calldata->state);
3034 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003035 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003036 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003037 kfree(calldata);
3038}
3039
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003040static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003042 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003045 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Chuck Levera3ca5652012-03-01 17:00:40 -05003047 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003048 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3049 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003050 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /* hmm. we are done with the inode, and in the process of freeing
3052 * the state_owner. we keep this around to process errors
3053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 switch (task->tk_status) {
3055 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003056 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003057 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003058 pnfs_roc_set_barrier(state->inode,
3059 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003060 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003061 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003062 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003064 case -NFS4ERR_EXPIRED:
3065 nfs4_free_revoked_stateid(server,
3066 &calldata->arg.stateid,
3067 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003068 case -NFS4ERR_OLD_STATEID:
3069 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003070 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003071 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003072 rpc_restart_call_prepare(task);
3073 goto out_release;
3074 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003075 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003078 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003079 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003080 goto out_release;
3081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003083 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3084 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003085out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003086 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003087 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003088 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003091static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003093 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003094 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003095 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003096 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003097 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003098
Chuck Levera3ca5652012-03-01 17:00:40 -05003099 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003101 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003102
Trond Myklebust88069f72009-12-08 08:33:16 -05003103 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003104 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003105 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3106 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3107 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003108 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003109 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003110 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003111 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003112 if (state->n_rdonly == 0)
3113 call_close |= is_rdonly;
3114 else if (is_rdonly)
3115 calldata->arg.fmode |= FMODE_READ;
3116 if (state->n_wronly == 0)
3117 call_close |= is_wronly;
3118 else if (is_wronly)
3119 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003120 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3121 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003122 } else if (is_rdwr)
3123 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3124
Trond Myklebust5d422302013-03-14 16:57:48 -04003125 if (!nfs4_valid_open_stateid(state))
3126 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003127 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003128
3129 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003130 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003131 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003132 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003133
Peng Tao500d7012015-09-22 11:35:22 +08003134 if (nfs4_wait_on_layoutreturn(inode, task)) {
3135 nfs_release_seqid(calldata->arg.seqid);
3136 goto out_wait;
3137 }
3138
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003139 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003140 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003141 if (calldata->roc)
3142 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3143
Trond Myklebust6ae37332015-01-30 14:21:14 -05003144 calldata->arg.share_access =
3145 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3146 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003147
Trond Myklebust516a6af2005-10-27 22:12:41 -04003148 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003149 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003150 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003151 &calldata->arg.seq_args,
3152 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003153 task) != 0)
3154 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003155 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003156 return;
3157out_no_action:
3158 task->tk_action = NULL;
3159out_wait:
3160 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003163static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003164 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003165 .rpc_call_done = nfs4_close_done,
3166 .rpc_release = nfs4_free_closedata,
3167};
3168
Peng Taofe08c542014-07-03 13:05:01 +08003169static bool nfs4_roc(struct inode *inode)
3170{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003171 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003172 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003173 return pnfs_roc(inode);
3174}
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176/*
3177 * It is possible for data to be read/written from a mem-mapped file
3178 * after the sys_close call (which hits the vfs layer as a flush).
3179 * This means that we can't safely call nfsv4 close on a file until
3180 * the inode is cleared. This in turn means that we are not good
3181 * NFSv4 citizens - we do not indicate to the server to update the file's
3182 * share state even when we are done with one of the three share
3183 * stateid's in the inode.
3184 *
3185 * NOTE: Caller must be holding the sp->so_owner semaphore!
3186 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003187int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003189 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003190 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003192 struct nfs4_state_owner *sp = state->owner;
3193 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3196 .rpc_cred = state->owner->so_cred,
3197 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003198 struct rpc_task_setup task_setup_data = {
3199 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003200 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003201 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003202 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003203 .flags = RPC_TASK_ASYNC,
3204 };
Trond Myklebust95121352005-10-18 14:20:12 -07003205 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003207 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3208 &task_setup_data.rpc_client, &msg);
3209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003210 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003212 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003213 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003214 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003216 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003218 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3219 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003220 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003221 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003222 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003223 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003224 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003225 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003226 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003227 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003228 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003229
Trond Myklebust1174dd12010-12-21 10:52:24 -05003230 msg.rpc_argp = &calldata->arg;
3231 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003232 task_setup_data.callback_data = calldata;
3233 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003234 if (IS_ERR(task))
3235 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003236 status = 0;
3237 if (wait)
3238 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003239 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003240 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003241out_free_calldata:
3242 kfree(calldata);
3243out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003244 nfs4_put_open_state(state);
3245 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003246 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Trond Myklebust2b484292010-09-17 10:56:51 -04003249static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003250nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3251 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003254 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3255
3256 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Trond Myklebust565277f2007-10-15 18:17:53 -04003258 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003259 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003260
3261 nfs4_label_release_security(label);
3262
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003263 if (IS_ERR(state))
3264 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003265 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Trond Myklebust1185a552009-12-03 15:54:02 -05003268static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003269{
3270 if (ctx->state == NULL)
3271 return;
3272 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003273 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003274 else
Al Viro643168c2011-06-22 18:20:23 -04003275 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Trond Myklebustb944dba2013-11-04 15:20:20 -05003278#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3279#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003280#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3283{
Kinglong Mee8c612822015-08-26 21:12:58 +08003284 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003285 struct nfs4_server_caps_arg args = {
3286 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003287 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003288 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 struct nfs4_server_caps_res res = {};
3290 struct rpc_message msg = {
3291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003292 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 .rpc_resp = &res,
3294 };
3295 int status;
3296
Kinglong Mee8c612822015-08-26 21:12:58 +08003297 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3298 FATTR4_WORD0_FH_EXPIRE_TYPE |
3299 FATTR4_WORD0_LINK_SUPPORT |
3300 FATTR4_WORD0_SYMLINK_SUPPORT |
3301 FATTR4_WORD0_ACLSUPPORT;
3302 if (minorversion)
3303 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3304
Bryan Schumaker7c513052011-03-24 17:12:24 +00003305 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003307 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003308 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003309 case 0:
3310 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3311 res.attr_bitmask[2] = 0;
3312 break;
3313 case 1:
3314 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3315 break;
3316 case 2:
3317 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003320 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3321 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3322 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3323 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003324 NFS_CAP_CTIME|NFS_CAP_MTIME|
3325 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003326 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3327 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 server->caps |= NFS_CAP_ACLS;
3329 if (res.has_links != 0)
3330 server->caps |= NFS_CAP_HARDLINKS;
3331 if (res.has_symlinks != 0)
3332 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003333 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3334 server->caps |= NFS_CAP_FILEID;
3335 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3336 server->caps |= NFS_CAP_MODE;
3337 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3338 server->caps |= NFS_CAP_NLINK;
3339 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3340 server->caps |= NFS_CAP_OWNER;
3341 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3342 server->caps |= NFS_CAP_OWNER_GROUP;
3343 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3344 server->caps |= NFS_CAP_ATIME;
3345 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3346 server->caps |= NFS_CAP_CTIME;
3347 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3348 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003349#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3350 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3351 server->caps |= NFS_CAP_SECURITY_LABEL;
3352#endif
3353 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3354 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003355 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003356
Trond Myklebusta65318b2009-03-11 14:10:28 -04003357 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3358 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3359 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003360 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003361 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3362 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003364 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return status;
3368}
3369
Trond Myklebust55a97592006-06-09 09:34:19 -04003370int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 struct nfs4_exception exception = { };
3373 int err;
3374 do {
3375 err = nfs4_handle_exception(server,
3376 _nfs4_server_capabilities(server, fhandle),
3377 &exception);
3378 } while (exception.retry);
3379 return err;
3380}
3381
3382static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3383 struct nfs_fsinfo *info)
3384{
David Quigleyaa9c2662013-05-22 12:50:44 -04003385 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003387 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 };
3389 struct nfs4_lookup_res res = {
3390 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003391 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 .fh = fhandle,
3393 };
3394 struct rpc_message msg = {
3395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3396 .rpc_argp = &args,
3397 .rpc_resp = &res,
3398 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003399
David Quigleyaa9c2662013-05-22 12:50:44 -04003400 bitmask[0] = nfs4_fattr_bitmap[0];
3401 bitmask[1] = nfs4_fattr_bitmap[1];
3402 /*
3403 * Process the label in the upcoming getfattr
3404 */
3405 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3406
Trond Myklebust0e574af2005-10-27 22:12:38 -04003407 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003408 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
3411static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3412 struct nfs_fsinfo *info)
3413{
3414 struct nfs4_exception exception = { };
3415 int err;
3416 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003417 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003418 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003419 switch (err) {
3420 case 0:
3421 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003422 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003423 default:
3424 err = nfs4_handle_exception(server, err, &exception);
3425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003427out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return err;
3429}
3430
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003431static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3432 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3433{
Trond Myklebustc2190662013-08-26 19:23:04 -04003434 struct rpc_auth_create_args auth_args = {
3435 .pseudoflavor = flavor,
3436 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003437 struct rpc_auth *auth;
3438 int ret;
3439
Trond Myklebustc2190662013-08-26 19:23:04 -04003440 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003441 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003442 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003443 goto out;
3444 }
3445 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003446out:
3447 return ret;
3448}
3449
Chuck Lever9a744ba2013-03-16 15:56:02 -04003450/*
3451 * Retry pseudoroot lookup with various security flavors. We do this when:
3452 *
3453 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3454 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3455 *
3456 * Returns zero on success, or a negative NFS4ERR value, or a
3457 * negative errno value.
3458 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003459static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003460 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003462 /* Per 3530bis 15.33.5 */
3463 static const rpc_authflavor_t flav_array[] = {
3464 RPC_AUTH_GSS_KRB5P,
3465 RPC_AUTH_GSS_KRB5I,
3466 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003467 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003468 RPC_AUTH_NULL,
3469 };
3470 int status = -EPERM;
3471 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003473 if (server->auth_info.flavor_len > 0) {
3474 /* try each flavor specified by user */
3475 for (i = 0; i < server->auth_info.flavor_len; i++) {
3476 status = nfs4_lookup_root_sec(server, fhandle, info,
3477 server->auth_info.flavors[i]);
3478 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3479 continue;
3480 break;
3481 }
3482 } else {
3483 /* no flavors specified by user, try default list */
3484 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3485 status = nfs4_lookup_root_sec(server, fhandle, info,
3486 flav_array[i]);
3487 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3488 continue;
3489 break;
3490 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003491 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003492
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003493 /*
3494 * -EACCESS could mean that the user doesn't have correct permissions
3495 * to access the mount. It could also mean that we tried to mount
3496 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3497 * existing mount programs don't handle -EACCES very well so it should
3498 * be mapped to -EPERM instead.
3499 */
3500 if (status == -EACCES)
3501 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003502 return status;
3503}
3504
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003505/**
3506 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3507 * @server: initialized nfs_server handle
3508 * @fhandle: we fill in the pseudo-fs root file handle
3509 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003510 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003511 *
3512 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003513 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003514int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003515 struct nfs_fsinfo *info,
3516 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003517{
Andre Przywarac7757072015-04-23 17:17:40 +01003518 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003519
Andre Przywarac7757072015-04-23 17:17:40 +01003520 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003521 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003522
3523 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003524 status = server->nfs_client->cl_mvops->find_root_sec(server,
3525 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 if (status == 0)
3528 status = nfs4_server_capabilities(server, fhandle);
3529 if (status == 0)
3530 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003531
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003532 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533}
3534
Bryan Schumakerbae36242012-05-10 15:07:31 -04003535static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3536 struct nfs_fsinfo *info)
3537{
3538 int error;
3539 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003540 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003541
3542 error = nfs4_server_capabilities(server, mntfh);
3543 if (error < 0) {
3544 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3545 return error;
3546 }
3547
David Quigley14c43f72013-05-22 12:50:43 -04003548 label = nfs4_label_alloc(server, GFP_KERNEL);
3549 if (IS_ERR(label))
3550 return PTR_ERR(label);
3551
David Quigley1775fd32013-05-22 12:50:42 -04003552 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003553 if (error < 0) {
3554 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003555 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003556 }
3557
3558 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3559 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3560 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3561
David Quigley14c43f72013-05-22 12:50:43 -04003562err_free_label:
3563 nfs4_label_free(label);
3564
Bryan Schumakerbae36242012-05-10 15:07:31 -04003565 return error;
3566}
3567
Manoj Naik6b97fd32006-06-09 09:34:29 -04003568/*
3569 * Get locations and (maybe) other attributes of a referral.
3570 * Note that we'll actually follow the referral later when
3571 * we detect fsid mismatch in inode revalidation
3572 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003573static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3574 const struct qstr *name, struct nfs_fattr *fattr,
3575 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003576{
3577 int status = -ENOMEM;
3578 struct page *page = NULL;
3579 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580
3581 page = alloc_page(GFP_KERNEL);
3582 if (page == NULL)
3583 goto out;
3584 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3585 if (locations == NULL)
3586 goto out;
3587
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003588 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003589 if (status != 0)
3590 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003591
3592 /*
3593 * If the fsid didn't change, this is a migration event, not a
3594 * referral. Cause us to drop into the exception handler, which
3595 * will kick off migration recovery.
3596 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003597 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003598 dprintk("%s: server did not return a different fsid for"
3599 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003600 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003601 goto out;
3602 }
Andy Adamson533eb462011-06-13 18:25:56 -04003603 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3604 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003605
Andy Adamson533eb462011-06-13 18:25:56 -04003606 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003607 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003608 memset(fhandle, 0, sizeof(struct nfs_fh));
3609out:
3610 if (page)
3611 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003612 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003613 return status;
3614}
3615
David Quigley1775fd32013-05-22 12:50:42 -04003616static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3617 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 struct nfs4_getattr_arg args = {
3620 .fh = fhandle,
3621 .bitmask = server->attr_bitmask,
3622 };
3623 struct nfs4_getattr_res res = {
3624 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003625 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 .server = server,
3627 };
3628 struct rpc_message msg = {
3629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3630 .rpc_argp = &args,
3631 .rpc_resp = &res,
3632 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003633
3634 args.bitmask = nfs4_bitmask(server, label);
3635
Trond Myklebust0e574af2005-10-27 22:12:38 -04003636 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003637 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
David Quigley1775fd32013-05-22 12:50:42 -04003640static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3641 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 struct nfs4_exception exception = { };
3644 int err;
3645 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003646 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3647 trace_nfs4_getattr(server, fhandle, fattr, err);
3648 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 &exception);
3650 } while (exception.retry);
3651 return err;
3652}
3653
3654/*
3655 * The file is not closed if it is opened due to the a request to change
3656 * the size of the file. The open call will not be needed once the
3657 * VFS layer lookup-intents are implemented.
3658 *
3659 * Close is called when the inode is destroyed.
3660 * If we haven't opened the file for O_WRONLY, we
3661 * need to in the size_change case to obtain a stateid.
3662 *
3663 * Got race?
3664 * Because OPEN is always done by name in nfsv4, it is
3665 * possible that we opened a different file by the same
3666 * name. We can recognize this race condition, but we
3667 * can't do anything about it besides returning an error.
3668 *
3669 * This will be fixed with VFS changes (lookup-intent).
3670 */
3671static int
3672nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3673 struct iattr *sattr)
3674{
David Howells2b0143b2015-03-17 22:25:59 +00003675 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003676 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003677 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003678 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 int status;
3680
Peng Tao88ac8152014-09-12 11:04:10 +08003681 if (pnfs_ld_layoutret_on_setattr(inode) &&
3682 sattr->ia_valid & ATTR_SIZE &&
3683 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003684 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003685
Trond Myklebust0e574af2005-10-27 22:12:38 -04003686 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Andy Adamson26699402012-05-30 16:12:24 -04003688 /* Deal with open(O_TRUNC) */
3689 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003690 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003691
3692 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003693 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003694 return 0;
3695
Trond Myklebustd5308382005-11-04 15:33:38 -05003696 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003697 if (sattr->ia_valid & ATTR_FILE) {
3698 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003699
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003700 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003701 if (ctx) {
3702 cred = ctx->cred;
3703 state = ctx->state;
3704 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003705 }
3706
David Quigley14c43f72013-05-22 12:50:43 -04003707 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3708 if (IS_ERR(label))
3709 return PTR_ERR(label);
3710
3711 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003712 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003713 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003714 nfs_setsecurity(inode, fattr, label);
3715 }
David Quigley14c43f72013-05-22 12:50:43 -04003716 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return status;
3718}
3719
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003720static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3721 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003722 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003723{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003724 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003725 int status;
3726 struct nfs4_lookup_arg args = {
3727 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003728 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003729 .name = name,
3730 };
3731 struct nfs4_lookup_res res = {
3732 .server = server,
3733 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003734 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003735 .fh = fhandle,
3736 };
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3739 .rpc_argp = &args,
3740 .rpc_resp = &res,
3741 };
3742
David Quigleyaa9c2662013-05-22 12:50:44 -04003743 args.bitmask = nfs4_bitmask(server, label);
3744
David Howells2b3de442006-08-22 20:06:09 -04003745 nfs_fattr_init(fattr);
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003748 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 dprintk("NFS reply lookup: %d\n", status);
3750 return status;
3751}
3752
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003753static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003754{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003755 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003756 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003757 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3758 fattr->nlink = 2;
3759}
3760
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003761static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003762 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003763 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003764{
3765 struct nfs4_exception exception = { };
3766 struct rpc_clnt *client = *clnt;
3767 int err;
3768 do {
David Quigley1775fd32013-05-22 12:50:42 -04003769 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003770 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003771 switch (err) {
3772 case -NFS4ERR_BADNAME:
3773 err = -ENOENT;
3774 goto out;
3775 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003776 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003777 if (err == -NFS4ERR_MOVED)
3778 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003779 goto out;
3780 case -NFS4ERR_WRONGSEC:
3781 err = -EPERM;
3782 if (client != *clnt)
3783 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003784 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003785 if (IS_ERR(client))
3786 return PTR_ERR(client);
3787
3788 exception.retry = 1;
3789 break;
3790 default:
3791 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3792 }
3793 } while (exception.retry);
3794
3795out:
3796 if (err == 0)
3797 *clnt = client;
3798 else if (client != *clnt)
3799 rpc_shutdown_client(client);
3800
3801 return err;
3802}
3803
Al Virobeffb8f2016-07-20 16:34:42 -04003804static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003805 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3806 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003808 int status;
3809 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003810
David Quigley1775fd32013-05-22 12:50:42 -04003811 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003812 if (client != NFS_CLIENT(dir)) {
3813 rpc_shutdown_client(client);
3814 nfs_fixup_secinfo_attributes(fattr);
3815 }
3816 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003819struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003820nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003821 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3822{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003823 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003824 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003825
David Quigley1775fd32013-05-22 12:50:42 -04003826 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003827 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003828 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003829 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003830}
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3833{
Trond Myklebust76b32992007-08-10 17:45:11 -04003834 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 struct nfs4_accessargs args = {
3836 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003837 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003839 struct nfs4_accessres res = {
3840 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003841 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 struct rpc_message msg = {
3843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3844 .rpc_argp = &args,
3845 .rpc_resp = &res,
3846 .rpc_cred = entry->cred,
3847 };
3848 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003849 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
3851 /*
3852 * Determine which access bits we want to ask for...
3853 */
3854 if (mode & MAY_READ)
3855 args.access |= NFS4_ACCESS_READ;
3856 if (S_ISDIR(inode->i_mode)) {
3857 if (mode & MAY_WRITE)
3858 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3859 if (mode & MAY_EXEC)
3860 args.access |= NFS4_ACCESS_LOOKUP;
3861 } else {
3862 if (mode & MAY_WRITE)
3863 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3864 if (mode & MAY_EXEC)
3865 args.access |= NFS4_ACCESS_EXECUTE;
3866 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003867
3868 res.fattr = nfs_alloc_fattr();
3869 if (res.fattr == NULL)
3870 return -ENOMEM;
3871
Bryan Schumaker7c513052011-03-24 17:12:24 +00003872 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003874 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003875 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003877 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 return status;
3879}
3880
3881static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3882{
3883 struct nfs4_exception exception = { };
3884 int err;
3885 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003886 err = _nfs4_proc_access(inode, entry);
3887 trace_nfs4_access(inode, err);
3888 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 &exception);
3890 } while (exception.retry);
3891 return err;
3892}
3893
3894/*
3895 * TODO: For the time being, we don't try to get any attributes
3896 * along with any of the zero-copy operations READ, READDIR,
3897 * READLINK, WRITE.
3898 *
3899 * In the case of the first three, we want to put the GETATTR
3900 * after the read-type operation -- this is because it is hard
3901 * to predict the length of a GETATTR response in v4, and thus
3902 * align the READ data correctly. This means that the GETATTR
3903 * may end up partially falling into the page cache, and we should
3904 * shift it into the 'tail' of the xdr_buf before processing.
3905 * To do this efficiently, we need to know the total length
3906 * of data received, which doesn't seem to be available outside
3907 * of the RPC layer.
3908 *
3909 * In the case of WRITE, we also want to put the GETATTR after
3910 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003911 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 *
3913 * Both of these changes to the XDR layer would in fact be quite
3914 * minor, but I decided to leave them for a subsequent patch.
3915 */
3916static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3917 unsigned int pgbase, unsigned int pglen)
3918{
3919 struct nfs4_readlink args = {
3920 .fh = NFS_FH(inode),
3921 .pgbase = pgbase,
3922 .pglen = pglen,
3923 .pages = &page,
3924 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003925 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3928 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003929 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 };
3931
Bryan Schumaker7c513052011-03-24 17:12:24 +00003932 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 -07003933}
3934
3935static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3936 unsigned int pgbase, unsigned int pglen)
3937{
3938 struct nfs4_exception exception = { };
3939 int err;
3940 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003941 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3942 trace_nfs4_readlink(inode, err);
3943 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 &exception);
3945 } while (exception.retry);
3946 return err;
3947}
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003950 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952static int
3953nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003954 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
David Quigleyaa9c2662013-05-22 12:50:44 -04003956 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003957 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 int status = 0;
3960
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003961 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3962 if (IS_ERR(ctx))
3963 return PTR_ERR(ctx);
3964
David Quigleyaa9c2662013-05-22 12:50:44 -04003965 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3966
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003967 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003968 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (IS_ERR(state)) {
3970 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003974 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003975 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return status;
3977}
3978
Al Virobeffb8f2016-07-20 16:34:42 -04003979static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Trond Myklebust16e42952005-10-27 22:12:44 -04003981 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003982 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003984 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003986 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003987 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003988 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3991 .rpc_argp = &args,
3992 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003994 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003995
Bryan Schumaker7c513052011-03-24 17:12:24 +00003996 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003997 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003998 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 return status;
4000}
4001
Al Virobeffb8f2016-07-20 16:34:42 -04004002static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003{
4004 struct nfs4_exception exception = { };
4005 int err;
4006 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004007 err = _nfs4_proc_remove(dir, name);
4008 trace_nfs4_remove(dir, name, err);
4009 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 &exception);
4011 } while (exception.retry);
4012 return err;
4013}
4014
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004017 struct nfs_server *server = NFS_SERVER(dir);
4018 struct nfs_removeargs *args = msg->rpc_argp;
4019 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004021 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004023 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004024
4025 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004028static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4029{
Al Viro884be172016-04-28 23:56:31 -04004030 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004031 &data->args.seq_args,
4032 &data->res.seq_res,
4033 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004036static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004038 struct nfs_unlinkdata *data = task->tk_calldata;
4039 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004040
Trond Myklebust14516c32010-07-31 14:29:06 -04004041 if (!nfs4_sequence_done(task, &res->seq_res))
4042 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004043 if (nfs4_async_handle_error(task, res->server, NULL,
4044 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004045 return 0;
4046 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004047 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
Jeff Laytond3d41522010-09-17 17:31:57 -04004050static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4051{
4052 struct nfs_server *server = NFS_SERVER(dir);
4053 struct nfs_renameargs *arg = msg->rpc_argp;
4054 struct nfs_renameres *res = msg->rpc_resp;
4055
4056 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004057 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004058 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004059}
4060
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004061static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4062{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004063 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4064 &data->args.seq_args,
4065 &data->res.seq_res,
4066 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004067}
4068
4069static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4070 struct inode *new_dir)
4071{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004072 struct nfs_renamedata *data = task->tk_calldata;
4073 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004074
4075 if (!nfs4_sequence_done(task, &res->seq_res))
4076 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004077 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004078 return 0;
4079
4080 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004081 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004082 return 1;
4083}
4084
Al Virobeffb8f2016-07-20 16:34:42 -04004085static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004087 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 struct nfs4_link_arg arg = {
4089 .fh = NFS_FH(inode),
4090 .dir_fh = NFS_FH(dir),
4091 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004092 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004094 struct nfs4_link_res res = {
4095 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004096 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 struct rpc_message msg = {
4099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4100 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004101 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004103 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Trond Myklebust136f2622010-04-16 16:22:49 -04004105 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004106 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004107 goto out;
4108
David Quigley14c43f72013-05-22 12:50:43 -04004109 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4110 if (IS_ERR(res.label)) {
4111 status = PTR_ERR(res.label);
4112 goto out;
4113 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004114 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004115
Bryan Schumaker7c513052011-03-24 17:12:24 +00004116 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004117 if (!status) {
4118 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004119 status = nfs_post_op_update_inode(inode, res.fattr);
4120 if (!status)
4121 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004122 }
David Quigley14c43f72013-05-22 12:50:43 -04004123
4124
4125 nfs4_label_free(res.label);
4126
Trond Myklebust136f2622010-04-16 16:22:49 -04004127out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004128 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return status;
4130}
4131
Al Virobeffb8f2016-07-20 16:34:42 -04004132static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
4134 struct nfs4_exception exception = { };
4135 int err;
4136 do {
4137 err = nfs4_handle_exception(NFS_SERVER(inode),
4138 _nfs4_proc_link(inode, dir, name),
4139 &exception);
4140 } while (exception.retry);
4141 return err;
4142}
4143
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004144struct nfs4_createdata {
4145 struct rpc_message msg;
4146 struct nfs4_create_arg arg;
4147 struct nfs4_create_res res;
4148 struct nfs_fh fh;
4149 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004150 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004151};
4152
4153static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004154 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004155{
4156 struct nfs4_createdata *data;
4157
4158 data = kzalloc(sizeof(*data), GFP_KERNEL);
4159 if (data != NULL) {
4160 struct nfs_server *server = NFS_SERVER(dir);
4161
David Quigley14c43f72013-05-22 12:50:43 -04004162 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4163 if (IS_ERR(data->label))
4164 goto out_free;
4165
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004166 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4167 data->msg.rpc_argp = &data->arg;
4168 data->msg.rpc_resp = &data->res;
4169 data->arg.dir_fh = NFS_FH(dir);
4170 data->arg.server = server;
4171 data->arg.name = name;
4172 data->arg.attrs = sattr;
4173 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004174 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004175 data->res.server = server;
4176 data->res.fh = &data->fh;
4177 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004178 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180 }
4181 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004182out_free:
4183 kfree(data);
4184 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185}
4186
4187static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4188{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004189 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004190 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191 if (status == 0) {
4192 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004193 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194 }
4195 return status;
4196}
4197
4198static void nfs4_free_createdata(struct nfs4_createdata *data)
4199{
David Quigley14c43f72013-05-22 12:50:43 -04004200 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 kfree(data);
4202}
4203
Chuck Lever4f390c12006-08-22 20:06:22 -04004204static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004205 struct page *page, unsigned int len, struct iattr *sattr,
4206 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 struct nfs4_createdata *data;
4209 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Chuck Lever94a6d752006-08-22 20:06:23 -04004211 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004213
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004214 status = -ENOMEM;
4215 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4216 if (data == NULL)
4217 goto out;
4218
4219 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4220 data->arg.u.symlink.pages = &page;
4221 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004222 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004224 status = nfs4_do_create(dir, dentry, data);
4225
4226 nfs4_free_createdata(data);
4227out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 return status;
4229}
4230
Chuck Lever4f390c12006-08-22 20:06:22 -04004231static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004232 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
4234 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004235 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004237
4238 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004241 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4242 trace_nfs4_symlink(dir, &dentry->d_name, err);
4243 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 &exception);
4245 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004246
4247 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return err;
4249}
4250
4251static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004252 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004254 struct nfs4_createdata *data;
4255 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4258 if (data == NULL)
4259 goto out;
4260
David Quigley1775fd32013-05-22 12:50:42 -04004261 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 status = nfs4_do_create(dir, dentry, data);
4263
4264 nfs4_free_createdata(data);
4265out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return status;
4267}
4268
4269static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4270 struct iattr *sattr)
4271{
4272 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004273 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004275
David Quigleyaa9c2662013-05-22 12:50:44 -04004276 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4277
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004278 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004280 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4281 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4282 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 &exception);
4284 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004285 nfs4_label_release_security(label);
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return err;
4288}
4289
4290static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004291 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
David Howells2b0143b2015-03-17 22:25:59 +00004293 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 struct nfs4_readdir_arg args = {
4295 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004296 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 .pgbase = 0,
4298 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004299 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004300 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 };
4302 struct nfs4_readdir_res res;
4303 struct rpc_message msg = {
4304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4305 .rpc_argp = &args,
4306 .rpc_resp = &res,
4307 .rpc_cred = cred,
4308 };
4309 int status;
4310
Al Viro6de14722013-09-16 10:53:17 -04004311 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4312 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004313 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004314 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004316 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 -05004317 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004318 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004319 status += args.pgbase;
4320 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004321
4322 nfs_invalidate_atime(dir);
4323
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return status;
4326}
4327
4328static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004329 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
4331 struct nfs4_exception exception = { };
4332 int err;
4333 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004334 err = _nfs4_proc_readdir(dentry, cred, cookie,
4335 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004336 trace_nfs4_readdir(d_inode(dentry), err);
4337 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 &exception);
4339 } while (exception.retry);
4340 return err;
4341}
4342
4343static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004344 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004346 struct nfs4_createdata *data;
4347 int mode = sattr->ia_mode;
4348 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4351 if (data == NULL)
4352 goto out;
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004355 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004357 data->arg.ftype = NF4BLK;
4358 data->arg.u.device.specdata1 = MAJOR(rdev);
4359 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 }
4361 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004362 data->arg.ftype = NF4CHR;
4363 data->arg.u.device.specdata1 = MAJOR(rdev);
4364 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004365 } else if (!S_ISSOCK(mode)) {
4366 status = -EINVAL;
4367 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
David Quigley1775fd32013-05-22 12:50:42 -04004369
David Quigleyaa9c2662013-05-22 12:50:44 -04004370 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004371 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004372out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004373 nfs4_free_createdata(data);
4374out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return status;
4376}
4377
4378static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4379 struct iattr *sattr, dev_t rdev)
4380{
4381 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004382 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004384
David Quigleyaa9c2662013-05-22 12:50:44 -04004385 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4386
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004387 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004389 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4390 trace_nfs4_mknod(dir, &dentry->d_name, err);
4391 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 &exception);
4393 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004394
4395 nfs4_label_release_security(label);
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return err;
4398}
4399
4400static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4401 struct nfs_fsstat *fsstat)
4402{
4403 struct nfs4_statfs_arg args = {
4404 .fh = fhandle,
4405 .bitmask = server->attr_bitmask,
4406 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004407 struct nfs4_statfs_res res = {
4408 .fsstat = fsstat,
4409 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4412 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004413 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 };
4415
Trond Myklebust0e574af2005-10-27 22:12:38 -04004416 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004417 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
4420static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4421{
4422 struct nfs4_exception exception = { };
4423 int err;
4424 do {
4425 err = nfs4_handle_exception(server,
4426 _nfs4_proc_statfs(server, fhandle, fsstat),
4427 &exception);
4428 } while (exception.retry);
4429 return err;
4430}
4431
4432static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4433 struct nfs_fsinfo *fsinfo)
4434{
4435 struct nfs4_fsinfo_arg args = {
4436 .fh = fhandle,
4437 .bitmask = server->attr_bitmask,
4438 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004439 struct nfs4_fsinfo_res res = {
4440 .fsinfo = fsinfo,
4441 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 struct rpc_message msg = {
4443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4444 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004445 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 };
4447
Bryan Schumaker7c513052011-03-24 17:12:24 +00004448 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
4451static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4452{
4453 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004454 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 int err;
4456
4457 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004458 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004459 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004460 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004461 nfs4_set_lease_period(server->nfs_client,
4462 fsinfo->lease_time * HZ,
4463 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004464 break;
4465 }
4466 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 } while (exception.retry);
4468 return err;
4469}
4470
4471static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4472{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004473 int error;
4474
Trond Myklebust0e574af2005-10-27 22:12:38 -04004475 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004476 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004477 if (error == 0) {
4478 /* block layout checks this! */
4479 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004480 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004481 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004482
4483 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
4486static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4487 struct nfs_pathconf *pathconf)
4488{
4489 struct nfs4_pathconf_arg args = {
4490 .fh = fhandle,
4491 .bitmask = server->attr_bitmask,
4492 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004493 struct nfs4_pathconf_res res = {
4494 .pathconf = pathconf,
4495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4498 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004499 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 };
4501
4502 /* None of the pathconf attributes are mandatory to implement */
4503 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4504 memset(pathconf, 0, sizeof(*pathconf));
4505 return 0;
4506 }
4507
Trond Myklebust0e574af2005-10-27 22:12:38 -04004508 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004509 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510}
4511
4512static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4513 struct nfs_pathconf *pathconf)
4514{
4515 struct nfs4_exception exception = { };
4516 int err;
4517
4518 do {
4519 err = nfs4_handle_exception(server,
4520 _nfs4_proc_pathconf(server, fhandle, pathconf),
4521 &exception);
4522 } while (exception.retry);
4523 return err;
4524}
4525
Trond Myklebust5521abf2013-03-16 20:54:34 -04004526int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004527 const struct nfs_open_context *ctx,
4528 const struct nfs_lock_context *l_ctx,
4529 fmode_t fmode)
4530{
4531 const struct nfs_lockowner *lockowner = NULL;
4532
4533 if (l_ctx != NULL)
4534 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004535 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004536}
4537EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4538
Trond Myklebust5521abf2013-03-16 20:54:34 -04004539static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4540 const struct nfs_open_context *ctx,
4541 const struct nfs_lock_context *l_ctx,
4542 fmode_t fmode)
4543{
4544 nfs4_stateid current_stateid;
4545
Trond Myklebuste1253be2014-03-05 08:44:23 -05004546 /* If the current stateid represents a lost lock, then exit */
4547 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4548 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004549 return nfs4_stateid_match(stateid, &current_stateid);
4550}
4551
4552static bool nfs4_error_stateid_expired(int err)
4553{
4554 switch (err) {
4555 case -NFS4ERR_DELEG_REVOKED:
4556 case -NFS4ERR_ADMIN_REVOKED:
4557 case -NFS4ERR_BAD_STATEID:
4558 case -NFS4ERR_STALE_STATEID:
4559 case -NFS4ERR_OLD_STATEID:
4560 case -NFS4ERR_OPENMODE:
4561 case -NFS4ERR_EXPIRED:
4562 return true;
4563 }
4564 return false;
4565}
4566
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004567static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004569 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004571 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004572 if (task->tk_status < 0) {
4573 struct nfs4_exception exception = {
4574 .inode = hdr->inode,
4575 .state = hdr->args.context->state,
4576 .stateid = &hdr->args.stateid,
4577 };
4578 task->tk_status = nfs4_async_handle_exception(task,
4579 server, task->tk_status, &exception);
4580 if (exception.retry) {
4581 rpc_restart_call_prepare(task);
4582 return -EAGAIN;
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004587 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589}
4590
Trond Myklebust5521abf2013-03-16 20:54:34 -04004591static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004592 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004593{
4594
4595 if (!nfs4_error_stateid_expired(task->tk_status) ||
4596 nfs4_stateid_is_current(&args->stateid,
4597 args->context,
4598 args->lock_context,
4599 FMODE_READ))
4600 return false;
4601 rpc_restart_call_prepare(task);
4602 return true;
4603}
4604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004606{
4607
4608 dprintk("--> %s\n", __func__);
4609
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004611 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004613 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004614 if (task->tk_status > 0)
4615 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004616 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4617 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004618}
4619
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4621 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004624 if (!hdr->pgio_done_cb)
4625 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4631 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004632{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004633 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4634 &hdr->args.seq_args,
4635 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004636 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004637 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004638 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4639 hdr->args.lock_context,
4640 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004641 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004642 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004643 return -EIO;
4644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004647static int nfs4_write_done_cb(struct rpc_task *task,
4648 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004651
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004652 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004653 if (task->tk_status < 0) {
4654 struct nfs4_exception exception = {
4655 .inode = hdr->inode,
4656 .state = hdr->args.context->state,
4657 .stateid = &hdr->args.stateid,
4658 };
4659 task->tk_status = nfs4_async_handle_exception(task,
4660 NFS_SERVER(inode), task->tk_status,
4661 &exception);
4662 if (exception.retry) {
4663 rpc_restart_call_prepare(task);
4664 return -EAGAIN;
4665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004667 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004669 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004670 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
Trond Myklebust5521abf2013-03-16 20:54:34 -04004674static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004675 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004676{
4677
4678 if (!nfs4_error_stateid_expired(task->tk_status) ||
4679 nfs4_stateid_is_current(&args->stateid,
4680 args->context,
4681 args->lock_context,
4682 FMODE_WRITE))
4683 return false;
4684 rpc_restart_call_prepare(task);
4685 return true;
4686}
4687
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004689{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004691 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004693 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4695 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004696}
4697
Trond Myklebust5a37f852012-04-28 14:55:16 -04004698static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004700{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004701 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004703 return false;
4704 /* Otherwise, request attributes if and only if we don't hold
4705 * a delegation
4706 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004707 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004708}
Fred Isamana69aef12011-03-03 15:13:47 +00004709
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004710static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4711 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004714
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4716 hdr->args.bitmask = NULL;
4717 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004718 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004720
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004721 if (!hdr->pgio_done_cb)
4722 hdr->pgio_done_cb = nfs4_write_done_cb;
4723 hdr->res.server = server;
4724 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004727 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Fred Isaman0b7c0152012-04-20 14:47:39 -04004730static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4731{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004732 nfs4_setup_sequence(NFS_SERVER(data->inode),
4733 &data->args.seq_args,
4734 &data->res.seq_res,
4735 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736}
4737
Fred Isaman0b7c0152012-04-20 14:47:39 -04004738static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004741
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004742 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004743 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4744 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004745 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004746 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
Fred Isaman0b7c0152012-04-20 14:47:39 -04004751static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004752{
4753 if (!nfs4_sequence_done(task, &data->res.seq_res))
4754 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004755 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004756}
4757
Fred Isaman0b7c0152012-04-20 14:47:39 -04004758static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004760 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004761
Fred Isaman0b7c0152012-04-20 14:47:39 -04004762 if (data->commit_done_cb == NULL)
4763 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004764 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004765 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004766 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004769struct nfs4_renewdata {
4770 struct nfs_client *client;
4771 unsigned long timestamp;
4772};
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774/*
4775 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4776 * standalone procedure for queueing an asynchronous RENEW.
4777 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004778static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004779{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004780 struct nfs4_renewdata *data = calldata;
4781 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004782
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004783 if (atomic_read(&clp->cl_count) > 1)
4784 nfs4_schedule_state_renewal(clp);
4785 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004786 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004787}
4788
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004789static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004791 struct nfs4_renewdata *data = calldata;
4792 struct nfs_client *clp = data->client;
4793 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004795 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004796 switch (task->tk_status) {
4797 case 0:
4798 break;
4799 case -NFS4ERR_LEASE_MOVED:
4800 nfs4_schedule_lease_moved_recovery(clp);
4801 break;
4802 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004803 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004804 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4805 return;
4806 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004807 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004808 return;
4809 }
4810 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004812 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004815static const struct rpc_call_ops nfs4_renew_ops = {
4816 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004817 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004818};
4819
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004820static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4824 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004825 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004829 if (renew_flags == 0)
4830 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004831 if (!atomic_inc_not_zero(&clp->cl_count))
4832 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004833 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004834 if (data == NULL)
4835 return -ENOMEM;
4836 data->client = clp;
4837 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004838 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004839 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004842static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
4844 struct rpc_message msg = {
4845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4846 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004847 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 };
4849 unsigned long now = jiffies;
4850 int status;
4851
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004852 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 if (status < 0)
4854 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004855 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return 0;
4857}
4858
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004859static inline int nfs4_server_supports_acls(struct nfs_server *server)
4860{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004861 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004862}
4863
Trond Myklebust21f498c2012-08-24 10:59:25 -04004864/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4865 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004866 * the stack.
4867 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004868#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004869
Neil Hormane9e3d722011-03-04 19:26:03 -05004870static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004871 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004872{
4873 struct page *newpage, **spages;
4874 int rc = 0;
4875 size_t len;
4876 spages = pages;
4877
4878 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004879 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004880 newpage = alloc_page(GFP_KERNEL);
4881
4882 if (newpage == NULL)
4883 goto unwind;
4884 memcpy(page_address(newpage), buf, len);
4885 buf += len;
4886 buflen -= len;
4887 *pages++ = newpage;
4888 rc++;
4889 } while (buflen != 0);
4890
4891 return rc;
4892
4893unwind:
4894 for(; rc > 0; rc--)
4895 __free_page(spages[rc-1]);
4896 return -ENOMEM;
4897}
4898
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004899struct nfs4_cached_acl {
4900 int cached;
4901 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004902 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004903};
4904
4905static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004906{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004907 struct nfs_inode *nfsi = NFS_I(inode);
4908
4909 spin_lock(&inode->i_lock);
4910 kfree(nfsi->nfs4_acl);
4911 nfsi->nfs4_acl = acl;
4912 spin_unlock(&inode->i_lock);
4913}
4914
4915static void nfs4_zap_acl_attr(struct inode *inode)
4916{
4917 nfs4_set_cached_acl(inode, NULL);
4918}
4919
4920static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4921{
4922 struct nfs_inode *nfsi = NFS_I(inode);
4923 struct nfs4_cached_acl *acl;
4924 int ret = -ENOENT;
4925
4926 spin_lock(&inode->i_lock);
4927 acl = nfsi->nfs4_acl;
4928 if (acl == NULL)
4929 goto out;
4930 if (buf == NULL) /* user is just asking for length */
4931 goto out_len;
4932 if (acl->cached == 0)
4933 goto out;
4934 ret = -ERANGE; /* see getxattr(2) man page */
4935 if (acl->len > buflen)
4936 goto out;
4937 memcpy(buf, acl->data, acl->len);
4938out_len:
4939 ret = acl->len;
4940out:
4941 spin_unlock(&inode->i_lock);
4942 return ret;
4943}
4944
Sachin Prabhu5794d212012-04-17 14:36:40 +01004945static 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 +00004946{
4947 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004948 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004949
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004950 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004951 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004952 if (acl == NULL)
4953 goto out;
4954 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004955 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004956 } else {
4957 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4958 if (acl == NULL)
4959 goto out;
4960 acl->cached = 0;
4961 }
4962 acl->len = acl_len;
4963out:
4964 nfs4_set_cached_acl(inode, acl);
4965}
4966
Andy Adamsonbf118a32011-12-07 11:55:27 -05004967/*
4968 * The getxattr API returns the required buffer length when called with a
4969 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4970 * the required buf. On a NULL buf, we send a page of data to the server
4971 * guessing that the ACL request can be serviced by a page. If so, we cache
4972 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4973 * the cache. If not so, we throw away the page, and cache the required
4974 * length. The next getxattr call will then produce another round trip to
4975 * the server, this time with the input buf of the required size.
4976 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004977static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004978{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004979 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004980 struct nfs_getaclargs args = {
4981 .fh = NFS_FH(inode),
4982 .acl_pages = pages,
4983 .acl_len = buflen,
4984 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004985 struct nfs_getaclres res = {
4986 .acl_len = buflen,
4987 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4990 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004991 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004992 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004993 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4994 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004995
Andy Adamsonbf118a32011-12-07 11:55:27 -05004996 /* As long as we're doing a round trip to the server anyway,
4997 * let's be prepared for a page of acl data. */
4998 if (npages == 0)
4999 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005000 if (npages > ARRAY_SIZE(pages))
5001 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005002
Andy Adamsonbf118a32011-12-07 11:55:27 -05005003 for (i = 0; i < npages; i++) {
5004 pages[i] = alloc_page(GFP_KERNEL);
5005 if (!pages[i])
5006 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005007 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005008
5009 /* for decoding across pages */
5010 res.acl_scratch = alloc_page(GFP_KERNEL);
5011 if (!res.acl_scratch)
5012 goto out_free;
5013
Andy Adamsonbf118a32011-12-07 11:55:27 -05005014 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005015
Peng Taode040be2012-01-10 22:42:47 +08005016 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005017 __func__, buf, buflen, npages, args.acl_len);
5018 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5019 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005020 if (ret)
5021 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005022
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005023 /* Handle the case where the passed-in buffer is too short */
5024 if (res.acl_flags & NFS4_ACL_TRUNC) {
5025 /* Did the user only issue a request for the acl length? */
5026 if (buf == NULL)
5027 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005028 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005029 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005030 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005031 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005032 if (buf) {
5033 if (res.acl_len > buflen) {
5034 ret = -ERANGE;
5035 goto out_free;
5036 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005037 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005038 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005039out_ok:
5040 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005041out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005042 for (i = 0; i < npages; i++)
5043 if (pages[i])
5044 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005045 if (res.acl_scratch)
5046 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047 return ret;
5048}
5049
Trond Myklebust16b42892006-08-24 12:27:15 -04005050static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5051{
5052 struct nfs4_exception exception = { };
5053 ssize_t ret;
5054 do {
5055 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005056 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005057 if (ret >= 0)
5058 break;
5059 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5060 } while (exception.retry);
5061 return ret;
5062}
5063
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5065{
5066 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005067 int ret;
5068
5069 if (!nfs4_server_supports_acls(server))
5070 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005071 ret = nfs_revalidate_inode(server, inode);
5072 if (ret < 0)
5073 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005074 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5075 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005076 ret = nfs4_read_cached_acl(inode, buf, buflen);
5077 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005078 /* -ENOENT is returned if there is no ACL or if there is an ACL
5079 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005080 return ret;
5081 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005082}
5083
Trond Myklebust16b42892006-08-24 12:27:15 -04005084static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005085{
5086 struct nfs_server *server = NFS_SERVER(inode);
5087 struct page *pages[NFS4ACL_MAXPAGES];
5088 struct nfs_setaclargs arg = {
5089 .fh = NFS_FH(inode),
5090 .acl_pages = pages,
5091 .acl_len = buflen,
5092 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005093 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5096 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005097 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005098 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005099 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005100 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005101
5102 if (!nfs4_server_supports_acls(server))
5103 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005104 if (npages > ARRAY_SIZE(pages))
5105 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005106 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005107 if (i < 0)
5108 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005109 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005110 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005111
5112 /*
5113 * Free each page after tx, so the only ref left is
5114 * held by the network stack
5115 */
5116 for (; i > 0; i--)
5117 put_page(pages[i-1]);
5118
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005119 /*
5120 * Acl update can result in inode attribute update.
5121 * so mark the attribute cache invalid.
5122 */
5123 spin_lock(&inode->i_lock);
5124 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5125 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005126 nfs_access_zap_cache(inode);
5127 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005128 return ret;
5129}
5130
Trond Myklebust16b42892006-08-24 12:27:15 -04005131static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5132{
5133 struct nfs4_exception exception = { };
5134 int err;
5135 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005136 err = __nfs4_proc_set_acl(inode, buf, buflen);
5137 trace_nfs4_set_acl(inode, err);
5138 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005139 &exception);
5140 } while (exception.retry);
5141 return err;
5142}
5143
David Quigleyaa9c2662013-05-22 12:50:44 -04005144#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5145static int _nfs4_get_security_label(struct inode *inode, void *buf,
5146 size_t buflen)
5147{
5148 struct nfs_server *server = NFS_SERVER(inode);
5149 struct nfs_fattr fattr;
5150 struct nfs4_label label = {0, 0, buflen, buf};
5151
5152 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005153 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005154 .fh = NFS_FH(inode),
5155 .bitmask = bitmask,
5156 };
5157 struct nfs4_getattr_res res = {
5158 .fattr = &fattr,
5159 .label = &label,
5160 .server = server,
5161 };
5162 struct rpc_message msg = {
5163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005164 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005165 .rpc_resp = &res,
5166 };
5167 int ret;
5168
5169 nfs_fattr_init(&fattr);
5170
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005172 if (ret)
5173 return ret;
5174 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5175 return -ENOENT;
5176 if (buflen < label.len)
5177 return -ERANGE;
5178 return 0;
5179}
5180
5181static int nfs4_get_security_label(struct inode *inode, void *buf,
5182 size_t buflen)
5183{
5184 struct nfs4_exception exception = { };
5185 int err;
5186
5187 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5188 return -EOPNOTSUPP;
5189
5190 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005191 err = _nfs4_get_security_label(inode, buf, buflen);
5192 trace_nfs4_get_security_label(inode, err);
5193 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005194 &exception);
5195 } while (exception.retry);
5196 return err;
5197}
5198
5199static int _nfs4_do_set_security_label(struct inode *inode,
5200 struct nfs4_label *ilabel,
5201 struct nfs_fattr *fattr,
5202 struct nfs4_label *olabel)
5203{
5204
5205 struct iattr sattr = {0};
5206 struct nfs_server *server = NFS_SERVER(inode);
5207 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005208 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005209 .fh = NFS_FH(inode),
5210 .iap = &sattr,
5211 .server = server,
5212 .bitmask = bitmask,
5213 .label = ilabel,
5214 };
5215 struct nfs_setattrres res = {
5216 .fattr = fattr,
5217 .label = olabel,
5218 .server = server,
5219 };
5220 struct rpc_message msg = {
5221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005222 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005223 .rpc_resp = &res,
5224 };
5225 int status;
5226
Jeff Layton12207f62013-11-01 10:49:32 -04005227 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005228
Jeff Layton12207f62013-11-01 10:49:32 -04005229 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005230 if (status)
5231 dprintk("%s failed: %d\n", __func__, status);
5232
5233 return status;
5234}
5235
5236static int nfs4_do_set_security_label(struct inode *inode,
5237 struct nfs4_label *ilabel,
5238 struct nfs_fattr *fattr,
5239 struct nfs4_label *olabel)
5240{
5241 struct nfs4_exception exception = { };
5242 int err;
5243
5244 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005245 err = _nfs4_do_set_security_label(inode, ilabel,
5246 fattr, olabel);
5247 trace_nfs4_set_security_label(inode, err);
5248 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005249 &exception);
5250 } while (exception.retry);
5251 return err;
5252}
5253
5254static int
Al Viro59301222016-05-27 10:19:30 -04005255nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005256{
5257 struct nfs4_label ilabel, *olabel = NULL;
5258 struct nfs_fattr fattr;
5259 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005260 int status;
5261
5262 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5263 return -EOPNOTSUPP;
5264
5265 nfs_fattr_init(&fattr);
5266
5267 ilabel.pi = 0;
5268 ilabel.lfs = 0;
5269 ilabel.label = (char *)buf;
5270 ilabel.len = buflen;
5271
5272 cred = rpc_lookup_cred();
5273 if (IS_ERR(cred))
5274 return PTR_ERR(cred);
5275
5276 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5277 if (IS_ERR(olabel)) {
5278 status = -PTR_ERR(olabel);
5279 goto out;
5280 }
5281
5282 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5283 if (status == 0)
5284 nfs_setsecurity(inode, &fattr, olabel);
5285
5286 nfs4_label_free(olabel);
5287out:
5288 put_rpccred(cred);
5289 return status;
5290}
5291#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5292
5293
Chuck Leverf0920752012-05-21 22:45:41 -04005294static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5295 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005296{
5297 __be32 verf[2];
5298
Chuck Lever2c820d92012-05-21 22:45:33 -04005299 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5300 /* An impossible timestamp guarantees this value
5301 * will never match a generated boot time. */
5302 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005303 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005304 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005305 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005306 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5307 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005308 }
Chuck Levercd937102012-03-02 17:14:31 -05005309 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5310}
5311
Jeff Laytona3192682015-06-09 19:43:59 -04005312static int
5313nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005314{
Jeff Laytona3192682015-06-09 19:43:59 -04005315 size_t len;
5316 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005317
Trond Myklebustceb3a162015-01-03 15:16:04 -05005318 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005319 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005320
Jeff Laytona3192682015-06-09 19:43:59 -04005321 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005322 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005323 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5324 1 +
5325 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5326 1;
5327 rcu_read_unlock();
5328
5329 if (len > NFS4_OPAQUE_LIMIT + 1)
5330 return -EINVAL;
5331
5332 /*
5333 * Since this string is allocated at mount time, and held until the
5334 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5335 * about a memory-reclaim deadlock.
5336 */
5337 str = kmalloc(len, GFP_KERNEL);
5338 if (!str)
5339 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005340
Chuck Levere984a552012-09-14 17:24:21 -04005341 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005342 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005343 clp->cl_ipaddr,
5344 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5345 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005346 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005347
Jeff Laytona3192682015-06-09 19:43:59 -04005348 clp->cl_owner_id = str;
5349 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005350}
5351
Jeff Layton873e3852015-06-09 19:44:00 -04005352static int
5353nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005354{
Jeff Layton873e3852015-06-09 19:44:00 -04005355 size_t len;
5356 char *str;
5357
5358 len = 10 + 10 + 1 + 10 + 1 +
5359 strlen(nfs4_client_id_uniquifier) + 1 +
5360 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5361
5362 if (len > NFS4_OPAQUE_LIMIT + 1)
5363 return -EINVAL;
5364
5365 /*
5366 * Since this string is allocated at mount time, and held until the
5367 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5368 * about a memory-reclaim deadlock.
5369 */
5370 str = kmalloc(len, GFP_KERNEL);
5371 if (!str)
5372 return -ENOMEM;
5373
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005374 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005375 clp->rpc_ops->version, clp->cl_minorversion,
5376 nfs4_client_id_uniquifier,
5377 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005378 clp->cl_owner_id = str;
5379 return 0;
5380}
5381
5382static int
5383nfs4_init_uniform_client_string(struct nfs_client *clp)
5384{
Jeff Layton873e3852015-06-09 19:44:00 -04005385 size_t len;
5386 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005387
5388 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005389 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005390
5391 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005392 return nfs4_init_uniquifier_client_string(clp);
5393
5394 len = 10 + 10 + 1 + 10 + 1 +
5395 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5396
5397 if (len > NFS4_OPAQUE_LIMIT + 1)
5398 return -EINVAL;
5399
5400 /*
5401 * Since this string is allocated at mount time, and held until the
5402 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5403 * about a memory-reclaim deadlock.
5404 */
5405 str = kmalloc(len, GFP_KERNEL);
5406 if (!str)
5407 return -ENOMEM;
5408
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005409 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005410 clp->rpc_ops->version, clp->cl_minorversion,
5411 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005412 clp->cl_owner_id = str;
5413 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005414}
5415
Chuck Lever706cb8d2014-03-12 12:51:47 -04005416/*
5417 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5418 * services. Advertise one based on the address family of the
5419 * clientaddr.
5420 */
5421static unsigned int
5422nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5423{
5424 if (strchr(clp->cl_ipaddr, ':') != NULL)
5425 return scnprintf(buf, len, "tcp6");
5426 else
5427 return scnprintf(buf, len, "tcp");
5428}
5429
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005430static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5431{
5432 struct nfs4_setclientid *sc = calldata;
5433
5434 if (task->tk_status == 0)
5435 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5436}
5437
5438static const struct rpc_call_ops nfs4_setclientid_ops = {
5439 .rpc_call_done = nfs4_setclientid_done,
5440};
5441
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005442/**
5443 * nfs4_proc_setclientid - Negotiate client ID
5444 * @clp: state data structure
5445 * @program: RPC program for NFSv4 callback service
5446 * @port: IP port number for NFS4 callback service
5447 * @cred: RPC credential to use for this call
5448 * @res: where to place the result
5449 *
5450 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5451 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005452int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5453 unsigned short port, struct rpc_cred *cred,
5454 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
5456 nfs4_verifier sc_verifier;
5457 struct nfs4_setclientid setclientid = {
5458 .sc_verifier = &sc_verifier,
5459 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005460 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 };
5462 struct rpc_message msg = {
5463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5464 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005465 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005466 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005468 struct rpc_task *task;
5469 struct rpc_task_setup task_setup_data = {
5470 .rpc_client = clp->cl_rpcclient,
5471 .rpc_message = &msg,
5472 .callback_ops = &nfs4_setclientid_ops,
5473 .callback_data = &setclientid,
5474 .flags = RPC_TASK_TIMEOUT,
5475 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005476 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Chuck Leverde734832012-07-11 16:30:50 -04005478 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005479 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005480
5481 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5482 status = nfs4_init_uniform_client_string(clp);
5483 else
Jeff Laytona3192682015-06-09 19:43:59 -04005484 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005485
5486 if (status)
5487 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005488
Chuck Leverde734832012-07-11 16:30:50 -04005489 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005490 setclientid.sc_netid_len =
5491 nfs4_init_callback_netid(clp,
5492 setclientid.sc_netid,
5493 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005494 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005495 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 clp->cl_ipaddr, port >> 8, port & 255);
5497
Jeff Layton3a6bb732015-06-09 19:43:57 -04005498 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005499 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005500 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005501 task = rpc_run_task(&task_setup_data);
5502 if (IS_ERR(task)) {
5503 status = PTR_ERR(task);
5504 goto out;
5505 }
5506 status = task->tk_status;
5507 if (setclientid.sc_cred) {
5508 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5509 put_rpccred(setclientid.sc_cred);
5510 }
5511 rpc_put_task(task);
5512out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005513 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005514 dprintk("NFS reply setclientid: %d\n", status);
5515 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516}
5517
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518/**
5519 * nfs4_proc_setclientid_confirm - Confirm client ID
5520 * @clp: state data structure
5521 * @res: result of a previous SETCLIENTID
5522 * @cred: RPC credential to use for this call
5523 *
5524 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5525 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005526int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005527 struct nfs4_setclientid_res *arg,
5528 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 struct rpc_message msg = {
5531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005532 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005533 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 int status;
5536
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005537 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5538 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5539 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005540 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005541 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005542 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 return status;
5544}
5545
Trond Myklebustfe650402006-01-03 09:55:18 +01005546struct nfs4_delegreturndata {
5547 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005548 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005549 struct nfs_fh fh;
5550 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005551 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005552 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005553 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005554 struct inode *inode;
5555 bool roc;
5556 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005557};
5558
Trond Myklebustfe650402006-01-03 09:55:18 +01005559static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5560{
5561 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005562
Trond Myklebust14516c32010-07-31 14:29:06 -04005563 if (!nfs4_sequence_done(task, &data->res.seq_res))
5564 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005565
Trond Myklebustca8acf82013-08-13 10:36:56 -04005566 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005567 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005568 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005569 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005570 case -NFS4ERR_ADMIN_REVOKED:
5571 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005572 case -NFS4ERR_EXPIRED:
5573 nfs4_free_revoked_stateid(data->res.server,
5574 data->args.stateid,
5575 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005576 case -NFS4ERR_BAD_STATEID:
5577 case -NFS4ERR_OLD_STATEID:
5578 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005579 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005580 if (data->roc)
5581 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005582 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005583 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005584 if (nfs4_async_handle_error(task, data->res.server,
5585 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005586 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005587 return;
5588 }
5589 }
5590 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005591}
5592
5593static void nfs4_delegreturn_release(void *calldata)
5594{
Peng Tao039b7562014-07-03 13:05:02 +08005595 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005596 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005597
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005598 if (inode) {
5599 if (data->roc)
5600 pnfs_roc_release(inode);
5601 nfs_iput_and_deactive(inode);
5602 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005603 kfree(calldata);
5604}
5605
Andy Adamson938e1012009-04-01 09:22:28 -04005606static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5607{
5608 struct nfs4_delegreturndata *d_data;
5609
5610 d_data = (struct nfs4_delegreturndata *)data;
5611
Peng Tao500d7012015-09-22 11:35:22 +08005612 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5613 return;
5614
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005615 if (d_data->roc)
5616 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005617
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005618 nfs4_setup_sequence(d_data->res.server,
5619 &d_data->args.seq_args,
5620 &d_data->res.seq_res,
5621 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005622}
Andy Adamson938e1012009-04-01 09:22:28 -04005623
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005624static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005625 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005626 .rpc_call_done = nfs4_delegreturn_done,
5627 .rpc_release = nfs4_delegreturn_release,
5628};
5629
Trond Myklebuste6f81072008-01-24 18:14:34 -05005630static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005631{
5632 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005633 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005634 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005635 struct rpc_message msg = {
5636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5637 .rpc_cred = cred,
5638 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005639 struct rpc_task_setup task_setup_data = {
5640 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005641 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005642 .callback_ops = &nfs4_delegreturn_ops,
5643 .flags = RPC_TASK_ASYNC,
5644 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005645 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005646
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005647 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005648 if (data == NULL)
5649 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005650 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005651
5652 nfs4_state_protect(server->nfs_client,
5653 NFS_SP4_MACH_CRED_CLEANUP,
5654 &task_setup_data.rpc_client, &msg);
5655
Trond Myklebustfe650402006-01-03 09:55:18 +01005656 data->args.fhandle = &data->fh;
5657 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005658 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005659 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005660 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005661 data->res.fattr = &data->fattr;
5662 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005663 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005664 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005665 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005666 data->inode = nfs_igrab_and_active(inode);
5667 if (data->inode)
5668 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005669
Trond Myklebustc970aa82007-07-14 15:39:59 -04005670 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005671 msg.rpc_argp = &data->args;
5672 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005673 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005674 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005675 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005676 if (!issync)
5677 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005678 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005679 if (status != 0)
5680 goto out;
5681 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005682 if (status == 0)
5683 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5684 else
5685 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005686out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005687 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005688 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
5690
Trond Myklebuste6f81072008-01-24 18:14:34 -05005691int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
5693 struct nfs_server *server = NFS_SERVER(inode);
5694 struct nfs4_exception exception = { };
5695 int err;
5696 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005697 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005698 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 switch (err) {
5700 case -NFS4ERR_STALE_STATEID:
5701 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 case 0:
5703 return 0;
5704 }
5705 err = nfs4_handle_exception(server, err, &exception);
5706 } while (exception.retry);
5707 return err;
5708}
5709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5711{
5712 struct inode *inode = state->inode;
5713 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005714 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005715 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005717 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005719 struct nfs_lockt_res res = {
5720 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 };
5722 struct rpc_message msg = {
5723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5724 .rpc_argp = &arg,
5725 .rpc_resp = &res,
5726 .rpc_cred = state->owner->so_cred,
5727 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 struct nfs4_lock_state *lsp;
5729 int status;
5730
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005731 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005732 status = nfs4_set_lock_state(state, request);
5733 if (status != 0)
5734 goto out;
5735 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005736 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005737 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005738 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005739 switch (status) {
5740 case 0:
5741 request->fl_type = F_UNLCK;
5742 break;
5743 case -NFS4ERR_DENIED:
5744 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005746 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005747 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005748out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 return status;
5750}
5751
5752static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5753{
5754 struct nfs4_exception exception = { };
5755 int err;
5756
5757 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005758 err = _nfs4_proc_getlk(state, cmd, request);
5759 trace_nfs4_get_lock(request, state, cmd, err);
5760 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 &exception);
5762 } while (exception.retry);
5763 return err;
5764}
5765
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005766struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005767 struct nfs_locku_args arg;
5768 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005769 struct nfs4_lock_state *lsp;
5770 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005771 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005772 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005773 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005774};
5775
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005776static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5777 struct nfs_open_context *ctx,
5778 struct nfs4_lock_state *lsp,
5779 struct nfs_seqid *seqid)
5780{
5781 struct nfs4_unlockdata *p;
5782 struct inode *inode = lsp->ls_state->inode;
5783
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005784 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005785 if (p == NULL)
5786 return NULL;
5787 p->arg.fh = NFS_FH(inode);
5788 p->arg.fl = &p->fl;
5789 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005790 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005791 p->lsp = lsp;
5792 atomic_inc(&lsp->ls_count);
5793 /* Ensure we don't close file until we're done freeing locks! */
5794 p->ctx = get_nfs_open_context(ctx);
5795 memcpy(&p->fl, fl, sizeof(p->fl));
5796 p->server = NFS_SERVER(inode);
5797 return p;
5798}
5799
Trond Myklebust06f814a2006-01-03 09:55:07 +01005800static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005801{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005802 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005803 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005804 nfs4_put_lock_state(calldata->lsp);
5805 put_nfs_open_context(calldata->ctx);
5806 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005807}
5808
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005809static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005810{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005811 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005812
Trond Myklebust14516c32010-07-31 14:29:06 -04005813 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5814 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005815 switch (task->tk_status) {
5816 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005817 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005818 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005819 if (nfs4_update_lock_stateid(calldata->lsp,
5820 &calldata->res.stateid))
5821 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005822 case -NFS4ERR_ADMIN_REVOKED:
5823 case -NFS4ERR_EXPIRED:
5824 nfs4_free_revoked_stateid(calldata->server,
5825 &calldata->arg.stateid,
5826 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005827 case -NFS4ERR_BAD_STATEID:
5828 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005830 if (!nfs4_stateid_match(&calldata->arg.stateid,
5831 &calldata->lsp->ls_stateid))
5832 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005833 break;
5834 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005835 if (nfs4_async_handle_error(task, calldata->server,
5836 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005837 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005838 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005839 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005840}
5841
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005842static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005843{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005844 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005846 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005847 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005848 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005849 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005850 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005851 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005853 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005854 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005855 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005856 &calldata->res.seq_res,
5857 task) != 0)
5858 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005859 return;
5860out_no_action:
5861 task->tk_action = NULL;
5862out_wait:
5863 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864}
5865
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005866static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005867 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005868 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005869 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005870};
5871
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005872static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5873 struct nfs_open_context *ctx,
5874 struct nfs4_lock_state *lsp,
5875 struct nfs_seqid *seqid)
5876{
5877 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005878 struct rpc_message msg = {
5879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5880 .rpc_cred = ctx->cred,
5881 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005882 struct rpc_task_setup task_setup_data = {
5883 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005884 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005885 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005886 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005887 .flags = RPC_TASK_ASYNC,
5888 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005889
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005890 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5891 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5892
Frank Filz137d6ac2007-07-09 15:32:29 -07005893 /* Ensure this is an unlock - when canceling a lock, the
5894 * canceled lock is passed in, and it won't be an unlock.
5895 */
5896 fl->fl_type = F_UNLCK;
5897
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005898 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5899 if (data == NULL) {
5900 nfs_free_seqid(seqid);
5901 return ERR_PTR(-ENOMEM);
5902 }
5903
Chuck Levera9c92d62013-08-09 12:48:18 -04005904 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005905 msg.rpc_argp = &data->arg;
5906 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005907 task_setup_data.callback_data = data;
5908 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005909}
5910
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5912{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005913 struct inode *inode = state->inode;
5914 struct nfs4_state_owner *sp = state->owner;
5915 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005916 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005917 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005918 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005919 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005920 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005921 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Trond Myklebust9b073572006-06-29 16:38:34 -04005923 status = nfs4_set_lock_state(state, request);
5924 /* Unlock _before_ we do the RPC call */
5925 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005926 /* Exclude nfs_delegation_claim_locks() */
5927 mutex_lock(&sp->so_delegreturn_mutex);
5928 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005929 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005930 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005931 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005932 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005933 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005934 }
5935 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005936 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005937 if (status != 0)
5938 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005939 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005940 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005941 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5942 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005943 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5944 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005945 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005946 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005947 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005948 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949 status = PTR_ERR(task);
5950 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005951 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005952 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005953 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005954out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005955 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005956 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005957 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958}
5959
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005960struct nfs4_lockdata {
5961 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005962 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963 struct nfs4_lock_state *lsp;
5964 struct nfs_open_context *ctx;
5965 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005966 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005967 int rpc_status;
5968 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005969 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005970};
5971
5972static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005973 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5974 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005975{
5976 struct nfs4_lockdata *p;
5977 struct inode *inode = lsp->ls_state->inode;
5978 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005979 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005980
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005981 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005982 if (p == NULL)
5983 return NULL;
5984
5985 p->arg.fh = NFS_FH(inode);
5986 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005987 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005988 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005989 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005990 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5991 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005992 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005993 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005994 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005995 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005996 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005997 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005998 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005999 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000 atomic_inc(&lsp->ls_count);
6001 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006002 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003 memcpy(&p->fl, fl, sizeof(p->fl));
6004 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006005out_free_seqid:
6006 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007out_free:
6008 kfree(p);
6009 return NULL;
6010}
6011
6012static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6013{
6014 struct nfs4_lockdata *data = calldata;
6015 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Harvey Harrison3110ff82008-05-02 13:42:44 -07006017 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006018 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006019 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006021 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006022 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006023 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006024 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006025 nfs4_stateid_copy(&data->arg.open_stateid,
6026 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006027 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006028 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006029 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006030 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006031 nfs4_stateid_copy(&data->arg.lock_stateid,
6032 &data->lsp->ls_stateid);
6033 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006034 if (!nfs4_valid_open_stateid(state)) {
6035 data->rpc_status = -EBADF;
6036 task->tk_action = NULL;
6037 goto out_release_open_seqid;
6038 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006039 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006040 if (nfs4_setup_sequence(data->server,
6041 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006042 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006043 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006044 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006045out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006046 nfs_release_seqid(data->arg.open_seqid);
6047out_release_lock_seqid:
6048 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006049out_wait:
6050 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006051 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006052}
6053
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006054static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6055{
6056 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006057 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006058
Harvey Harrison3110ff82008-05-02 13:42:44 -07006059 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006060
Trond Myklebust14516c32010-07-31 14:29:06 -04006061 if (!nfs4_sequence_done(task, &data->res.seq_res))
6062 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006063
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006065 switch (task->tk_status) {
6066 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006067 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006068 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006069 if (data->arg.new_lock) {
6070 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006071 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006072 rpc_restart_call_prepare(task);
6073 break;
6074 }
6075 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006076 if (data->arg.new_lock_owner != 0) {
6077 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6078 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6079 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6080 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6081 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006082 break;
6083 case -NFS4ERR_BAD_STATEID:
6084 case -NFS4ERR_OLD_STATEID:
6085 case -NFS4ERR_STALE_STATEID:
6086 case -NFS4ERR_EXPIRED:
6087 if (data->arg.new_lock_owner != 0) {
6088 if (!nfs4_stateid_match(&data->arg.open_stateid,
6089 &lsp->ls_state->open_stateid))
6090 rpc_restart_call_prepare(task);
6091 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6092 &lsp->ls_stateid))
6093 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006095 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096}
6097
6098static void nfs4_lock_release(void *calldata)
6099{
6100 struct nfs4_lockdata *data = calldata;
6101
Harvey Harrison3110ff82008-05-02 13:42:44 -07006102 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006103 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104 if (data->cancelled != 0) {
6105 struct rpc_task *task;
6106 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6107 data->arg.lock_seqid);
6108 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006109 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006110 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006111 } else
6112 nfs_free_seqid(data->arg.lock_seqid);
6113 nfs4_put_lock_state(data->lsp);
6114 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006115 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006116 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006117 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006118}
6119
6120static const struct rpc_call_ops nfs4_lock_ops = {
6121 .rpc_call_prepare = nfs4_lock_prepare,
6122 .rpc_call_done = nfs4_lock_done,
6123 .rpc_release = nfs4_lock_release,
6124};
6125
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006126static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6127{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006128 switch (error) {
6129 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006130 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006131 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006132 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006133 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006134 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006135 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006136 break;
6137 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006138 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006139 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006140 };
6141}
6142
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006143static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006144{
6145 struct nfs4_lockdata *data;
6146 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006147 struct rpc_message msg = {
6148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6149 .rpc_cred = state->owner->so_cred,
6150 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006151 struct rpc_task_setup task_setup_data = {
6152 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006153 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006154 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006155 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006156 .flags = RPC_TASK_ASYNC,
6157 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006158 int ret;
6159
Harvey Harrison3110ff82008-05-02 13:42:44 -07006160 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006161 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006162 fl->fl_u.nfs4_fl.owner,
6163 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006164 if (data == NULL)
6165 return -ENOMEM;
6166 if (IS_SETLKW(cmd))
6167 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006168 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006169 msg.rpc_argp = &data->arg;
6170 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006171 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006172 if (recovery_type > NFS_LOCK_NEW) {
6173 if (recovery_type == NFS_LOCK_RECLAIM)
6174 data->arg.reclaim = NFS_LOCK_RECLAIM;
6175 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006176 } else
6177 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006178 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006179 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006180 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006181 ret = nfs4_wait_for_completion_rpc_task(task);
6182 if (ret == 0) {
6183 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006184 if (ret)
6185 nfs4_handle_setlk_error(data->server, data->lsp,
6186 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006187 } else
6188 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006189 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006190 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006191 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193}
6194
6195static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6196{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006197 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006198 struct nfs4_exception exception = {
6199 .inode = state->inode,
6200 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006201 int err;
6202
6203 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006204 /* Cache the lock if possible... */
6205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6206 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006207 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006208 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006209 break;
6210 nfs4_handle_exception(server, err, &exception);
6211 } while (exception.retry);
6212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213}
6214
6215static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6216{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006217 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006218 struct nfs4_exception exception = {
6219 .inode = state->inode,
6220 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006221 int err;
6222
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006223 err = nfs4_set_lock_state(state, request);
6224 if (err != 0)
6225 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006226 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006227 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6228 return 0;
6229 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006230 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006231 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6232 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006233 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006234 switch (err) {
6235 default:
6236 goto out;
6237 case -NFS4ERR_GRACE:
6238 case -NFS4ERR_DELAY:
6239 nfs4_handle_exception(server, err, &exception);
6240 err = 0;
6241 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006242 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006243out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245}
6246
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006247#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006248static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6249{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006250 struct nfs4_lock_state *lsp;
6251 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006252
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006253 status = nfs4_set_lock_state(state, request);
6254 if (status != 0)
6255 return status;
6256 lsp = request->fl_u.nfs4_fl.owner;
6257 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6258 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6259 return 0;
6260 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006261 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006262}
6263#endif
6264
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6266{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006267 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006268 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006269 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006270 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
Trond Myklebust01c3b862006-06-29 16:38:39 -04006272 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006273 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006274 if (status < 0)
6275 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006276 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006277 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006278 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006279 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006280 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006281 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006282 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006283 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006284 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006285 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006286 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006287 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006288 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006289 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006290out:
6291 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 return status;
6293}
6294
6295static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6296{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006297 struct nfs4_exception exception = {
6298 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006299 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006300 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 int err;
6302
6303 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006304 err = _nfs4_proc_setlk(state, cmd, request);
6305 if (err == -NFS4ERR_DENIED)
6306 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006308 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 } while (exception.retry);
6310 return err;
6311}
6312
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006313#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6314#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6315
6316static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006317nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6318 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006319{
6320 int status = -ERESTARTSYS;
6321 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6322
6323 while(!signalled()) {
6324 status = nfs4_proc_setlk(state, cmd, request);
6325 if ((status != -EAGAIN) || IS_SETLK(cmd))
6326 break;
6327 freezable_schedule_timeout_interruptible(timeout);
6328 timeout *= 2;
6329 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6330 status = -ERESTARTSYS;
6331 }
6332 return status;
6333}
6334
Jeff Laytona1d617d82016-09-17 18:17:39 -04006335#ifdef CONFIG_NFS_V4_1
6336struct nfs4_lock_waiter {
6337 struct task_struct *task;
6338 struct inode *inode;
6339 struct nfs_lowner *owner;
6340 bool notified;
6341};
6342
6343static int
6344nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6345{
6346 int ret;
6347 struct cb_notify_lock_args *cbnl = key;
6348 struct nfs4_lock_waiter *waiter = wait->private;
6349 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6350 *wowner = waiter->owner;
6351
6352 /* Only wake if the callback was for the same owner */
6353 if (lowner->clientid != wowner->clientid ||
6354 lowner->id != wowner->id ||
6355 lowner->s_dev != wowner->s_dev)
6356 return 0;
6357
6358 /* Make sure it's for the right inode */
6359 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6360 return 0;
6361
6362 waiter->notified = true;
6363
6364 /* override "private" so we can use default_wake_function */
6365 wait->private = waiter->task;
6366 ret = autoremove_wake_function(wait, mode, flags, key);
6367 wait->private = waiter;
6368 return ret;
6369}
6370
6371static int
6372nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6373{
6374 int status = -ERESTARTSYS;
6375 unsigned long flags;
6376 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6377 struct nfs_server *server = NFS_SERVER(state->inode);
6378 struct nfs_client *clp = server->nfs_client;
6379 wait_queue_head_t *q = &clp->cl_lock_waitq;
6380 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6381 .id = lsp->ls_seqid.owner_id,
6382 .s_dev = server->s_dev };
6383 struct nfs4_lock_waiter waiter = { .task = current,
6384 .inode = state->inode,
6385 .owner = &owner,
6386 .notified = false };
6387 wait_queue_t wait;
6388
6389 /* Don't bother with waitqueue if we don't expect a callback */
6390 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6391 return nfs4_retry_setlk_simple(state, cmd, request);
6392
6393 init_wait(&wait);
6394 wait.private = &waiter;
6395 wait.func = nfs4_wake_lock_waiter;
6396 add_wait_queue(q, &wait);
6397
6398 while(!signalled()) {
6399 status = nfs4_proc_setlk(state, cmd, request);
6400 if ((status != -EAGAIN) || IS_SETLK(cmd))
6401 break;
6402
6403 status = -ERESTARTSYS;
6404 spin_lock_irqsave(&q->lock, flags);
6405 if (waiter.notified) {
6406 spin_unlock_irqrestore(&q->lock, flags);
6407 continue;
6408 }
6409 set_current_state(TASK_INTERRUPTIBLE);
6410 spin_unlock_irqrestore(&q->lock, flags);
6411
6412 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6413 }
6414
6415 finish_wait(q, &wait);
6416 return status;
6417}
6418#else /* !CONFIG_NFS_V4_1 */
6419static inline int
6420nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6421{
6422 return nfs4_retry_setlk_simple(state, cmd, request);
6423}
6424#endif
6425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426static int
6427nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6428{
6429 struct nfs_open_context *ctx;
6430 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 int status;
6432
6433 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006434 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 state = ctx->state;
6436
6437 if (request->fl_start < 0 || request->fl_end < 0)
6438 return -EINVAL;
6439
Trond Myklebustd9531262009-07-21 19:22:38 -04006440 if (IS_GETLK(cmd)) {
6441 if (state != NULL)
6442 return nfs4_proc_getlk(state, F_GETLK, request);
6443 return 0;
6444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445
6446 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6447 return -EINVAL;
6448
Trond Myklebustd9531262009-07-21 19:22:38 -04006449 if (request->fl_type == F_UNLCK) {
6450 if (state != NULL)
6451 return nfs4_proc_unlck(state, cmd, request);
6452 return 0;
6453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
Trond Myklebustd9531262009-07-21 19:22:38 -04006455 if (state == NULL)
6456 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006457
6458 if ((request->fl_flags & FL_POSIX) &&
6459 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6460 return -ENOLCK;
6461
Trond Myklebust55725512012-04-18 12:48:35 -04006462 /*
6463 * Don't rely on the VFS having checked the file open mode,
6464 * since it won't do this for flock() locks.
6465 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006466 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006467 case F_RDLCK:
6468 if (!(filp->f_mode & FMODE_READ))
6469 return -EBADF;
6470 break;
6471 case F_WRLCK:
6472 if (!(filp->f_mode & FMODE_WRITE))
6473 return -EBADF;
6474 }
6475
Jeff Layton1ea67db2016-09-17 18:17:37 -04006476 status = nfs4_set_lock_state(state, request);
6477 if (status != 0)
6478 return status;
6479
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006480 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481}
6482
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006483int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006484{
6485 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006486 int err;
6487
6488 err = nfs4_set_lock_state(state, fl);
6489 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006490 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006491 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006492 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006493}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006494
Trond Myklebustcf470c32012-03-07 13:49:12 -05006495struct nfs_release_lockowner_data {
6496 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006497 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006498 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006499 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006500 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006501};
6502
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006503static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6504{
6505 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006506 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006507 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6508 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006509 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006510 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006511}
6512
6513static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6514{
6515 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006516 struct nfs_server *server = data->server;
6517
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006518 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006519
6520 switch (task->tk_status) {
6521 case 0:
6522 renew_lease(server, data->timestamp);
6523 break;
6524 case -NFS4ERR_STALE_CLIENTID:
6525 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006526 nfs4_schedule_lease_recovery(server->nfs_client);
6527 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006528 case -NFS4ERR_LEASE_MOVED:
6529 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006530 if (nfs4_async_handle_error(task, server,
6531 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006532 rpc_restart_call_prepare(task);
6533 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006534}
6535
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006536static void nfs4_release_lockowner_release(void *calldata)
6537{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006538 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006539 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006540 kfree(calldata);
6541}
6542
Trond Myklebust17280172012-03-11 13:11:00 -04006543static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006544 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6545 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006546 .rpc_release = nfs4_release_lockowner_release,
6547};
6548
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006549static void
6550nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006551{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006552 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006553 struct rpc_message msg = {
6554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6555 };
6556
6557 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006558 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006559
Trond Myklebustcf470c32012-03-07 13:49:12 -05006560 data = kmalloc(sizeof(*data), GFP_NOFS);
6561 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006562 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006563 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006564 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006565 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6566 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6567 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006568
Trond Myklebustcf470c32012-03-07 13:49:12 -05006569 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006570 msg.rpc_resp = &data->res;
6571 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006572 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006573}
6574
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006575#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6576
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006577static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006578 struct dentry *unused, struct inode *inode,
6579 const char *key, const void *buf,
6580 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006581{
Al Viro59301222016-05-27 10:19:30 -04006582 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006583}
6584
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006585static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006586 struct dentry *unused, struct inode *inode,
6587 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006588{
Al Virob2968212016-04-10 20:48:24 -04006589 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006590}
6591
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006592static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006593{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006594 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006595}
6596
David Quigleyc9bccef2013-05-22 12:50:45 -04006597#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006598
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006599static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006600 struct dentry *unused, struct inode *inode,
6601 const char *key, const void *buf,
6602 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006603{
6604 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006605 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006606
6607 return -EOPNOTSUPP;
6608}
6609
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006610static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006611 struct dentry *unused, struct inode *inode,
6612 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006613{
6614 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006615 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006616 return -EOPNOTSUPP;
6617}
6618
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006619static ssize_t
6620nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006621{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006622 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006623
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006624 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6625 len = security_inode_listsecurity(inode, list, list_len);
6626 if (list_len && len > list_len)
6627 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006628 }
6629 return len;
6630}
6631
6632static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6633 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006634 .get = nfs4_xattr_get_nfs4_label,
6635 .set = nfs4_xattr_set_nfs4_label,
6636};
David Quigleyc9bccef2013-05-22 12:50:45 -04006637
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006638#else
6639
6640static ssize_t
6641nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6642{
6643 return 0;
6644}
6645
6646#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006647
Andy Adamson533eb462011-06-13 18:25:56 -04006648/*
6649 * nfs_fhget will use either the mounted_on_fileid or the fileid
6650 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006651static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6652{
Andy Adamson533eb462011-06-13 18:25:56 -04006653 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6654 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6655 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006656 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006657 return;
6658
6659 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006660 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006661 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6662 fattr->nlink = 2;
6663}
6664
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006665static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6666 const struct qstr *name,
6667 struct nfs4_fs_locations *fs_locations,
6668 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006669{
6670 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006671 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006672 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006673 };
6674 struct nfs4_fs_locations_arg args = {
6675 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006676 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006677 .page = page,
6678 .bitmask = bitmask,
6679 };
Benny Halevy22958462009-04-01 09:22:02 -04006680 struct nfs4_fs_locations_res res = {
6681 .fs_locations = fs_locations,
6682 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006683 struct rpc_message msg = {
6684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6685 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006686 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006687 };
6688 int status;
6689
Harvey Harrison3110ff82008-05-02 13:42:44 -07006690 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006691
6692 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6693 * is not supported */
6694 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6695 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6696 else
6697 bitmask[0] |= FATTR4_WORD0_FILEID;
6698
Trond Myklebustc228fd32007-01-13 02:28:11 -05006699 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006700 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006701 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006702 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006703 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006704 return status;
6705}
6706
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006707int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6708 const struct qstr *name,
6709 struct nfs4_fs_locations *fs_locations,
6710 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006711{
6712 struct nfs4_exception exception = { };
6713 int err;
6714 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006715 err = _nfs4_proc_fs_locations(client, dir, name,
6716 fs_locations, page);
6717 trace_nfs4_get_fs_locations(dir, name, err);
6718 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006719 &exception);
6720 } while (exception.retry);
6721 return err;
6722}
6723
Chuck Leverb03d7352013-10-17 14:12:50 -04006724/*
6725 * This operation also signals the server that this client is
6726 * performing migration recovery. The server can stop returning
6727 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6728 * appended to this compound to identify the client ID which is
6729 * performing recovery.
6730 */
6731static int _nfs40_proc_get_locations(struct inode *inode,
6732 struct nfs4_fs_locations *locations,
6733 struct page *page, struct rpc_cred *cred)
6734{
6735 struct nfs_server *server = NFS_SERVER(inode);
6736 struct rpc_clnt *clnt = server->client;
6737 u32 bitmask[2] = {
6738 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6739 };
6740 struct nfs4_fs_locations_arg args = {
6741 .clientid = server->nfs_client->cl_clientid,
6742 .fh = NFS_FH(inode),
6743 .page = page,
6744 .bitmask = bitmask,
6745 .migration = 1, /* skip LOOKUP */
6746 .renew = 1, /* append RENEW */
6747 };
6748 struct nfs4_fs_locations_res res = {
6749 .fs_locations = locations,
6750 .migration = 1,
6751 .renew = 1,
6752 };
6753 struct rpc_message msg = {
6754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6755 .rpc_argp = &args,
6756 .rpc_resp = &res,
6757 .rpc_cred = cred,
6758 };
6759 unsigned long now = jiffies;
6760 int status;
6761
6762 nfs_fattr_init(&locations->fattr);
6763 locations->server = server;
6764 locations->nlocations = 0;
6765
6766 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6767 nfs4_set_sequence_privileged(&args.seq_args);
6768 status = nfs4_call_sync_sequence(clnt, server, &msg,
6769 &args.seq_args, &res.seq_res);
6770 if (status)
6771 return status;
6772
6773 renew_lease(server, now);
6774 return 0;
6775}
6776
6777#ifdef CONFIG_NFS_V4_1
6778
6779/*
6780 * This operation also signals the server that this client is
6781 * performing migration recovery. The server can stop asserting
6782 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6783 * performing this operation is identified in the SEQUENCE
6784 * operation in this compound.
6785 *
6786 * When the client supports GETATTR(fs_locations_info), it can
6787 * be plumbed in here.
6788 */
6789static int _nfs41_proc_get_locations(struct inode *inode,
6790 struct nfs4_fs_locations *locations,
6791 struct page *page, struct rpc_cred *cred)
6792{
6793 struct nfs_server *server = NFS_SERVER(inode);
6794 struct rpc_clnt *clnt = server->client;
6795 u32 bitmask[2] = {
6796 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6797 };
6798 struct nfs4_fs_locations_arg args = {
6799 .fh = NFS_FH(inode),
6800 .page = page,
6801 .bitmask = bitmask,
6802 .migration = 1, /* skip LOOKUP */
6803 };
6804 struct nfs4_fs_locations_res res = {
6805 .fs_locations = locations,
6806 .migration = 1,
6807 };
6808 struct rpc_message msg = {
6809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6810 .rpc_argp = &args,
6811 .rpc_resp = &res,
6812 .rpc_cred = cred,
6813 };
6814 int status;
6815
6816 nfs_fattr_init(&locations->fattr);
6817 locations->server = server;
6818 locations->nlocations = 0;
6819
6820 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6821 nfs4_set_sequence_privileged(&args.seq_args);
6822 status = nfs4_call_sync_sequence(clnt, server, &msg,
6823 &args.seq_args, &res.seq_res);
6824 if (status == NFS4_OK &&
6825 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6826 status = -NFS4ERR_LEASE_MOVED;
6827 return status;
6828}
6829
6830#endif /* CONFIG_NFS_V4_1 */
6831
6832/**
6833 * nfs4_proc_get_locations - discover locations for a migrated FSID
6834 * @inode: inode on FSID that is migrating
6835 * @locations: result of query
6836 * @page: buffer
6837 * @cred: credential to use for this operation
6838 *
6839 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6840 * operation failed, or a negative errno if a local error occurred.
6841 *
6842 * On success, "locations" is filled in, but if the server has
6843 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6844 * asserted.
6845 *
6846 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6847 * from this client that require migration recovery.
6848 */
6849int nfs4_proc_get_locations(struct inode *inode,
6850 struct nfs4_fs_locations *locations,
6851 struct page *page, struct rpc_cred *cred)
6852{
6853 struct nfs_server *server = NFS_SERVER(inode);
6854 struct nfs_client *clp = server->nfs_client;
6855 const struct nfs4_mig_recovery_ops *ops =
6856 clp->cl_mvops->mig_recovery_ops;
6857 struct nfs4_exception exception = { };
6858 int status;
6859
6860 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6861 (unsigned long long)server->fsid.major,
6862 (unsigned long long)server->fsid.minor,
6863 clp->cl_hostname);
6864 nfs_display_fhandle(NFS_FH(inode), __func__);
6865
6866 do {
6867 status = ops->get_locations(inode, locations, page, cred);
6868 if (status != -NFS4ERR_DELAY)
6869 break;
6870 nfs4_handle_exception(server, status, &exception);
6871 } while (exception.retry);
6872 return status;
6873}
6874
Chuck Lever44c99932013-10-17 14:13:30 -04006875/*
6876 * This operation also signals the server that this client is
6877 * performing "lease moved" recovery. The server can stop
6878 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6879 * is appended to this compound to identify the client ID which is
6880 * performing recovery.
6881 */
6882static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6883{
6884 struct nfs_server *server = NFS_SERVER(inode);
6885 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6886 struct rpc_clnt *clnt = server->client;
6887 struct nfs4_fsid_present_arg args = {
6888 .fh = NFS_FH(inode),
6889 .clientid = clp->cl_clientid,
6890 .renew = 1, /* append RENEW */
6891 };
6892 struct nfs4_fsid_present_res res = {
6893 .renew = 1,
6894 };
6895 struct rpc_message msg = {
6896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6897 .rpc_argp = &args,
6898 .rpc_resp = &res,
6899 .rpc_cred = cred,
6900 };
6901 unsigned long now = jiffies;
6902 int status;
6903
6904 res.fh = nfs_alloc_fhandle();
6905 if (res.fh == NULL)
6906 return -ENOMEM;
6907
6908 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6909 nfs4_set_sequence_privileged(&args.seq_args);
6910 status = nfs4_call_sync_sequence(clnt, server, &msg,
6911 &args.seq_args, &res.seq_res);
6912 nfs_free_fhandle(res.fh);
6913 if (status)
6914 return status;
6915
6916 do_renew_lease(clp, now);
6917 return 0;
6918}
6919
6920#ifdef CONFIG_NFS_V4_1
6921
6922/*
6923 * This operation also signals the server that this client is
6924 * performing "lease moved" recovery. The server can stop asserting
6925 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6926 * this operation is identified in the SEQUENCE operation in this
6927 * compound.
6928 */
6929static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6930{
6931 struct nfs_server *server = NFS_SERVER(inode);
6932 struct rpc_clnt *clnt = server->client;
6933 struct nfs4_fsid_present_arg args = {
6934 .fh = NFS_FH(inode),
6935 };
6936 struct nfs4_fsid_present_res res = {
6937 };
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6940 .rpc_argp = &args,
6941 .rpc_resp = &res,
6942 .rpc_cred = cred,
6943 };
6944 int status;
6945
6946 res.fh = nfs_alloc_fhandle();
6947 if (res.fh == NULL)
6948 return -ENOMEM;
6949
6950 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6951 nfs4_set_sequence_privileged(&args.seq_args);
6952 status = nfs4_call_sync_sequence(clnt, server, &msg,
6953 &args.seq_args, &res.seq_res);
6954 nfs_free_fhandle(res.fh);
6955 if (status == NFS4_OK &&
6956 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6957 status = -NFS4ERR_LEASE_MOVED;
6958 return status;
6959}
6960
6961#endif /* CONFIG_NFS_V4_1 */
6962
6963/**
6964 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6965 * @inode: inode on FSID to check
6966 * @cred: credential to use for this operation
6967 *
6968 * Server indicates whether the FSID is present, moved, or not
6969 * recognized. This operation is necessary to clear a LEASE_MOVED
6970 * condition for this client ID.
6971 *
6972 * Returns NFS4_OK if the FSID is present on this server,
6973 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6974 * NFS4ERR code if some error occurred on the server, or a
6975 * negative errno if a local failure occurred.
6976 */
6977int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6978{
6979 struct nfs_server *server = NFS_SERVER(inode);
6980 struct nfs_client *clp = server->nfs_client;
6981 const struct nfs4_mig_recovery_ops *ops =
6982 clp->cl_mvops->mig_recovery_ops;
6983 struct nfs4_exception exception = { };
6984 int status;
6985
6986 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6987 (unsigned long long)server->fsid.major,
6988 (unsigned long long)server->fsid.minor,
6989 clp->cl_hostname);
6990 nfs_display_fhandle(NFS_FH(inode), __func__);
6991
6992 do {
6993 status = ops->fsid_present(inode, cred);
6994 if (status != -NFS4ERR_DELAY)
6995 break;
6996 nfs4_handle_exception(server, status, &exception);
6997 } while (exception.retry);
6998 return status;
6999}
7000
Andy Adamson5ec16a82013-08-08 10:57:55 -04007001/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007002 * If 'use_integrity' is true and the state managment nfs_client
7003 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7004 * and the machine credential as per RFC3530bis and RFC5661 Security
7005 * Considerations sections. Otherwise, just use the user cred with the
7006 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007007 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007008static 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 +00007009{
7010 int status;
7011 struct nfs4_secinfo_arg args = {
7012 .dir_fh = NFS_FH(dir),
7013 .name = name,
7014 };
7015 struct nfs4_secinfo_res res = {
7016 .flavors = flavors,
7017 };
7018 struct rpc_message msg = {
7019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7020 .rpc_argp = &args,
7021 .rpc_resp = &res,
7022 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007023 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007024 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007025
7026 if (use_integrity) {
7027 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007028 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7029 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007030 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007031
7032 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007033
7034 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7035 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7036
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007037 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7038 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007039 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007040
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007041 if (cred)
7042 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007043
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007044 return status;
7045}
7046
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007047int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7048 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007049{
7050 struct nfs4_exception exception = { };
7051 int err;
7052 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007053 err = -NFS4ERR_WRONGSEC;
7054
7055 /* try to use integrity protection with machine cred */
7056 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7057 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7058
7059 /*
7060 * if unable to use integrity protection, or SECINFO with
7061 * integrity protection returns NFS4ERR_WRONGSEC (which is
7062 * disallowed by spec, but exists in deployed servers) use
7063 * the current filesystem's rpc_client and the user cred.
7064 */
7065 if (err == -NFS4ERR_WRONGSEC)
7066 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7067
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007068 trace_nfs4_secinfo(dir, name, err);
7069 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007070 &exception);
7071 } while (exception.retry);
7072 return err;
7073}
7074
Andy Adamson557134a2009-04-01 09:21:53 -04007075#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007076/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007077 * Check the exchange flags returned by the server for invalid flags, having
7078 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7079 * DS flags set.
7080 */
7081static int nfs4_check_cl_exchange_flags(u32 flags)
7082{
7083 if (flags & ~EXCHGID4_FLAG_MASK_R)
7084 goto out_inval;
7085 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7086 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7087 goto out_inval;
7088 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7089 goto out_inval;
7090 return NFS_OK;
7091out_inval:
7092 return -NFS4ERR_INVAL;
7093}
7094
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007095static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007096nfs41_same_server_scope(struct nfs41_server_scope *a,
7097 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007098{
7099 if (a->server_scope_sz == b->server_scope_sz &&
7100 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7101 return true;
7102
7103 return false;
7104}
7105
Andy Adamson02a95de2016-02-05 16:08:37 -05007106static void
7107nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7108{
7109}
7110
7111static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7112 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7113};
7114
Andy Adamson357f54d2010-12-14 10:11:57 -05007115/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007116 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007117 *
7118 * The 4.1 client currently uses the same TCP connection for the
7119 * fore and backchannel.
7120 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007121static
7122int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7123 struct rpc_xprt *xprt,
7124 struct nfs_client *clp,
7125 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007126{
7127 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007128 struct nfs41_bind_conn_to_session_args args = {
7129 .client = clp,
7130 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7131 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007132 struct nfs41_bind_conn_to_session_res res;
7133 struct rpc_message msg = {
7134 .rpc_proc =
7135 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007136 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007137 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007138 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007139 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007140 struct rpc_task_setup task_setup_data = {
7141 .rpc_client = clnt,
7142 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007143 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007144 .rpc_message = &msg,
7145 .flags = RPC_TASK_TIMEOUT,
7146 };
7147 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007148
7149 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007150
Trond Myklebust71a097c2015-02-18 09:27:18 -08007151 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7152 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7153 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007154
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007155 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7156 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7157 args.dir = NFS4_CDFC4_FORE;
7158
7159 task = rpc_run_task(&task_setup_data);
7160 if (!IS_ERR(task)) {
7161 status = task->tk_status;
7162 rpc_put_task(task);
7163 } else
7164 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007165 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007166 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007167 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007168 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7169 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7170 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007171 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007172 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007173 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007174 dprintk("NFS: %s: Unexpected direction from server\n",
7175 __func__);
7176 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007177 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007178 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007179 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7181 __func__);
7182 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007183 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007184 }
7185 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007186out:
7187 dprintk("<-- %s status= %d\n", __func__, status);
7188 return status;
7189}
7190
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007191struct rpc_bind_conn_calldata {
7192 struct nfs_client *clp;
7193 struct rpc_cred *cred;
7194};
7195
7196static int
7197nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7198 struct rpc_xprt *xprt,
7199 void *calldata)
7200{
7201 struct rpc_bind_conn_calldata *p = calldata;
7202
7203 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7204}
7205
7206int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7207{
7208 struct rpc_bind_conn_calldata data = {
7209 .clp = clp,
7210 .cred = cred,
7211 };
7212 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7213 nfs4_proc_bind_conn_to_session_callback, &data);
7214}
7215
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007216/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007217 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7218 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007219 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007220static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7221 .how = SP4_MACH_CRED,
7222 .enforce.u.words = {
7223 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7224 1 << (OP_EXCHANGE_ID - 32) |
7225 1 << (OP_CREATE_SESSION - 32) |
7226 1 << (OP_DESTROY_SESSION - 32) |
7227 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007228 },
7229 .allow.u.words = {
7230 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007231 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007232 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007233 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007234 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007235 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007236 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007237 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007238 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007239 1 << (OP_FREE_STATEID - 32) |
7240 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007241 }
7242};
7243
7244/*
7245 * Select the state protection mode for client `clp' given the server results
7246 * from exchange_id in `sp'.
7247 *
7248 * Returns 0 on success, negative errno otherwise.
7249 */
7250static int nfs4_sp4_select_mode(struct nfs_client *clp,
7251 struct nfs41_state_protection *sp)
7252{
7253 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7254 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7255 1 << (OP_EXCHANGE_ID - 32) |
7256 1 << (OP_CREATE_SESSION - 32) |
7257 1 << (OP_DESTROY_SESSION - 32) |
7258 1 << (OP_DESTROY_CLIENTID - 32)
7259 };
7260 unsigned int i;
7261
7262 if (sp->how == SP4_MACH_CRED) {
7263 /* Print state protect result */
7264 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7265 for (i = 0; i <= LAST_NFS4_OP; i++) {
7266 if (test_bit(i, sp->enforce.u.longs))
7267 dfprintk(MOUNT, " enforce op %d\n", i);
7268 if (test_bit(i, sp->allow.u.longs))
7269 dfprintk(MOUNT, " allow op %d\n", i);
7270 }
7271
7272 /* make sure nothing is on enforce list that isn't supported */
7273 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7274 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7275 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7276 return -EINVAL;
7277 }
7278 }
7279
7280 /*
7281 * Minimal mode - state operations are allowed to use machine
7282 * credential. Note this already happens by default, so the
7283 * client doesn't have to do anything more than the negotiation.
7284 *
7285 * NOTE: we don't care if EXCHANGE_ID is in the list -
7286 * we're already using the machine cred for exchange_id
7287 * and will never use a different cred.
7288 */
7289 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7290 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7291 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7292 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7293 dfprintk(MOUNT, "sp4_mach_cred:\n");
7294 dfprintk(MOUNT, " minimal mode enabled\n");
7295 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7296 } else {
7297 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7298 return -EINVAL;
7299 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007300
7301 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007302 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7303 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007304 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7305 dfprintk(MOUNT, " cleanup mode enabled\n");
7306 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7307 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007308
Andrew Elble99ade3c2015-12-02 09:39:51 -05007309 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7310 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7311 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7312 &clp->cl_sp4_flags);
7313 }
7314
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007315 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7316 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7317 dfprintk(MOUNT, " secinfo mode enabled\n");
7318 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7319 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007320
7321 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7322 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7323 dfprintk(MOUNT, " stateid mode enabled\n");
7324 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7325 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007326
7327 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7328 dfprintk(MOUNT, " write mode enabled\n");
7329 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7330 }
7331
7332 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7333 dfprintk(MOUNT, " commit mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7335 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007336 }
7337
7338 return 0;
7339}
7340
Andy Adamson8d89bd72016-09-09 09:22:18 -04007341struct nfs41_exchange_id_data {
7342 struct nfs41_exchange_id_res res;
7343 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007344 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007345 int rpc_status;
7346};
7347
7348static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7349{
7350 struct nfs41_exchange_id_data *cdata =
7351 (struct nfs41_exchange_id_data *)data;
7352 struct nfs_client *clp = cdata->args.client;
7353 int status = task->tk_status;
7354
7355 trace_nfs4_exchange_id(clp, status);
7356
7357 if (status == 0)
7358 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007359
7360 if (cdata->xprt && status == 0) {
7361 status = nfs4_detect_session_trunking(clp, &cdata->res,
7362 cdata->xprt);
7363 goto out;
7364 }
7365
Andy Adamson8d89bd72016-09-09 09:22:18 -04007366 if (status == 0)
7367 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7368
7369 if (status == 0) {
7370 clp->cl_clientid = cdata->res.clientid;
7371 clp->cl_exchange_flags = cdata->res.flags;
7372 /* Client ID is not confirmed */
7373 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7374 clear_bit(NFS4_SESSION_ESTABLISHED,
7375 &clp->cl_session->session_state);
7376 clp->cl_seqid = cdata->res.seqid;
7377 }
7378
7379 kfree(clp->cl_serverowner);
7380 clp->cl_serverowner = cdata->res.server_owner;
7381 cdata->res.server_owner = NULL;
7382
7383 /* use the most recent implementation id */
7384 kfree(clp->cl_implid);
7385 clp->cl_implid = cdata->res.impl_id;
7386 cdata->res.impl_id = NULL;
7387
7388 if (clp->cl_serverscope != NULL &&
7389 !nfs41_same_server_scope(clp->cl_serverscope,
7390 cdata->res.server_scope)) {
7391 dprintk("%s: server_scope mismatch detected\n",
7392 __func__);
7393 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7394 kfree(clp->cl_serverscope);
7395 clp->cl_serverscope = NULL;
7396 }
7397
7398 if (clp->cl_serverscope == NULL) {
7399 clp->cl_serverscope = cdata->res.server_scope;
7400 cdata->res.server_scope = NULL;
7401 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007402 /* Save the EXCHANGE_ID verifier session trunk tests */
7403 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7404 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007405 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007406out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007407 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007408 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007409}
7410
7411static void nfs4_exchange_id_release(void *data)
7412{
7413 struct nfs41_exchange_id_data *cdata =
7414 (struct nfs41_exchange_id_data *)data;
7415
7416 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007417 if (cdata->xprt) {
7418 xprt_put(cdata->xprt);
7419 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7420 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007421 kfree(cdata->res.impl_id);
7422 kfree(cdata->res.server_scope);
7423 kfree(cdata->res.server_owner);
7424 kfree(cdata);
7425}
7426
7427static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7428 .rpc_call_done = nfs4_exchange_id_done,
7429 .rpc_release = nfs4_exchange_id_release,
7430};
7431
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007432/*
7433 * _nfs4_proc_exchange_id()
7434 *
7435 * Wrapper for EXCHANGE_ID operation.
7436 */
7437static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007438 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007439{
7440 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007441 struct rpc_message msg = {
7442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007443 .rpc_cred = cred,
7444 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007445 struct rpc_task_setup task_setup_data = {
7446 .rpc_client = clp->cl_rpcclient,
7447 .callback_ops = &nfs4_exchange_id_call_ops,
7448 .rpc_message = &msg,
7449 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7450 };
7451 struct nfs41_exchange_id_data *calldata;
7452 struct rpc_task *task;
7453 int status = -EIO;
7454
7455 if (!atomic_inc_not_zero(&clp->cl_count))
7456 goto out;
7457
7458 status = -ENOMEM;
7459 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7460 if (!calldata)
7461 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007462
Andy Adamsonad0849a2016-09-09 09:22:28 -04007463 if (!xprt)
7464 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007465
7466 status = nfs4_init_uniform_client_string(clp);
7467 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007468 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007469
7470 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007471 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007472 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007473
Andy Adamson8d89bd72016-09-09 09:22:18 -04007474 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7475 GFP_NOFS);
7476 status = -ENOMEM;
7477 if (unlikely(calldata->res.server_owner == NULL))
7478 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007479
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007481 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007482 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007483 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7486 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007487 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007488
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007489 switch (sp4_how) {
7490 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007491 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007492 break;
7493
7494 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007496 break;
7497
7498 default:
7499 /* unsupported! */
7500 WARN_ON_ONCE(1);
7501 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007502 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007503 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007504 if (xprt) {
7505 calldata->xprt = xprt;
7506 task_setup_data.rpc_xprt = xprt;
7507 task_setup_data.flags =
7508 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7509 calldata->args.verifier = &clp->cl_confirm;
7510 } else {
7511 calldata->args.verifier = &verifier;
7512 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007513 calldata->args.client = clp;
7514#ifdef CONFIG_NFS_V4_1_MIGRATION
7515 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7516 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7517 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7518#else
7519 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7520 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7521#endif
7522 msg.rpc_argp = &calldata->args;
7523 msg.rpc_resp = &calldata->res;
7524 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007525
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 task = rpc_run_task(&task_setup_data);
7527 if (IS_ERR(task)) {
7528 status = PTR_ERR(task);
7529 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007530 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007531
Andy Adamsonad0849a2016-09-09 09:22:28 -04007532 if (!xprt) {
7533 status = rpc_wait_for_completion_task(task);
7534 if (!status)
7535 status = calldata->rpc_status;
7536 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007537 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007538
Andy Adamson8d89bd72016-09-09 09:22:18 -04007539 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007540out:
Chuck Lever177313f2012-05-21 22:44:58 -04007541 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007542 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007543 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007544 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007545 clp->cl_implid->date.seconds,
7546 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007547 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007548 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007549
7550out_impl_id:
7551 kfree(calldata->res.impl_id);
7552out_server_scope:
7553 kfree(calldata->res.server_scope);
7554out_server_owner:
7555 kfree(calldata->res.server_owner);
7556out_calldata:
7557 kfree(calldata);
7558 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007559}
7560
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007561/*
7562 * nfs4_proc_exchange_id()
7563 *
7564 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7565 *
7566 * Since the clientid has expired, all compounds using sessions
7567 * associated with the stale clientid will be returning
7568 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7569 * be in some phase of session reset.
7570 *
7571 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7572 */
7573int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7574{
7575 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7576 int status;
7577
7578 /* try SP4_MACH_CRED if krb5i/p */
7579 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7580 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007581 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007582 if (!status)
7583 return 0;
7584 }
7585
7586 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007587 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007588}
7589
Andy Adamson04fa2c62016-09-09 09:22:29 -04007590/**
7591 * nfs4_test_session_trunk
7592 *
7593 * This is an add_xprt_test() test function called from
7594 * rpc_clnt_setup_test_and_add_xprt.
7595 *
7596 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7597 * and is dereferrenced in nfs4_exchange_id_release
7598 *
7599 * Upon success, add the new transport to the rpc_clnt
7600 *
7601 * @clnt: struct rpc_clnt to get new transport
7602 * @xprt: the rpc_xprt to test
7603 * @data: call data for _nfs4_proc_exchange_id.
7604 */
7605int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7606 void *data)
7607{
7608 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7609 u32 sp4_how;
7610
7611 dprintk("--> %s try %s\n", __func__,
7612 xprt->address_strings[RPC_DISPLAY_ADDR]);
7613
7614 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7615
7616 /* Test connection for session trunking. Async exchange_id call */
7617 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7618}
7619EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7620
Trond Myklebust66245532012-05-25 17:18:09 -04007621static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7622 struct rpc_cred *cred)
7623{
7624 struct rpc_message msg = {
7625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7626 .rpc_argp = clp,
7627 .rpc_cred = cred,
7628 };
7629 int status;
7630
7631 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007632 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007633 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007634 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007635 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7636 return status;
7637}
7638
7639static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7640 struct rpc_cred *cred)
7641{
7642 unsigned int loop;
7643 int ret;
7644
7645 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7646 ret = _nfs4_proc_destroy_clientid(clp, cred);
7647 switch (ret) {
7648 case -NFS4ERR_DELAY:
7649 case -NFS4ERR_CLIENTID_BUSY:
7650 ssleep(1);
7651 break;
7652 default:
7653 return ret;
7654 }
7655 }
7656 return 0;
7657}
7658
7659int nfs4_destroy_clientid(struct nfs_client *clp)
7660{
7661 struct rpc_cred *cred;
7662 int ret = 0;
7663
7664 if (clp->cl_mvops->minor_version < 1)
7665 goto out;
7666 if (clp->cl_exchange_flags == 0)
7667 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007668 if (clp->cl_preserve_clid)
7669 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007670 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007671 ret = nfs4_proc_destroy_clientid(clp, cred);
7672 if (cred)
7673 put_rpccred(cred);
7674 switch (ret) {
7675 case 0:
7676 case -NFS4ERR_STALE_CLIENTID:
7677 clp->cl_exchange_flags = 0;
7678 }
7679out:
7680 return ret;
7681}
7682
Andy Adamson2050f0c2009-04-01 09:22:30 -04007683struct nfs4_get_lease_time_data {
7684 struct nfs4_get_lease_time_args *args;
7685 struct nfs4_get_lease_time_res *res;
7686 struct nfs_client *clp;
7687};
7688
7689static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7690 void *calldata)
7691{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007692 struct nfs4_get_lease_time_data *data =
7693 (struct nfs4_get_lease_time_data *)calldata;
7694
7695 dprintk("--> %s\n", __func__);
7696 /* just setup sequence, do not trigger session recovery
7697 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007698 nfs41_setup_sequence(data->clp->cl_session,
7699 &data->args->la_seq_args,
7700 &data->res->lr_seq_res,
7701 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007702 dprintk("<-- %s\n", __func__);
7703}
7704
7705/*
7706 * Called from nfs4_state_manager thread for session setup, so don't recover
7707 * from sequence operation or clientid errors.
7708 */
7709static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7710{
7711 struct nfs4_get_lease_time_data *data =
7712 (struct nfs4_get_lease_time_data *)calldata;
7713
7714 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007715 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7716 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007717 switch (task->tk_status) {
7718 case -NFS4ERR_DELAY:
7719 case -NFS4ERR_GRACE:
7720 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7721 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7722 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007723 /* fall through */
7724 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007725 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007726 return;
7727 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007728 dprintk("<-- %s\n", __func__);
7729}
7730
Trond Myklebust17280172012-03-11 13:11:00 -04007731static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007732 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7733 .rpc_call_done = nfs4_get_lease_time_done,
7734};
7735
7736int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7737{
7738 struct rpc_task *task;
7739 struct nfs4_get_lease_time_args args;
7740 struct nfs4_get_lease_time_res res = {
7741 .lr_fsinfo = fsinfo,
7742 };
7743 struct nfs4_get_lease_time_data data = {
7744 .args = &args,
7745 .res = &res,
7746 .clp = clp,
7747 };
7748 struct rpc_message msg = {
7749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7750 .rpc_argp = &args,
7751 .rpc_resp = &res,
7752 };
7753 struct rpc_task_setup task_setup = {
7754 .rpc_client = clp->cl_rpcclient,
7755 .rpc_message = &msg,
7756 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007757 .callback_data = &data,
7758 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007759 };
7760 int status;
7761
Chuck Levera9c92d62013-08-09 12:48:18 -04007762 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007763 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007764 dprintk("--> %s\n", __func__);
7765 task = rpc_run_task(&task_setup);
7766
7767 if (IS_ERR(task))
7768 status = PTR_ERR(task);
7769 else {
7770 status = task->tk_status;
7771 rpc_put_task(task);
7772 }
7773 dprintk("<-- %s return %d\n", __func__, status);
7774
7775 return status;
7776}
7777
Andy Adamsonfc931582009-04-01 09:22:31 -04007778/*
7779 * Initialize the values to be used by the client in CREATE_SESSION
7780 * If nfs4_init_session set the fore channel request and response sizes,
7781 * use them.
7782 *
7783 * Set the back channel max_resp_sz_cached to zero to force the client to
7784 * always set csa_cachethis to FALSE because the current implementation
7785 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7786 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007787static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7788 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007789{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007790 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007791 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007792
Andy Adamson18aad3d2013-06-26 12:21:49 -04007793 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7794 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7795
Andy Adamsonfc931582009-04-01 09:22:31 -04007796 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007797 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7798 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007799 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007800 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007801
7802 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007803 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007804 __func__,
7805 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007806 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007807
7808 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007809 args->bc_attrs.max_rqst_sz = max_bc_payload;
7810 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007811 args->bc_attrs.max_resp_sz_cached = 0;
7812 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007813 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007814
7815 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7816 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7817 __func__,
7818 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7819 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7820 args->bc_attrs.max_reqs);
7821}
7822
Trond Myklebust79969dd2015-02-18 11:30:18 -08007823static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7824 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007825{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007826 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007827 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007828
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007829 if (rcvd->max_resp_sz > sent->max_resp_sz)
7830 return -EINVAL;
7831 /*
7832 * Our requested max_ops is the minimum we need; we're not
7833 * prepared to break up compounds into smaller pieces than that.
7834 * So, no point even trying to continue if the server won't
7835 * cooperate:
7836 */
7837 if (rcvd->max_ops < sent->max_ops)
7838 return -EINVAL;
7839 if (rcvd->max_reqs == 0)
7840 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007841 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7842 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007843 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007844}
7845
Trond Myklebust79969dd2015-02-18 11:30:18 -08007846static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7847 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007848{
7849 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007850 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007851
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007852 if (!(res->flags & SESSION4_BACK_CHAN))
7853 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007854 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7855 return -EINVAL;
7856 if (rcvd->max_resp_sz < sent->max_resp_sz)
7857 return -EINVAL;
7858 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7859 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007860 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007861 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007862 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007863 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007864out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007865 return 0;
7866}
Andy Adamson8d353012009-04-01 09:22:32 -04007867
Andy Adamson8d353012009-04-01 09:22:32 -04007868static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007870{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007871 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007872
Trond Myklebust79969dd2015-02-18 11:30:18 -08007873 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007874 if (ret)
7875 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007876 return nfs4_verify_back_channel_attrs(args, res);
7877}
7878
7879static void nfs4_update_session(struct nfs4_session *session,
7880 struct nfs41_create_session_res *res)
7881{
7882 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007883 /* Mark client id and session as being confirmed */
7884 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7885 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007886 session->flags = res->flags;
7887 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007888 if (res->flags & SESSION4_BACK_CHAN)
7889 memcpy(&session->bc_attrs, &res->bc_attrs,
7890 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007891}
7892
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007893static int _nfs4_proc_create_session(struct nfs_client *clp,
7894 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007895{
7896 struct nfs4_session *session = clp->cl_session;
7897 struct nfs41_create_session_args args = {
7898 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007899 .clientid = clp->cl_clientid,
7900 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007901 .cb_program = NFS4_CALLBACK,
7902 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007903 struct nfs41_create_session_res res;
7904
Andy Adamsonfc931582009-04-01 09:22:31 -04007905 struct rpc_message msg = {
7906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7907 .rpc_argp = &args,
7908 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007909 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007910 };
7911 int status;
7912
Chuck Lever6b26cc82016-05-02 14:40:40 -04007913 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007914 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007915
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007916 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007917 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007918
Trond Myklebustb519d402016-09-11 14:50:01 -04007919 switch (status) {
7920 case -NFS4ERR_STALE_CLIENTID:
7921 case -NFS4ERR_DELAY:
7922 case -ETIMEDOUT:
7923 case -EACCES:
7924 case -EAGAIN:
7925 goto out;
7926 };
7927
7928 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007929 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007930 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007931 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007932 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007933 if (status)
7934 goto out;
7935 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007936 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007937out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007938 return status;
7939}
7940
7941/*
7942 * Issues a CREATE_SESSION operation to the server.
7943 * It is the responsibility of the caller to verify the session is
7944 * expired before calling this routine.
7945 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007946int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007947{
7948 int status;
7949 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007950 struct nfs4_session *session = clp->cl_session;
7951
7952 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7953
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007954 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007955 if (status)
7956 goto out;
7957
Andy Adamsonaacd5532011-11-09 13:58:21 -05007958 /* Init or reset the session slot tables */
7959 status = nfs4_setup_session_slot_tables(session);
7960 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007961 if (status)
7962 goto out;
7963
7964 ptr = (unsigned *)&session->sess_id.data[0];
7965 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7966 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007967out:
7968 dprintk("<-- %s\n", __func__);
7969 return status;
7970}
7971
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007972/*
7973 * Issue the over-the-wire RPC DESTROY_SESSION.
7974 * The caller must serialize access to this routine.
7975 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007976int nfs4_proc_destroy_session(struct nfs4_session *session,
7977 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007978{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007979 struct rpc_message msg = {
7980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7981 .rpc_argp = session,
7982 .rpc_cred = cred,
7983 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007984 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007985
7986 dprintk("--> nfs4_proc_destroy_session\n");
7987
7988 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007989 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7990 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007991
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007992 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007993 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007994
7995 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007996 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007997 "Session has been destroyed regardless...\n", status);
7998
7999 dprintk("<-- nfs4_proc_destroy_session\n");
8000 return status;
8001}
8002
Trond Myklebust7b38c362012-05-23 13:23:31 -04008003/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008004 * Renew the cl_session lease.
8005 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008006struct nfs4_sequence_data {
8007 struct nfs_client *clp;
8008 struct nfs4_sequence_args args;
8009 struct nfs4_sequence_res res;
8010};
8011
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008012static void nfs41_sequence_release(void *data)
8013{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008014 struct nfs4_sequence_data *calldata = data;
8015 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008016
Alexandros Batsakis71358402010-02-05 03:45:05 -08008017 if (atomic_read(&clp->cl_count) > 1)
8018 nfs4_schedule_state_renewal(clp);
8019 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008020 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008021}
8022
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008023static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8024{
8025 switch(task->tk_status) {
8026 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008027 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8028 return -EAGAIN;
8029 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008030 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008031 }
8032 return 0;
8033}
8034
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008035static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008036{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008037 struct nfs4_sequence_data *calldata = data;
8038 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039
Trond Myklebust14516c32010-07-31 14:29:06 -04008040 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8041 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008042
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008043 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008044 if (task->tk_status < 0) {
8045 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008046 if (atomic_read(&clp->cl_count) == 1)
8047 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008048
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008049 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8050 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008051 return;
8052 }
8053 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008054 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008055out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008056 dprintk("<-- %s\n", __func__);
8057}
8058
8059static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8060{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008061 struct nfs4_sequence_data *calldata = data;
8062 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063 struct nfs4_sequence_args *args;
8064 struct nfs4_sequence_res *res;
8065
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008066 args = task->tk_msg.rpc_argp;
8067 res = task->tk_msg.rpc_resp;
8068
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008069 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008070}
8071
8072static const struct rpc_call_ops nfs41_sequence_ops = {
8073 .rpc_call_done = nfs41_sequence_call_done,
8074 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008075 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008076};
8077
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008078static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8079 struct rpc_cred *cred,
8080 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008082 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008083 struct rpc_message msg = {
8084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8085 .rpc_cred = cred,
8086 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008087 struct rpc_task_setup task_setup_data = {
8088 .rpc_client = clp->cl_rpcclient,
8089 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008090 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008091 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093
Alexandros Batsakis71358402010-02-05 03:45:05 -08008094 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008095 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008096 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008097 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008098 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008099 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008101 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008102 if (is_privileged)
8103 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008104 msg.rpc_argp = &calldata->args;
8105 msg.rpc_resp = &calldata->res;
8106 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008107 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008108
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008109 return rpc_run_task(&task_setup_data);
8110}
8111
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008112static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008113{
8114 struct rpc_task *task;
8115 int ret = 0;
8116
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008117 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008118 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008119 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008120 if (IS_ERR(task))
8121 ret = PTR_ERR(task);
8122 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008123 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008124 dprintk("<-- %s status=%d\n", __func__, ret);
8125 return ret;
8126}
8127
8128static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8129{
8130 struct rpc_task *task;
8131 int ret;
8132
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008133 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008134 if (IS_ERR(task)) {
8135 ret = PTR_ERR(task);
8136 goto out;
8137 }
8138 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008139 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008140 ret = task->tk_status;
8141 rpc_put_task(task);
8142out:
8143 dprintk("<-- %s status=%d\n", __func__, ret);
8144 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008145}
8146
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008147struct nfs4_reclaim_complete_data {
8148 struct nfs_client *clp;
8149 struct nfs41_reclaim_complete_args arg;
8150 struct nfs41_reclaim_complete_res res;
8151};
8152
8153static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8154{
8155 struct nfs4_reclaim_complete_data *calldata = data;
8156
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008157 nfs41_setup_sequence(calldata->clp->cl_session,
8158 &calldata->arg.seq_args,
8159 &calldata->res.seq_res,
8160 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008161}
8162
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008163static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8164{
8165 switch(task->tk_status) {
8166 case 0:
8167 case -NFS4ERR_COMPLETE_ALREADY:
8168 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8169 break;
8170 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008171 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008172 /* fall through */
8173 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008174 return -EAGAIN;
8175 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008176 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008177 }
8178 return 0;
8179}
8180
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008181static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8182{
8183 struct nfs4_reclaim_complete_data *calldata = data;
8184 struct nfs_client *clp = calldata->clp;
8185 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8186
8187 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008188 if (!nfs41_sequence_done(task, res))
8189 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008190
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008191 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008192 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8193 rpc_restart_call_prepare(task);
8194 return;
8195 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008196 dprintk("<-- %s\n", __func__);
8197}
8198
8199static void nfs4_free_reclaim_complete_data(void *data)
8200{
8201 struct nfs4_reclaim_complete_data *calldata = data;
8202
8203 kfree(calldata);
8204}
8205
8206static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8207 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8208 .rpc_call_done = nfs4_reclaim_complete_done,
8209 .rpc_release = nfs4_free_reclaim_complete_data,
8210};
8211
8212/*
8213 * Issue a global reclaim complete.
8214 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008215static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8216 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008217{
8218 struct nfs4_reclaim_complete_data *calldata;
8219 struct rpc_task *task;
8220 struct rpc_message msg = {
8221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008222 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008223 };
8224 struct rpc_task_setup task_setup_data = {
8225 .rpc_client = clp->cl_rpcclient,
8226 .rpc_message = &msg,
8227 .callback_ops = &nfs4_reclaim_complete_call_ops,
8228 .flags = RPC_TASK_ASYNC,
8229 };
8230 int status = -ENOMEM;
8231
8232 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008233 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008234 if (calldata == NULL)
8235 goto out;
8236 calldata->clp = clp;
8237 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238
Chuck Levera9c92d62013-08-09 12:48:18 -04008239 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008240 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008241 msg.rpc_argp = &calldata->arg;
8242 msg.rpc_resp = &calldata->res;
8243 task_setup_data.callback_data = calldata;
8244 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008245 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008246 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008247 goto out;
8248 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008249 status = nfs4_wait_for_completion_rpc_task(task);
8250 if (status == 0)
8251 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008253 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008254out:
8255 dprintk("<-- %s status=%d\n", __func__, status);
8256 return status;
8257}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008258
8259static void
8260nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8261{
8262 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008263 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008264 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008265
8266 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008267 nfs41_setup_sequence(session, &lgp->args.seq_args,
8268 &lgp->res.seq_res, task);
8269 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008270}
8271
8272static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8273{
8274 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008275
8276 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008277 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008278 dprintk("<-- %s\n", __func__);
8279}
8280
8281static int
8282nfs4_layoutget_handle_exception(struct rpc_task *task,
8283 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8284{
Trond Myklebustee314c22012-10-01 17:25:48 -07008285 struct inode *inode = lgp->args.inode;
8286 struct nfs_server *server = NFS_SERVER(inode);
8287 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008288 int nfs4err = task->tk_status;
8289 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008290 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008291
Boaz Harroshed7e5422014-01-22 20:34:54 +02008292 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008293
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008294 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008295 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008296 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008297
8298 /*
8299 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8300 * on the file. set tk_status to -ENODATA to tell upper layer to
8301 * retry go inband.
8302 */
8303 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008304 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008305 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008306 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008307 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8308 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8309 */
8310 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008311 status = -EOVERFLOW;
8312 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008313 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008314 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008315 * (or clients) writing to the same RAID stripe except when
8316 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008317 *
8318 * Treat it like we would RECALLCONFLICT -- we retry for a little
8319 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008320 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008321 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008322 if (lgp->args.minlength == 0) {
8323 status = -EOVERFLOW;
8324 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008325 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008326 status = -EBUSY;
8327 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008328 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008329 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008330 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008331 case -NFS4ERR_DELEG_REVOKED:
8332 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008333 case -NFS4ERR_EXPIRED:
8334 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008335 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008336 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008337 lo = NFS_I(inode)->layout;
8338 /* If the open stateid was bad, then recover it. */
8339 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8340 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008341 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008342 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008343 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008344 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008345 break;
8346 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008347
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008348 /*
8349 * Mark the bad layout state as invalid, then retry
8350 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008351 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008352 spin_unlock(&inode->i_lock);
8353 pnfs_free_lseg_list(&head);
8354 status = -EAGAIN;
8355 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008356 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008357
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008358 err = nfs4_handle_exception(server, nfs4err, exception);
8359 if (!status) {
8360 if (exception->retry)
8361 status = -EAGAIN;
8362 else
8363 status = err;
8364 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008365out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008366 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008367 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008368}
8369
Idan Kedar85541162012-08-02 11:47:10 +03008370static size_t max_response_pages(struct nfs_server *server)
8371{
8372 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8373 return nfs_page_array_len(0, max_resp_sz);
8374}
8375
8376static void nfs4_free_pages(struct page **pages, size_t size)
8377{
8378 int i;
8379
8380 if (!pages)
8381 return;
8382
8383 for (i = 0; i < size; i++) {
8384 if (!pages[i])
8385 break;
8386 __free_page(pages[i]);
8387 }
8388 kfree(pages);
8389}
8390
8391static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8392{
8393 struct page **pages;
8394 int i;
8395
8396 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8397 if (!pages) {
8398 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8399 return NULL;
8400 }
8401
8402 for (i = 0; i < size; i++) {
8403 pages[i] = alloc_page(gfp_flags);
8404 if (!pages[i]) {
8405 dprintk("%s: failed to allocate page\n", __func__);
8406 nfs4_free_pages(pages, size);
8407 return NULL;
8408 }
8409 }
8410
8411 return pages;
8412}
8413
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008414static void nfs4_layoutget_release(void *calldata)
8415{
8416 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008417 struct inode *inode = lgp->args.inode;
8418 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008419 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008420
8421 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008422 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008423 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008424 put_nfs_open_context(lgp->args.ctx);
8425 kfree(calldata);
8426 dprintk("<-- %s\n", __func__);
8427}
8428
8429static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8430 .rpc_call_prepare = nfs4_layoutget_prepare,
8431 .rpc_call_done = nfs4_layoutget_done,
8432 .rpc_release = nfs4_layoutget_release,
8433};
8434
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008435struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008436nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008437{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008438 struct inode *inode = lgp->args.inode;
8439 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008440 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008441 struct rpc_task *task;
8442 struct rpc_message msg = {
8443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8444 .rpc_argp = &lgp->args,
8445 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008446 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008447 };
8448 struct rpc_task_setup task_setup_data = {
8449 .rpc_client = server->client,
8450 .rpc_message = &msg,
8451 .callback_ops = &nfs4_layoutget_call_ops,
8452 .callback_data = lgp,
8453 .flags = RPC_TASK_ASYNC,
8454 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008455 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008456 struct nfs4_exception exception = {
8457 .inode = inode,
8458 .timeout = *timeout,
8459 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008460 int status = 0;
8461
8462 dprintk("--> %s\n", __func__);
8463
Peng Tao4bd5a982014-11-17 11:05:17 +08008464 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8465 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8466
Idan Kedar85541162012-08-02 11:47:10 +03008467 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8468 if (!lgp->args.layout.pages) {
8469 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008470 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008471 }
8472 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8473
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008474 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008475 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008476 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008477
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478 task = rpc_run_task(&task_setup_data);
8479 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008480 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008481 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008482 if (status == 0) {
8483 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8484 *timeout = exception.timeout;
8485 }
8486
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008487 trace_nfs4_layoutget(lgp->args.ctx,
8488 &lgp->args.range,
8489 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008490 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008491 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008492
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008493 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8494 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008495 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008496 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008497 rpc_put_task(task);
8498 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008499 if (status)
8500 return ERR_PTR(status);
8501 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008502}
8503
Benny Halevycbe82602011-05-22 19:52:37 +03008504static void
8505nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8506{
8507 struct nfs4_layoutreturn *lrp = calldata;
8508
8509 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008510 nfs41_setup_sequence(lrp->clp->cl_session,
8511 &lrp->args.seq_args,
8512 &lrp->res.seq_res,
8513 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008514}
8515
8516static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8517{
8518 struct nfs4_layoutreturn *lrp = calldata;
8519 struct nfs_server *server;
8520
8521 dprintk("--> %s\n", __func__);
8522
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008523 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008524 return;
8525
8526 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008527 switch (task->tk_status) {
8528 default:
8529 task->tk_status = 0;
8530 case 0:
8531 break;
8532 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008533 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008534 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008535 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008536 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008537 return;
8538 }
Benny Halevycbe82602011-05-22 19:52:37 +03008539 dprintk("<-- %s\n", __func__);
8540}
8541
8542static void nfs4_layoutreturn_release(void *calldata)
8543{
8544 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008545 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008546 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008547
8548 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008549 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008550 if (lrp->res.lrs_present) {
8551 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8552 &lrp->args.range,
8553 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008554 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008555 } else
8556 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008557 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008558 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008559 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008560 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008561 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008562 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008563 kfree(calldata);
8564 dprintk("<-- %s\n", __func__);
8565}
8566
8567static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8568 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8569 .rpc_call_done = nfs4_layoutreturn_done,
8570 .rpc_release = nfs4_layoutreturn_release,
8571};
8572
Peng Tao6c166052014-11-17 09:30:40 +08008573int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008574{
8575 struct rpc_task *task;
8576 struct rpc_message msg = {
8577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8578 .rpc_argp = &lrp->args,
8579 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008580 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008581 };
8582 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008583 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008584 .rpc_message = &msg,
8585 .callback_ops = &nfs4_layoutreturn_call_ops,
8586 .callback_data = lrp,
8587 };
Peng Tao6c166052014-11-17 09:30:40 +08008588 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008589
Andrew Elble99ade3c2015-12-02 09:39:51 -05008590 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8591 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8592 &task_setup_data.rpc_client, &msg);
8593
Benny Halevycbe82602011-05-22 19:52:37 +03008594 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008595 if (!sync) {
8596 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8597 if (!lrp->inode) {
8598 nfs4_layoutreturn_release(lrp);
8599 return -EAGAIN;
8600 }
8601 task_setup_data.flags |= RPC_TASK_ASYNC;
8602 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008603 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008604 task = rpc_run_task(&task_setup_data);
8605 if (IS_ERR(task))
8606 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008607 if (sync)
8608 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008609 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008610 dprintk("<-- %s status=%d\n", __func__, status);
8611 rpc_put_task(task);
8612 return status;
8613}
8614
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008616_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8617 struct pnfs_device *pdev,
8618 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008619{
8620 struct nfs4_getdeviceinfo_args args = {
8621 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008622 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8623 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008624 };
8625 struct nfs4_getdeviceinfo_res res = {
8626 .pdev = pdev,
8627 };
8628 struct rpc_message msg = {
8629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8630 .rpc_argp = &args,
8631 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008632 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008633 };
8634 int status;
8635
8636 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008637 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008638 if (res.notification & ~args.notify_types)
8639 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008640 if (res.notification != args.notify_types)
8641 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008642
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008643 dprintk("<-- %s status=%d\n", __func__, status);
8644
8645 return status;
8646}
8647
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008648int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8649 struct pnfs_device *pdev,
8650 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008651{
8652 struct nfs4_exception exception = { };
8653 int err;
8654
8655 do {
8656 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008657 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008658 &exception);
8659 } while (exception.retry);
8660 return err;
8661}
8662EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8663
Andy Adamson863a3c62011-03-23 13:27:54 +00008664static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8665{
8666 struct nfs4_layoutcommit_data *data = calldata;
8667 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008668 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008669
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008670 nfs41_setup_sequence(session,
8671 &data->args.seq_args,
8672 &data->res.seq_res,
8673 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008674}
8675
8676static void
8677nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8678{
8679 struct nfs4_layoutcommit_data *data = calldata;
8680 struct nfs_server *server = NFS_SERVER(data->args.inode);
8681
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008682 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008683 return;
8684
8685 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008686 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8687 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8688 case -NFS4ERR_BADLAYOUT: /* no layout */
8689 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008690 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008691 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008692 break;
8693 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008694 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008695 rpc_restart_call_prepare(task);
8696 return;
8697 }
8698 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008699}
8700
8701static void nfs4_layoutcommit_release(void *calldata)
8702{
8703 struct nfs4_layoutcommit_data *data = calldata;
8704
Andy Adamsondb29c082011-07-30 20:52:38 -04008705 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008706 nfs_post_op_update_inode_force_wcc(data->args.inode,
8707 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008708 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008709 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008710 kfree(data);
8711}
8712
8713static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8714 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8715 .rpc_call_done = nfs4_layoutcommit_done,
8716 .rpc_release = nfs4_layoutcommit_release,
8717};
8718
8719int
Andy Adamsonef311532011-03-12 02:58:10 -05008720nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008721{
8722 struct rpc_message msg = {
8723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8724 .rpc_argp = &data->args,
8725 .rpc_resp = &data->res,
8726 .rpc_cred = data->cred,
8727 };
8728 struct rpc_task_setup task_setup_data = {
8729 .task = &data->task,
8730 .rpc_client = NFS_CLIENT(data->args.inode),
8731 .rpc_message = &msg,
8732 .callback_ops = &nfs4_layoutcommit_ops,
8733 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008734 };
8735 struct rpc_task *task;
8736 int status = 0;
8737
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008738 dprintk("NFS: initiating layoutcommit call. sync %d "
8739 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008740 data->args.lastbytewritten,
8741 data->args.inode->i_ino);
8742
Trond Myklebust472e2592015-02-05 16:50:30 -05008743 if (!sync) {
8744 data->inode = nfs_igrab_and_active(data->args.inode);
8745 if (data->inode == NULL) {
8746 nfs4_layoutcommit_release(data);
8747 return -EAGAIN;
8748 }
8749 task_setup_data.flags = RPC_TASK_ASYNC;
8750 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008751 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008752 task = rpc_run_task(&task_setup_data);
8753 if (IS_ERR(task))
8754 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008755 if (sync)
8756 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008757 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008758 dprintk("%s: status %d\n", __func__, status);
8759 rpc_put_task(task);
8760 return status;
8761}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008762
Andy Adamson97431202013-08-08 10:57:56 -04008763/**
8764 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8765 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8766 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008767static int
8768_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008769 struct nfs_fsinfo *info,
8770 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008771{
8772 struct nfs41_secinfo_no_name_args args = {
8773 .style = SECINFO_STYLE_CURRENT_FH,
8774 };
8775 struct nfs4_secinfo_res res = {
8776 .flavors = flavors,
8777 };
8778 struct rpc_message msg = {
8779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8780 .rpc_argp = &args,
8781 .rpc_resp = &res,
8782 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008783 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008784 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008785 int status;
8786
8787 if (use_integrity) {
8788 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008789 cred = nfs4_get_clid_cred(server->nfs_client);
8790 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008791 }
8792
8793 dprintk("--> %s\n", __func__);
8794 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8795 &res.seq_res, 0);
8796 dprintk("<-- %s status=%d\n", __func__, status);
8797
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008798 if (cred)
8799 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008800
8801 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008802}
8803
8804static int
8805nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8806 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8807{
8808 struct nfs4_exception exception = { };
8809 int err;
8810 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008811 /* first try using integrity protection */
8812 err = -NFS4ERR_WRONGSEC;
8813
8814 /* try to use integrity protection with machine cred */
8815 if (_nfs4_is_integrity_protected(server->nfs_client))
8816 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8817 flavors, true);
8818
8819 /*
8820 * if unable to use integrity protection, or SECINFO with
8821 * integrity protection returns NFS4ERR_WRONGSEC (which is
8822 * disallowed by spec, but exists in deployed servers) use
8823 * the current filesystem's rpc_client and the user cred.
8824 */
8825 if (err == -NFS4ERR_WRONGSEC)
8826 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8827 flavors, false);
8828
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008829 switch (err) {
8830 case 0:
8831 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008832 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008833 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008834 default:
8835 err = nfs4_handle_exception(server, err, &exception);
8836 }
8837 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008838out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008839 return err;
8840}
8841
8842static int
8843nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8844 struct nfs_fsinfo *info)
8845{
8846 int err;
8847 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008848 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008849 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008850 struct nfs4_secinfo4 *secinfo;
8851 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008852
8853 page = alloc_page(GFP_KERNEL);
8854 if (!page) {
8855 err = -ENOMEM;
8856 goto out;
8857 }
8858
8859 flavors = page_address(page);
8860 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8861
8862 /*
8863 * Fall back on "guess and check" method if
8864 * the server doesn't support SECINFO_NO_NAME
8865 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008866 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008867 err = nfs4_find_root_sec(server, fhandle, info);
8868 goto out_freepage;
8869 }
8870 if (err)
8871 goto out_freepage;
8872
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008873 for (i = 0; i < flavors->num_flavors; i++) {
8874 secinfo = &flavors->flavors[i];
8875
8876 switch (secinfo->flavor) {
8877 case RPC_AUTH_NULL:
8878 case RPC_AUTH_UNIX:
8879 case RPC_AUTH_GSS:
8880 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8881 &secinfo->flavor_info);
8882 break;
8883 default:
8884 flavor = RPC_AUTH_MAXFLAVOR;
8885 break;
8886 }
8887
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008888 if (!nfs_auth_info_match(&server->auth_info, flavor))
8889 flavor = RPC_AUTH_MAXFLAVOR;
8890
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008891 if (flavor != RPC_AUTH_MAXFLAVOR) {
8892 err = nfs4_lookup_root_sec(server, fhandle,
8893 info, flavor);
8894 if (!err)
8895 break;
8896 }
8897 }
8898
8899 if (flavor == RPC_AUTH_MAXFLAVOR)
8900 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008901
8902out_freepage:
8903 put_page(page);
8904 if (err == -EACCES)
8905 return -EPERM;
8906out:
8907 return err;
8908}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008909
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008910static int _nfs41_test_stateid(struct nfs_server *server,
8911 nfs4_stateid *stateid,
8912 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008913{
8914 int status;
8915 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008916 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008917 };
8918 struct nfs41_test_stateid_res res;
8919 struct rpc_message msg = {
8920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8921 .rpc_argp = &args,
8922 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008923 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008924 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008925 struct rpc_clnt *rpc_client = server->client;
8926
8927 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8928 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008929
Chuck Lever38527b12012-07-11 16:30:23 -04008930 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008931 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008932 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008933 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008934 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008935 if (status != NFS_OK) {
8936 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008937 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008938 }
8939 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008940 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008941}
8942
Trond Myklebust43912bb2016-09-22 13:38:56 -04008943static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8944 int err, struct nfs4_exception *exception)
8945{
8946 exception->retry = 0;
8947 switch(err) {
8948 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008949 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008950 nfs4_handle_exception(server, err, exception);
8951 break;
8952 case -NFS4ERR_BADSESSION:
8953 case -NFS4ERR_BADSLOT:
8954 case -NFS4ERR_BAD_HIGH_SLOT:
8955 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8956 case -NFS4ERR_DEADSESSION:
8957 nfs4_do_handle_exception(server, err, exception);
8958 }
8959}
8960
Chuck Lever38527b12012-07-11 16:30:23 -04008961/**
8962 * nfs41_test_stateid - perform a TEST_STATEID operation
8963 *
8964 * @server: server / transport on which to perform the operation
8965 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008966 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008967 *
8968 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8969 * Otherwise a negative NFS4ERR value is returned if the operation
8970 * failed or the state ID is not currently valid.
8971 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008972static int nfs41_test_stateid(struct nfs_server *server,
8973 nfs4_stateid *stateid,
8974 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008975{
8976 struct nfs4_exception exception = { };
8977 int err;
8978 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008979 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008980 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008981 } while (exception.retry);
8982 return err;
8983}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008984
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008985struct nfs_free_stateid_data {
8986 struct nfs_server *server;
8987 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008988 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008989};
8990
8991static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8992{
8993 struct nfs_free_stateid_data *data = calldata;
8994 nfs41_setup_sequence(nfs4_get_session(data->server),
8995 &data->args.seq_args,
8996 &data->res.seq_res,
8997 task);
8998}
8999
9000static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9001{
9002 struct nfs_free_stateid_data *data = calldata;
9003
9004 nfs41_sequence_done(task, &data->res.seq_res);
9005
9006 switch (task->tk_status) {
9007 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009008 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009009 rpc_restart_call_prepare(task);
9010 }
9011}
9012
9013static void nfs41_free_stateid_release(void *calldata)
9014{
9015 kfree(calldata);
9016}
9017
Trond Myklebust17f26b12013-08-21 15:48:42 -04009018static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009019 .rpc_call_prepare = nfs41_free_stateid_prepare,
9020 .rpc_call_done = nfs41_free_stateid_done,
9021 .rpc_release = nfs41_free_stateid_release,
9022};
9023
9024static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009025 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009026 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009027 bool privileged)
9028{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009029 struct rpc_message msg = {
9030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009031 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009032 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009033 struct rpc_task_setup task_setup = {
9034 .rpc_client = server->client,
9035 .rpc_message = &msg,
9036 .callback_ops = &nfs41_free_stateid_ops,
9037 .flags = RPC_TASK_ASYNC,
9038 };
9039 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009040
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009041 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9042 &task_setup.rpc_client, &msg);
9043
Chuck Lever38527b12012-07-11 16:30:23 -04009044 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009045 data = kmalloc(sizeof(*data), GFP_NOFS);
9046 if (!data)
9047 return ERR_PTR(-ENOMEM);
9048 data->server = server;
9049 nfs4_stateid_copy(&data->args.stateid, stateid);
9050
9051 task_setup.callback_data = data;
9052
9053 msg.rpc_argp = &data->args;
9054 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009055 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009056 if (privileged)
9057 nfs4_set_sequence_privileged(&data->args.seq_args);
9058
9059 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009060}
9061
Chuck Lever38527b12012-07-11 16:30:23 -04009062/**
9063 * nfs41_free_stateid - perform a FREE_STATEID operation
9064 *
9065 * @server: server / transport on which to perform the operation
9066 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009067 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009068 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009069 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009070 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009071 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009072static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009073 const nfs4_stateid *stateid,
9074 struct rpc_cred *cred,
9075 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009076{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009077 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009078
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009079 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009080 if (IS_ERR(task))
9081 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009082 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009083 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009084}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009085
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009086static void
9087nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009088{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009089 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009090
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009091 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009092 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009093}
9094
Trond Myklebust36281ca2012-03-04 18:13:56 -05009095static bool nfs41_match_stateid(const nfs4_stateid *s1,
9096 const nfs4_stateid *s2)
9097{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009098 if (s1->type != s2->type)
9099 return false;
9100
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009101 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009102 return false;
9103
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009104 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009105 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009106 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009107 return true;
9108
9109 return false;
9110}
9111
Andy Adamson557134a2009-04-01 09:21:53 -04009112#endif /* CONFIG_NFS_V4_1 */
9113
Trond Myklebust36281ca2012-03-04 18:13:56 -05009114static bool nfs4_match_stateid(const nfs4_stateid *s1,
9115 const nfs4_stateid *s2)
9116{
Trond Myklebustf597c532012-03-04 18:13:56 -05009117 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009118}
9119
9120
Trond Myklebust17280172012-03-11 13:11:00 -04009121static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009122 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009123 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 .recover_open = nfs4_open_reclaim,
9125 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009126 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009127 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128};
9129
Andy Adamson591d71c2009-04-01 09:22:47 -04009130#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009131static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009132 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9133 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9134 .recover_open = nfs4_open_reclaim,
9135 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009136 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009137 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009138 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009139};
9140#endif /* CONFIG_NFS_V4_1 */
9141
Trond Myklebust17280172012-03-11 13:11:00 -04009142static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009143 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009144 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009145 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009147 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009148};
9149
Andy Adamson591d71c2009-04-01 09:22:47 -04009150#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009151static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009152 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9153 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009154 .recover_open = nfs41_open_expired,
9155 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009156 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009157};
9158#endif /* CONFIG_NFS_V4_1 */
9159
Trond Myklebust17280172012-03-11 13:11:00 -04009160static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009161 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009162 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009163 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009164};
9165
9166#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009167static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009168 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009169 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009170 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009171};
9172#endif
9173
Chuck Leverec011fe2013-10-17 14:12:39 -04009174static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009175 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009176 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009177};
9178
9179#if defined(CONFIG_NFS_V4_1)
9180static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009181 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009182 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009183};
9184#endif /* CONFIG_NFS_V4_1 */
9185
Trond Myklebust97dc1352010-06-16 09:52:26 -04009186static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9187 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009188 .init_caps = NFS_CAP_READDIRPLUS
9189 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009190 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009191 .init_client = nfs40_init_client,
9192 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009193 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009194 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009195 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009196 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009197 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009198 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009199 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9200 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9201 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009202 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009203};
9204
9205#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009206static struct nfs_seqid *
9207nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9208{
9209 return NULL;
9210}
9211
Trond Myklebust97dc1352010-06-16 09:52:26 -04009212static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9213 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009214 .init_caps = NFS_CAP_READDIRPLUS
9215 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009216 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009217 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009218 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009219 .init_client = nfs41_init_client,
9220 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009221 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009222 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009223 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009224 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009225 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009226 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009227 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009228 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9229 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9230 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009231 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009232};
9233#endif
9234
Steve Dickson42c2c422013-05-22 12:50:38 -04009235#if defined(CONFIG_NFS_V4_2)
9236static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9237 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009238 .init_caps = NFS_CAP_READDIRPLUS
9239 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009240 | NFS_CAP_POSIX_LOCK
9241 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009242 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009243 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009244 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009245 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009246 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009247 | NFS_CAP_LAYOUTSTATS
9248 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009249 .init_client = nfs41_init_client,
9250 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009251 .match_stateid = nfs41_match_stateid,
9252 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009253 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009254 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009255 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009256 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009257 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009258 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9259 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9260 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009261 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009262};
9263#endif
9264
Trond Myklebust97dc1352010-06-16 09:52:26 -04009265const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9266 [0] = &nfs_v4_0_minor_ops,
9267#if defined(CONFIG_NFS_V4_1)
9268 [1] = &nfs_v4_1_minor_ops,
9269#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009270#if defined(CONFIG_NFS_V4_2)
9271 [2] = &nfs_v4_2_minor_ops,
9272#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009273};
9274
Trond Myklebust13997822016-07-24 17:10:52 -04009275static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009276{
9277 ssize_t error, error2;
9278
9279 error = generic_listxattr(dentry, list, size);
9280 if (error < 0)
9281 return error;
9282 if (list) {
9283 list += error;
9284 size -= error;
9285 }
9286
9287 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9288 if (error2 < 0)
9289 return error2;
9290 return error + error2;
9291}
9292
Trond Myklebust17f26b12013-08-21 15:48:42 -04009293static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009294 .create = nfs_create,
9295 .lookup = nfs_lookup,
9296 .atomic_open = nfs_atomic_open,
9297 .link = nfs_link,
9298 .unlink = nfs_unlink,
9299 .symlink = nfs_symlink,
9300 .mkdir = nfs_mkdir,
9301 .rmdir = nfs_rmdir,
9302 .mknod = nfs_mknod,
9303 .rename = nfs_rename,
9304 .permission = nfs_permission,
9305 .getattr = nfs_getattr,
9306 .setattr = nfs_setattr,
9307 .getxattr = generic_getxattr,
9308 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009309 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009310 .removexattr = generic_removexattr,
9311};
9312
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009313static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009314 .permission = nfs_permission,
9315 .getattr = nfs_getattr,
9316 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009317 .getxattr = generic_getxattr,
9318 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009319 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009320 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009321};
9322
David Howells509de812006-08-22 20:06:11 -04009323const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 .version = 4, /* protocol version */
9325 .dentry_ops = &nfs4_dentry_operations,
9326 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009327 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009328 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009330 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009331 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 .getattr = nfs4_proc_getattr,
9333 .setattr = nfs4_proc_setattr,
9334 .lookup = nfs4_proc_lookup,
9335 .access = nfs4_proc_access,
9336 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337 .create = nfs4_proc_create,
9338 .remove = nfs4_proc_remove,
9339 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009340 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009341 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009342 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009343 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009344 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345 .link = nfs4_proc_link,
9346 .symlink = nfs4_proc_symlink,
9347 .mkdir = nfs4_proc_mkdir,
9348 .rmdir = nfs4_proc_remove,
9349 .readdir = nfs4_proc_readdir,
9350 .mknod = nfs4_proc_mknod,
9351 .statfs = nfs4_proc_statfs,
9352 .fsinfo = nfs4_proc_fsinfo,
9353 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009354 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009356 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009358 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009360 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009362 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009363 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009365 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009366 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009367 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009368 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009369 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009370 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009371 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009372 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009373 .create_server = nfs4_create_server,
9374 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375};
9376
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009377static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009378 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009379 .list = nfs4_xattr_list_nfs4_acl,
9380 .get = nfs4_xattr_get_nfs4_acl,
9381 .set = nfs4_xattr_set_nfs4_acl,
9382};
9383
9384const struct xattr_handler *nfs4_xattr_handlers[] = {
9385 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009386#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9387 &nfs4_xattr_nfs4_label_handler,
9388#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009389 NULL
9390};
9391
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392/*
9393 * Local variables:
9394 * c-basic-offset: 8
9395 * End:
9396 */