blob: 6bbcd2f24e222eec67701c913edabfb756e304ba [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 *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
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
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400366 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500368 int ret = errorcode;
369
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400370 exception->delay = 0;
371 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 exception->retry = 0;
373 switch(errorcode) {
374 case 0:
375 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500376 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400380 if (inode) {
381 int err;
382
383 err = nfs_async_inode_return_delegation(inode,
384 stateid);
385 if (err == 0)
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
389 break;
390 }
391 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 if (state == NULL)
393 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400394 ret = nfs4_schedule_stateid_recovery(server, state);
395 if (ret < 0)
396 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400398 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400399 if (state != NULL) {
400 ret = nfs4_schedule_stateid_recovery(server, state);
401 if (ret < 0)
402 break;
403 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500405 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400408 case -NFS4ERR_MOVED:
409 ret = nfs4_schedule_migration_recovery(server);
410 if (ret < 0)
411 break;
412 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500416#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
425 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400427 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500428#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
432 * fail
433 */
434 ret = -EBUSY;
435 break;
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400438 nfs_inc_server_stats(server, NFSIOS_DELAY);
439 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400440 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400441 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400442 exception->delay = 1;
443 return 0;
444
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800448 break;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
457 "uid/gids. "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
460 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 }
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500464wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->recovering = 1;
466 return 0;
467}
468
469/* This is the error handling routine for processes that are allowed
470 * to sleep.
471 */
472int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
473{
474 struct nfs_client *clp = server->nfs_client;
475 int ret;
476
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
480 goto out_retry;
481 }
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
485 return -EIO;
486 goto out_retry;
487 }
488 return ret;
489out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500490 if (ret == 0)
491 exception->retry = 1;
492 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500493}
494
Trond Myklebust037fc982015-09-20 15:51:00 -0400495static int
496nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
498{
499 struct nfs_client *clp = server->nfs_client;
500 int ret;
501
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
505 goto out_retry;
506 }
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
511 goto out_retry;
512 }
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 ret = -EIO;
515 return ret;
516out_retry:
517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
520}
521
522static int
523nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
525{
526 struct nfs4_exception exception = {
527 .state = state,
528 };
529
530 if (task->tk_status >= 0)
531 return 0;
532 if (timeout)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
535 task->tk_status,
536 &exception);
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
539 if (exception.retry)
540 return -EAGAIN;
541 return 0;
542}
543
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400544/*
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
547 */
548static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
549{
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
551
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
554 return true;
555
556 return false;
557}
Trond Myklebust65de8722008-12-23 15:21:44 -0500558
Trond Myklebust452e9352010-07-31 14:29:06 -0400559static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
565}
566
Trond Myklebust452e9352010-07-31 14:29:06 -0400567static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
568{
Trond Myklebustbe824162015-07-05 14:50:46 -0400569 struct nfs_client *clp = server->nfs_client;
570
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400573}
574
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400575struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
579};
580
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800581void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400583{
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
587
588 res->sr_slot = NULL;
589}
590
591static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
592{
593 args->sa_privileged = 1;
594}
595
Peng Taocb04ad22014-06-11 05:24:15 +0800596int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400600{
Chuck Lever3bd23842013-08-09 12:49:19 -0400601 struct nfs4_slot *slot;
602
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
605 goto out_start;
606
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
609 goto out_sleep;
610
611 slot = nfs4_alloc_slot(tbl);
612 if (IS_ERR(slot)) {
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
615 goto out_sleep;
616 }
617 spin_unlock(&tbl->slot_tbl_lock);
618
Trond Myklebust0a014a42016-09-22 13:38:51 -0400619 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400620 args->sa_slot = slot;
621 res->sr_slot = slot;
622
623out_start:
624 rpc_call_start(task);
625 return 0;
626
627out_sleep:
628 if (args->sa_privileged)
629 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
630 NULL, RPC_PRIORITY_PRIVILEGED);
631 else
632 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
633 spin_unlock(&tbl->slot_tbl_lock);
634 return -EAGAIN;
635}
Peng Taocb04ad22014-06-11 05:24:15 +0800636EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400637
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400638static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400639{
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
642
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 tbl = slot->table;
644 spin_lock(&tbl->slot_tbl_lock);
645 if (!nfs41_wake_and_assign_slot(tbl, slot))
646 nfs4_free_slot(tbl, slot);
647 spin_unlock(&tbl->slot_tbl_lock);
648
649 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400650}
651
652static int nfs40_sequence_done(struct rpc_task *task,
653 struct nfs4_sequence_res *res)
654{
655 if (res->sr_slot != NULL)
656 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400657 return 1;
658}
659
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400660#if defined(CONFIG_NFS_V4_1)
661
Trond Myklebustd185a332010-06-16 09:52:25 -0400662static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400663{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500664 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400665 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500667 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400668
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500669 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500670 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500671
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400672 /* Bump the slot sequence number */
673 if (slot->seq_done)
674 slot->seq_nr++;
675 slot->seq_done = 0;
676
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500677 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500678 /* Be nice to the server: try to ensure that the last transmitted
679 * value for highest_user_slotid <= target_highest_slotid
680 */
681 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
682 send_new_highest_used_slotid = true;
683
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500684 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500685 send_new_highest_used_slotid = false;
686 goto out_unlock;
687 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500688 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500689
690 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
691 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500692out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500693 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500695 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400696 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400697 if (waitqueue_active(&tbl->slot_waitq))
698 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400699}
700
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400701static int nfs41_sequence_process(struct rpc_task *task,
702 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400706 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500707 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500708 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400709
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500710 if (slot == NULL)
711 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400712 /* don't increment the sequence number if the task wasn't sent */
713 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400714 goto out;
715
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500716 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500717
Trond Myklebustac20d162012-12-15 15:36:07 -0500718 if (slot->interrupted) {
719 slot->interrupted = 0;
720 interrupted = true;
721 }
722
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400723 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500724 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 switch (res->sr_status) {
726 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400727 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400728 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500729 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500730 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500731 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400732 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
733 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500734 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500736 case 1:
737 /*
738 * sr_status remains 1 if an RPC level error occurred.
739 * The server may or may not have processed the sequence
740 * operation..
741 * Mark the slot as having hosted an interrupted RPC call.
742 */
743 slot->interrupted = 1;
744 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400745 case -NFS4ERR_DELAY:
746 /* The server detected a resend of the RPC call and
747 * returned NFS4ERR_DELAY as per Section 2.10.6.2
748 * of RFC5661.
749 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500750 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400751 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500752 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500753 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400754 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500755 case -NFS4ERR_BADSLOT:
756 /*
757 * The slot id we used was probably retired. Try again
758 * using a different slot id.
759 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500760 goto retry_nowait;
761 case -NFS4ERR_SEQ_MISORDERED:
762 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 * Was the last operation on this sequence interrupted?
764 * If so, retry after bumping the sequence number.
765 */
766 if (interrupted) {
767 ++slot->seq_nr;
768 goto retry_nowait;
769 }
770 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500771 * Could this slot have been previously retired?
772 * If so, then the server may be expecting seq_nr = 1!
773 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500774 if (slot->seq_nr != 1) {
775 slot->seq_nr = 1;
776 goto retry_nowait;
777 }
778 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500779 case -NFS4ERR_SEQ_FALSE_RETRY:
780 ++slot->seq_nr;
781 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 default:
783 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400784 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400785 }
786out:
787 /* The session may be reset by one of the error handlers. */
788 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500789out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500790 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500791retry_nowait:
792 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 task->tk_status = 0;
795 ret = 0;
796 }
797 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400799 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 goto out;
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
802 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400804
805int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (!nfs41_sequence_process(task, res))
808 return 0;
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
811 return 1;
812
813}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500814EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400815
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400816static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817{
818 if (res->sr_slot == NULL)
819 return 1;
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
823}
824
825static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826{
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
830 else
831 nfs40_sequence_free_slot(res);
832 }
833}
834
Peng Tao2c4b1312014-06-11 05:24:16 +0800835int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400836{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500837 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842}
Peng Tao2c4b1312014-06-11 05:24:16 +0800843EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000845int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846 struct nfs4_sequence_args *args,
847 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848 struct rpc_task *task)
849{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400850 struct nfs4_slot *slot;
851 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400852
853 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400855 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400856 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400857
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400858 tbl = &session->fc_slot_table;
859
Trond Myklebust69d206b2012-11-22 13:21:02 -0500860 task->tk_timeout = 0;
861
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400862 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400863 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400864 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500865 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500866 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400867 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400868 }
869
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500870 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500871 if (IS_ERR(slot)) {
872 /* If out of memory, try again in 1/4 second */
873 if (slot == ERR_PTR(-ENOMEM))
874 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400875 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400876 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400877 }
878 spin_unlock(&tbl->slot_tbl_lock);
879
Trond Myklebust0a014a42016-09-22 13:38:51 -0400880 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500881 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400882
Chuck Levere8d92382013-08-09 12:47:51 -0400883 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500884 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400885
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500887 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400888 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 /*
890 * sr_status is only set in decode_sequence, and so will remain
891 * set to 1 if an rpc level failure occurs.
892 */
893 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400894 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400895out_success:
896 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400899 /* Privileged tasks are queued with top priority */
900 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400901 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
902 NULL, RPC_PRIORITY_PRIVILEGED);
903 else
904 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400905 spin_unlock(&tbl->slot_tbl_lock);
906 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000908EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400909
Chuck Lever5a580e02013-08-09 12:48:09 -0400910static int nfs4_setup_sequence(const struct nfs_server *server,
911 struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400914{
Trond Myklebust035168a2010-06-16 09:52:26 -0400915 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400916 int ret = 0;
917
Chuck Lever3bd23842013-08-09 12:49:19 -0400918 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
920 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400921
Chuck Levere8d92382013-08-09 12:47:51 -0400922 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400923 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400924 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400925
Trond Myklebust9d12b212012-01-17 22:04:25 -0500926 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400927
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 dprintk("<-- %s status=%d\n", __func__, ret);
929 return ret;
930}
931
Andy Adamsonce5039c2009-04-01 09:22:13 -0400932static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
933{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400934 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400935 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400936
Trond Myklebust035168a2010-06-16 09:52:26 -0400937 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
938
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400939 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940}
941
Andy Adamson69ab40c2009-04-01 09:22:19 -0400942static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
943{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400944 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400945
Trond Myklebust14516c32010-07-31 14:29:06 -0400946 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400947}
948
Trond Myklebust17280172012-03-11 13:11:00 -0400949static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400951 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400952};
953
Chuck Lever3bd23842013-08-09 12:49:19 -0400954#else /* !CONFIG_NFS_V4_1 */
955
Chuck Lever5a580e02013-08-09 12:48:09 -0400956static int nfs4_setup_sequence(const struct nfs_server *server,
957 struct nfs4_sequence_args *args,
958 struct nfs4_sequence_res *res,
959 struct rpc_task *task)
960{
Peng Taocb04ad22014-06-11 05:24:15 +0800961 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
962 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400963}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400964
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400965static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
966{
967 return nfs40_sequence_done(task, res);
968}
969
970static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
971{
972 if (res->sr_slot != NULL)
973 nfs40_sequence_free_slot(res);
974}
975
Peng Tao2c4b1312014-06-11 05:24:16 +0800976int nfs4_sequence_done(struct rpc_task *task,
977 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400978{
Chuck Lever3bd23842013-08-09 12:49:19 -0400979 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400980}
Peng Tao2c4b1312014-06-11 05:24:16 +0800981EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400982
983#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400984
Chuck Lever9915ea72013-08-09 12:48:27 -0400985static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
986{
987 struct nfs4_call_sync_data *data = calldata;
988 nfs4_setup_sequence(data->seq_server,
989 data->seq_args, data->seq_res, task);
990}
991
992static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
993{
994 struct nfs4_call_sync_data *data = calldata;
995 nfs4_sequence_done(task, data->seq_res);
996}
997
998static const struct rpc_call_ops nfs40_call_sync_ops = {
999 .rpc_call_prepare = nfs40_call_sync_prepare,
1000 .rpc_call_done = nfs40_call_sync_done,
1001};
1002
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001003static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001004 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs4_sequence_args *args,
1007 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001008{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 int ret;
1010 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001011 struct nfs_client *clp = server->nfs_client;
1012 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001013 .seq_server = server,
1014 .seq_args = args,
1015 .seq_res = res,
1016 };
1017 struct rpc_task_setup task_setup = {
1018 .rpc_client = clnt,
1019 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001020 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001021 .callback_data = &data
1022 };
1023
1024 task = rpc_run_task(&task_setup);
1025 if (IS_ERR(task))
1026 ret = PTR_ERR(task);
1027 else {
1028 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001029 rpc_put_task(task);
1030 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001031 return ret;
1032}
1033
Bryan Schumaker7c513052011-03-24 17:12:24 +00001034int nfs4_call_sync(struct rpc_clnt *clnt,
1035 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001036 struct rpc_message *msg,
1037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 int cache_reply)
1040{
Chuck Levera9c92d62013-08-09 12:48:18 -04001041 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1046{
1047 struct nfs_inode *nfsi = NFS_I(dir);
1048
1049 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001051 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001053 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001054 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001055 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 spin_unlock(&dir->i_lock);
1057}
1058
1059struct nfs4_opendata {
1060 struct kref kref;
1061 struct nfs_openargs o_arg;
1062 struct nfs_openres o_res;
1063 struct nfs_open_confirmargs c_arg;
1064 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 struct nfs4_string owner_name;
1066 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001067 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001069 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001071 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct nfs4_state_owner *owner;
1073 struct nfs4_state *state;
1074 struct iattr attrs;
1075 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001076 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001077 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001078 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001079 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int cancelled;
1081};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001082
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001083static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1084 int err, struct nfs4_exception *exception)
1085{
1086 if (err != -EINVAL)
1087 return false;
1088 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1089 return false;
1090 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1091 exception->retry = 1;
1092 return true;
1093}
1094
Trond Myklebust6ae37332015-01-30 14:21:14 -05001095static u32
1096nfs4_map_atomic_open_share(struct nfs_server *server,
1097 fmode_t fmode, int openflags)
1098{
1099 u32 res = 0;
1100
1101 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1102 case FMODE_READ:
1103 res = NFS4_SHARE_ACCESS_READ;
1104 break;
1105 case FMODE_WRITE:
1106 res = NFS4_SHARE_ACCESS_WRITE;
1107 break;
1108 case FMODE_READ|FMODE_WRITE:
1109 res = NFS4_SHARE_ACCESS_BOTH;
1110 }
1111 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1112 goto out;
1113 /* Want no delegation if we're using O_DIRECT */
1114 if (openflags & O_DIRECT)
1115 res |= NFS4_SHARE_WANT_NO_DELEG;
1116out:
1117 return res;
1118}
1119
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001120static enum open_claim_type4
1121nfs4_map_atomic_open_claim(struct nfs_server *server,
1122 enum open_claim_type4 claim)
1123{
1124 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1125 return claim;
1126 switch (claim) {
1127 default:
1128 return claim;
1129 case NFS4_OPEN_CLAIM_FH:
1130 return NFS4_OPEN_CLAIM_NULL;
1131 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1132 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1133 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1134 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1135 }
1136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139{
1140 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001141 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001142 p->o_res.seqid = p->o_arg.seqid;
1143 p->c_res.seqid = p->c_arg.seqid;
1144 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001145 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001147 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001148}
1149
Al Viro82a2c1b2011-06-22 18:30:55 -04001150static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001151 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001152 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001153 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001154 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001155 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156{
Al Viro82a2c1b2011-06-22 18:30:55 -04001157 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001158 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001160 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 struct nfs4_opendata *p;
1162
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001163 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001164 if (p == NULL)
1165 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001166
1167 p->f_label = nfs4_label_alloc(server, gfp_mask);
1168 if (IS_ERR(p->f_label))
1169 goto err_free_p;
1170
Kinglong Meea49c2692015-07-27 15:31:38 +08001171 p->a_label = nfs4_label_alloc(server, gfp_mask);
1172 if (IS_ERR(p->a_label))
1173 goto err_free_f;
1174
Trond Myklebust63f5f792015-01-23 19:19:25 -05001175 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1176 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001177 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001178 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001179 nfs_sb_active(dentry->d_sb);
1180 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001181 p->dir = parent;
1182 p->owner = sp;
1183 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 p->o_arg.open_flags = flags;
1185 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001186 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1187 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001188 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1189 * will return permission denied for all bits until close */
1190 if (!(flags & O_EXCL)) {
1191 /* ask server to check for all possible rights as results
1192 * are cached */
1193 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1194 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001195 }
David Howells7539bba2006-08-22 20:06:09 -04001196 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001197 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1198 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001199 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001201 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001202 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001203 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001204 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1205 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001206 case NFS4_OPEN_CLAIM_NULL:
1207 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1208 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1209 p->o_arg.fh = NFS_FH(dir);
1210 break;
1211 case NFS4_OPEN_CLAIM_PREVIOUS:
1212 case NFS4_OPEN_CLAIM_FH:
1213 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1214 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001215 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001216 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001217 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001218 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001219
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 p->o_arg.u.attrs = &p->attrs;
1221 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001222
1223 verf[0] = jiffies;
1224 verf[1] = current->pid;
1225 memcpy(p->o_arg.u.verifier.data, verf,
1226 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001228 p->c_arg.fh = &p->o_res.fh;
1229 p->c_arg.stateid = &p->o_res.stateid;
1230 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001232 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001234
1235err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001236 nfs4_label_free(p->a_label);
1237err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001238 nfs4_label_free(p->f_label);
1239err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001240 kfree(p);
1241err:
1242 dput(parent);
1243 return NULL;
1244}
1245
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001246static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 struct nfs4_opendata *p = container_of(kref,
1249 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001250 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251
1252 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001253 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001254 if (p->state != NULL)
1255 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001256 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001257
Kinglong Meea49c2692015-07-27 15:31:38 +08001258 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001259 nfs4_label_free(p->f_label);
1260
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001261 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001262 dput(p->dentry);
1263 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001264 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001265 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001266 kfree(p);
1267}
1268
1269static void nfs4_opendata_put(struct nfs4_opendata *p)
1270{
1271 if (p != NULL)
1272 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001273}
1274
Trond Myklebust06f814a2006-01-03 09:55:07 +01001275static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1276{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001277 int ret;
1278
Trond Myklebust06f814a2006-01-03 09:55:07 +01001279 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001280 return ret;
1281}
1282
Trond Myklebust24311f82015-09-20 10:50:17 -04001283static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1284 fmode_t fmode)
1285{
1286 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1287 case FMODE_READ|FMODE_WRITE:
1288 return state->n_rdwr != 0;
1289 case FMODE_WRITE:
1290 return state->n_wronly != 0;
1291 case FMODE_READ:
1292 return state->n_rdonly != 0;
1293 }
1294 WARN_ON_ONCE(1);
1295 return false;
1296}
1297
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001299{
1300 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301
Trond Myklebust536e43d2012-01-17 22:04:26 -05001302 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001303 goto out;
1304 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001306 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1307 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308 break;
1309 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001310 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1311 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001312 break;
1313 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001314 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1315 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001318 return ret;
1319}
1320
Trond Myklebust2a606182015-08-19 22:30:00 -05001321static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1322 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001323{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001324 if (delegation == NULL)
1325 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001327 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001328 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1329 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001330 switch (claim) {
1331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_FH:
1333 break;
1334 case NFS4_OPEN_CLAIM_PREVIOUS:
1335 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1336 break;
1337 default:
1338 return 0;
1339 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001340 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 return 1;
1342}
1343
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001345{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001347 case FMODE_WRITE:
1348 state->n_wronly++;
1349 break;
1350 case FMODE_READ:
1351 state->n_rdonly++;
1352 break;
1353 case FMODE_READ|FMODE_WRITE:
1354 state->n_rdwr++;
1355 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001357}
1358
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001360{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 struct nfs_client *clp = state->owner->so_server->nfs_client;
1362 bool need_recover = false;
1363
1364 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1365 need_recover = true;
1366 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1367 need_recover = true;
1368 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1369 need_recover = true;
1370 if (need_recover)
1371 nfs4_state_mark_reclaim_nograce(clp, state);
1372}
1373
Trond Myklebuste999e802014-02-10 18:20:47 -05001374static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1375 nfs4_stateid *stateid)
1376{
1377 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1378 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001379 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1380 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001381 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001382 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001383 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1384 return true;
1385 return false;
1386}
1387
Trond Myklebustf95549c2015-01-23 18:06:09 -05001388static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1389{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001390 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1391 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001392 if (state->n_wronly)
1393 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 if (state->n_rdonly)
1395 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1396 if (state->n_rdwr)
1397 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001398 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001399}
1400
Trond Myklebust226056c2014-02-11 10:41:07 -05001401static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001402 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001403 nfs4_stateid *stateid, fmode_t fmode)
1404{
1405 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1406 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1407 case FMODE_WRITE:
1408 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1409 break;
1410 case FMODE_READ:
1411 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1412 break;
1413 case 0:
1414 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1415 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1416 clear_bit(NFS_OPEN_STATE, &state->flags);
1417 }
1418 if (stateid == NULL)
1419 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001420 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001421 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1422 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1423 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001424 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001425 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001426 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001428 nfs4_stateid_copy(&state->stateid, stateid);
1429 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001430}
1431
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001432static void nfs_clear_open_stateid(struct nfs4_state *state,
1433 nfs4_stateid *arg_stateid,
1434 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001435{
1436 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001437 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001438 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001439 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1440 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001441}
1442
Trond Myklebust003707c2007-07-05 18:07:55 -04001443static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1444{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001446 case FMODE_READ:
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1448 break;
1449 case FMODE_WRITE:
1450 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1451 break;
1452 case FMODE_READ|FMODE_WRITE:
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
1454 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001455 if (!nfs_need_update_open_stateid(state, stateid))
1456 return;
1457 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1458 nfs4_stateid_copy(&state->stateid, stateid);
1459 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001460}
1461
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001464 /*
1465 * Protect the call to nfs4_state_set_mode_locked and
1466 * serialise the stateid update
1467 */
Andrew Elble361cad32015-12-02 09:20:57 -05001468 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001469 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001470 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001471 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001472 set_bit(NFS_DELEGATED_STATE, &state->flags);
1473 }
1474 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001476 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001478 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001481static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001482{
1483 struct nfs_inode *nfsi = NFS_I(state->inode);
1484 struct nfs_delegation *deleg_cur;
1485 int ret = 0;
1486
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001488
1489 rcu_read_lock();
1490 deleg_cur = rcu_dereference(nfsi->delegation);
1491 if (deleg_cur == NULL)
1492 goto no_delegation;
1493
1494 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001495 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001496 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001497 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001498 goto no_delegation_unlock;
1499
1500 if (delegation == NULL)
1501 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001502 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001503 goto no_delegation_unlock;
1504
Trond Myklebustb7391f42008-12-23 15:21:52 -05001505 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001506 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508no_delegation_unlock:
1509 spin_unlock(&deleg_cur->lock);
1510no_delegation:
1511 rcu_read_unlock();
1512
1513 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001514 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001515 ret = 1;
1516 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001517 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1518 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001519
1520 return ret;
1521}
1522
Trond Myklebust39071e62015-01-24 15:07:56 -05001523static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1524 const nfs4_stateid *stateid)
1525{
1526 struct nfs4_state *state = lsp->ls_state;
1527 bool ret = false;
1528
1529 spin_lock(&state->state_lock);
1530 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1531 goto out_noupdate;
1532 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1533 goto out_noupdate;
1534 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1535 ret = true;
1536out_noupdate:
1537 spin_unlock(&state->state_lock);
1538 return ret;
1539}
Trond Myklebust34310432008-12-23 15:21:38 -05001540
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542{
1543 struct nfs_delegation *delegation;
1544
1545 rcu_read_lock();
1546 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 rcu_read_unlock();
1549 return;
1550 }
1551 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001552 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553}
1554
Trond Myklebust6ee41262007-07-08 14:11:36 -04001555static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001556{
1557 struct nfs4_state *state = opendata->state;
1558 struct nfs_inode *nfsi = NFS_I(state->inode);
1559 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001560 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001562 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 nfs4_stateid stateid;
1564 int ret = -EAGAIN;
1565
Trond Myklebustaac00a82007-07-05 19:02:21 -04001566 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001567 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001568 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001569 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001570 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001571 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001572 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001573 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001574 rcu_read_lock();
1575 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001576 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001577 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001578 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001579 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001580 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001581 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001582 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001583 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001584 if (!opendata->is_recover) {
1585 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1586 if (ret != 0)
1587 goto out;
1588 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001590
1591 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001592 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001593 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001594 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001595out:
1596 return ERR_PTR(ret);
1597out_return_state:
1598 atomic_inc(&state->count);
1599 return state;
1600}
1601
Andy Adamsone23008e2012-10-02 21:07:32 -04001602static void
1603nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1604{
1605 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1606 struct nfs_delegation *delegation;
1607 int delegation_flags = 0;
1608
1609 rcu_read_lock();
1610 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1611 if (delegation)
1612 delegation_flags = delegation->flags;
1613 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001614 switch (data->o_arg.claim) {
1615 default:
1616 break;
1617 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1618 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001619 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1620 "returning a delegation for "
1621 "OPEN(CLAIM_DELEGATE_CUR)\n",
1622 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001623 return;
1624 }
1625 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001626 nfs_inode_set_delegation(state->inode,
1627 data->owner->so_cred,
1628 &data->o_res);
1629 else
1630 nfs_inode_reclaim_delegation(state->inode,
1631 data->owner->so_cred,
1632 &data->o_res);
1633}
1634
1635/*
1636 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1637 * and update the nfs4_state.
1638 */
1639static struct nfs4_state *
1640_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1641{
1642 struct inode *inode = data->state->inode;
1643 struct nfs4_state *state = data->state;
1644 int ret;
1645
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001646 if (!data->rpc_done) {
1647 if (data->rpc_status) {
1648 ret = data->rpc_status;
1649 goto err;
1650 }
1651 /* cached opens have already been processed */
1652 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001653 }
1654
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 ret = nfs_refresh_inode(inode, &data->f_attr);
1656 if (ret)
1657 goto err;
1658
1659 if (data->o_res.delegation_type != 0)
1660 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001661update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001662 update_open_stateid(state, &data->o_res.stateid, NULL,
1663 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001664 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001665
1666 return state;
1667err:
1668 return ERR_PTR(ret);
1669
1670}
1671
1672static struct nfs4_state *
1673_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674{
1675 struct inode *inode;
1676 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001680 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001681 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682 goto out;
1683 }
1684
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001687 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001688 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001689 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001690 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001691 goto err;
1692 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 state = nfs4_get_open_state(inode, data->owner);
1694 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001695 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001696 if (data->o_res.delegation_type != 0)
1697 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001698 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001699 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001701out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001702 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001704err_put_inode:
1705 iput(inode);
1706err:
1707 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708}
1709
Andy Adamsone23008e2012-10-02 21:07:32 -04001710static struct nfs4_state *
1711nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1712{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001713 struct nfs4_state *ret;
1714
Andy Adamsone23008e2012-10-02 21:07:32 -04001715 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001716 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1717 else
1718 ret = _nfs4_opendata_to_nfs4_state(data);
1719 nfs4_sequence_free_slot(&data->o_res.seq_res);
1720 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001721}
1722
Trond Myklebust864472e2006-01-03 09:55:15 +01001723static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1724{
1725 struct nfs_inode *nfsi = NFS_I(state->inode);
1726 struct nfs_open_context *ctx;
1727
1728 spin_lock(&state->inode->i_lock);
1729 list_for_each_entry(ctx, &nfsi->open_files, list) {
1730 if (ctx->state != state)
1731 continue;
1732 get_nfs_open_context(ctx);
1733 spin_unlock(&state->inode->i_lock);
1734 return ctx;
1735 }
1736 spin_unlock(&state->inode->i_lock);
1737 return ERR_PTR(-ENOENT);
1738}
1739
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001740static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1741 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001742{
1743 struct nfs4_opendata *opendata;
1744
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001745 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001746 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001747 if (opendata == NULL)
1748 return ERR_PTR(-ENOMEM);
1749 opendata->state = state;
1750 atomic_inc(&state->count);
1751 return opendata;
1752}
1753
Trond Myklebust24311f82015-09-20 10:50:17 -04001754static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1755 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001756{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 int ret;
1759
Trond Myklebust24311f82015-09-20 10:50:17 -04001760 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001761 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001762 opendata->o_arg.open_flags = 0;
1763 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001764 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1765 NFS_SB(opendata->dentry->d_sb),
1766 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001767 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1768 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1769 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001770 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001771 if (ret != 0)
1772 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001773 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774 if (IS_ERR(newstate))
1775 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001776 if (newstate != opendata->state)
1777 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001778 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001779 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001780}
1781
1782static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1783{
Trond Myklebust864472e2006-01-03 09:55:15 +01001784 int ret;
1785
Trond Myklebust4f14c192014-02-12 19:15:06 -05001786 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1787 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1788 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1789 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001791 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001792 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001793 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001794 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1795 if (ret != 0)
1796 return ret;
1797 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1798 if (ret != 0)
1799 return ret;
1800 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1801 if (ret != 0)
1802 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 /*
1804 * We may have performed cached opens for all three recoveries.
1805 * Check if we need to update the current stateid.
1806 */
1807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001808 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001809 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001810 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001811 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001812 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 return 0;
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * OPEN_RECLAIM:
1819 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001821static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001823 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001824 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001825 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int status;
1827
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001828 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1829 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001830 if (IS_ERR(opendata))
1831 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001832 rcu_read_lock();
1833 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001834 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001835 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001836 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001837 opendata->o_arg.u.delegation_type = delegation_type;
1838 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001839 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return status;
1841}
1842
Trond Myklebust539cd032007-06-05 11:46:42 -04001843static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct nfs_server *server = NFS_SERVER(state->inode);
1846 struct nfs4_exception exception = { };
1847 int err;
1848 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001849 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001850 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001851 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1852 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001853 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001854 break;
1855 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 } while (exception.retry);
1857 return err;
1858}
1859
Trond Myklebust864472e2006-01-03 09:55:15 +01001860static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1861{
1862 struct nfs_open_context *ctx;
1863 int ret;
1864
1865 ctx = nfs4_state_find_open_context(state);
1866 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001867 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001868 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001869 put_nfs_open_context(ctx);
1870 return ret;
1871}
1872
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001873static 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 -07001874{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001875 switch (err) {
1876 default:
1877 printk(KERN_ERR "NFS: %s: unhandled error "
1878 "%d.\n", __func__, err);
1879 case 0:
1880 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001881 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001882 case -ESTALE:
1883 break;
1884 case -NFS4ERR_BADSESSION:
1885 case -NFS4ERR_BADSLOT:
1886 case -NFS4ERR_BAD_HIGH_SLOT:
1887 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1888 case -NFS4ERR_DEADSESSION:
1889 set_bit(NFS_DELEGATED_STATE, &state->flags);
1890 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1891 return -EAGAIN;
1892 case -NFS4ERR_STALE_CLIENTID:
1893 case -NFS4ERR_STALE_STATEID:
1894 set_bit(NFS_DELEGATED_STATE, &state->flags);
1895 case -NFS4ERR_EXPIRED:
1896 /* Don't recall a delegation if it was lost */
1897 nfs4_schedule_lease_recovery(server->nfs_client);
1898 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001899 case -NFS4ERR_MOVED:
1900 nfs4_schedule_migration_recovery(server);
1901 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001902 case -NFS4ERR_LEASE_MOVED:
1903 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1904 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001905 case -NFS4ERR_DELEG_REVOKED:
1906 case -NFS4ERR_ADMIN_REVOKED:
1907 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001908 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001909 nfs_inode_find_state_and_recover(state->inode,
1910 stateid);
1911 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001912 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001913 case -NFS4ERR_DELAY:
1914 case -NFS4ERR_GRACE:
1915 set_bit(NFS_DELEGATED_STATE, &state->flags);
1916 ssleep(1);
1917 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001918 case -ENOMEM:
1919 case -NFS4ERR_DENIED:
1920 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1921 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return err;
1924}
1925
Trond Myklebust24311f82015-09-20 10:50:17 -04001926int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1927 struct nfs4_state *state, const nfs4_stateid *stateid,
1928 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001929{
1930 struct nfs_server *server = NFS_SERVER(state->inode);
1931 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001933
1934 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1935 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1936 if (IS_ERR(opendata))
1937 return PTR_ERR(opendata);
1938 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001939 write_seqlock(&state->seqlock);
1940 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1941 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1943 switch (type & (FMODE_READ|FMODE_WRITE)) {
1944 case FMODE_READ|FMODE_WRITE:
1945 case FMODE_WRITE:
1946 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1947 if (err)
1948 break;
1949 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1950 if (err)
1951 break;
1952 case FMODE_READ:
1953 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1954 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001955 nfs4_opendata_put(opendata);
1956 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1957}
1958
Chuck Leverbe05c862013-08-09 12:49:47 -04001959static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1960{
1961 struct nfs4_opendata *data = calldata;
1962
Peng Taocb04ad22014-06-11 05:24:15 +08001963 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1964 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001965}
1966
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001967static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1968{
1969 struct nfs4_opendata *data = calldata;
1970
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001971 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001972
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001973 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001974 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001975 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001976 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001977 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001978 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001979 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001980}
1981
1982static void nfs4_open_confirm_release(void *calldata)
1983{
1984 struct nfs4_opendata *data = calldata;
1985 struct nfs4_state *state = NULL;
1986
1987 /* If this request hasn't been cancelled, do nothing */
1988 if (data->cancelled == 0)
1989 goto out_free;
1990 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001991 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001993 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001994 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001995 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001996out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001997 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001998}
1999
2000static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002001 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 .rpc_call_done = nfs4_open_confirm_done,
2003 .rpc_release = nfs4_open_confirm_release,
2004};
2005
2006/*
2007 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2008 */
2009static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2010{
David Howells2b0143b2015-03-17 22:25:59 +00002011 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002012 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002013 struct rpc_message msg = {
2014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2015 .rpc_argp = &data->c_arg,
2016 .rpc_resp = &data->c_res,
2017 .rpc_cred = data->owner->so_cred,
2018 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002019 struct rpc_task_setup task_setup_data = {
2020 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002021 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 .callback_ops = &nfs4_open_confirm_ops,
2023 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002024 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002025 .flags = RPC_TASK_ASYNC,
2026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int status;
2028
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002029 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002030 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002031 data->rpc_done = 0;
2032 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002033 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002034 if (data->is_recover)
2035 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002036 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002037 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002038 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002039 status = nfs4_wait_for_completion_rpc_task(task);
2040 if (status != 0) {
2041 data->cancelled = 1;
2042 smp_wmb();
2043 } else
2044 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002045 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return status;
2047}
2048
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002049static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002051 struct nfs4_opendata *data = calldata;
2052 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002053 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002054 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002055
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002056 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002057 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002058 /*
2059 * Check if we still need to send an OPEN call, or if we can use
2060 * a delegation instead.
2061 */
2062 if (data->state != NULL) {
2063 struct nfs_delegation *delegation;
2064
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002065 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002066 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002067 rcu_read_lock();
2068 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002069 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002070 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002071 rcu_read_unlock();
2072 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002073 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002074 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002075 switch (claim) {
2076 default:
2077 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002078 case NFS4_OPEN_CLAIM_PREVIOUS:
2079 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2080 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002081 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002082 case NFS4_OPEN_CLAIM_FH:
2083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002084 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2085 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002086 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002087 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002088 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002089 &data->o_res.seq_res,
2090 task) != 0)
2091 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002092
2093 /* Set the create mode (note dependency on the session type) */
2094 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2095 if (data->o_arg.open_flags & O_EXCL) {
2096 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2097 if (nfs4_has_persistent_session(clp))
2098 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2099 else if (clp->cl_mvops->minor_version > 0)
2100 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2101 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002102 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002103unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002104 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002105 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002106out_no_action:
2107 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002108out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002109 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002110}
2111
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112static void nfs4_open_done(struct rpc_task *task, void *calldata)
2113{
2114 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002117
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002118 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002119 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002120
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002122 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2123 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002124 case S_IFREG:
2125 break;
2126 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128 break;
2129 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002131 break;
2132 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002134 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002135 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002136 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002137 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2138 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002139 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002140 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002141}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002142
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143static void nfs4_open_release(void *calldata)
2144{
2145 struct nfs4_opendata *data = calldata;
2146 struct nfs4_state *state = NULL;
2147
2148 /* If this request hasn't been cancelled, do nothing */
2149 if (data->cancelled == 0)
2150 goto out_free;
2151 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002152 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002153 goto out_free;
2154 /* In case we need an open_confirm, no cleanup! */
2155 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2156 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002158 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002159 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002161 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002162}
2163
2164static const struct rpc_call_ops nfs4_open_ops = {
2165 .rpc_call_prepare = nfs4_open_prepare,
2166 .rpc_call_done = nfs4_open_done,
2167 .rpc_release = nfs4_open_release,
2168};
2169
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002170static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002171{
David Howells2b0143b2015-03-17 22:25:59 +00002172 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002173 struct nfs_server *server = NFS_SERVER(dir);
2174 struct nfs_openargs *o_arg = &data->o_arg;
2175 struct nfs_openres *o_res = &data->o_res;
2176 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2179 .rpc_argp = o_arg,
2180 .rpc_resp = o_res,
2181 .rpc_cred = data->owner->so_cred,
2182 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 struct rpc_task_setup task_setup_data = {
2184 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 .callback_ops = &nfs4_open_ops,
2187 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002188 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 .flags = RPC_TASK_ASYNC,
2190 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 int status;
2192
Chuck Levera9c92d62013-08-09 12:48:18 -04002193 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002194 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002195 data->rpc_done = 0;
2196 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002197 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002198 data->is_recover = 0;
2199 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002200 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002201 data->is_recover = 1;
2202 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002203 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002204 if (IS_ERR(task))
2205 return PTR_ERR(task);
2206 status = nfs4_wait_for_completion_rpc_task(task);
2207 if (status != 0) {
2208 data->cancelled = 1;
2209 smp_wmb();
2210 } else
2211 status = data->rpc_status;
2212 rpc_put_task(task);
2213
2214 return status;
2215}
2216
2217static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2218{
David Howells2b0143b2015-03-17 22:25:59 +00002219 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 struct nfs_openres *o_res = &data->o_res;
2221 int status;
2222
2223 status = nfs4_run_open_task(data, 1);
2224 if (status != 0 || !data->rpc_done)
2225 return status;
2226
Trond Myklebust6926afd2012-01-07 13:22:46 -05002227 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2228
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002229 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2230 status = _nfs4_proc_open_confirm(data);
2231 if (status != 0)
2232 return status;
2233 }
2234
2235 return status;
2236}
2237
Trond Myklebustf3792d62014-07-10 08:54:32 -04002238/*
2239 * Additional permission checks in order to distinguish between an
2240 * open for read, and an open for execute. This works around the
2241 * fact that NFSv4 OPEN treats read and execute permissions as being
2242 * the same.
2243 * Note that in the non-execute case, we want to turn off permission
2244 * checking if we just created a new file (POSIX open() semantics).
2245 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002246static int nfs4_opendata_access(struct rpc_cred *cred,
2247 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002248 struct nfs4_state *state, fmode_t fmode,
2249 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002250{
2251 struct nfs_access_entry cache;
2252 u32 mask;
2253
2254 /* access call failed or for some reason the server doesn't
2255 * support any access modes -- defer access call until later */
2256 if (opendata->o_res.access_supported == 0)
2257 return 0;
2258
2259 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002260 /*
2261 * Use openflags to check for exec, because fmode won't
2262 * always have FMODE_EXEC set when file open for exec.
2263 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002264 if (openflags & __FMODE_EXEC) {
2265 /* ONLY check for exec rights */
2266 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002267 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002268 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002269
2270 cache.cred = cred;
2271 cache.jiffies = jiffies;
2272 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2273 nfs_access_add_cache(state->inode, &cache);
2274
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002275 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002276 return 0;
2277
2278 /* even though OPEN succeeded, access is denied. Close the file */
2279 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002280 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002281}
2282
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002283/*
2284 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2285 */
2286static int _nfs4_proc_open(struct nfs4_opendata *data)
2287{
David Howells2b0143b2015-03-17 22:25:59 +00002288 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002289 struct nfs_server *server = NFS_SERVER(dir);
2290 struct nfs_openargs *o_arg = &data->o_arg;
2291 struct nfs_openres *o_res = &data->o_res;
2292 int status;
2293
2294 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002295 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002296 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002297 if (status != 0) {
2298 if (status == -NFS4ERR_BADNAME &&
2299 !(o_arg->open_flags & O_CREAT))
2300 return -ENOENT;
2301 return status;
2302 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303
Trond Myklebust6926afd2012-01-07 13:22:46 -05002304 nfs_fattr_map_and_free_names(server, &data->f_attr);
2305
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002306 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002307 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002308 if (o_arg->open_flags & O_EXCL)
2309 data->file_created = 1;
2310 else if (o_res->cinfo.before != o_res->cinfo.after)
2311 data->file_created = 1;
2312 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002313 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2314 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002321 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Andy Adamsond83217c2011-03-01 01:34:17 +00002325static int nfs4_recover_expired_lease(struct nfs_server *server)
2326{
2327 return nfs4_client_recover_expired_lease(server->nfs_client);
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/*
2331 * OPEN_EXPIRED:
2332 * reclaim state on the server after a network partition.
2333 * Assumes caller holds the appropriate lock
2334 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002335static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002337 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002338 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002340 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002341 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002342 if (IS_ERR(opendata))
2343 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002344 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002345 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002346 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002347 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002351static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002352{
Trond Myklebust539cd032007-06-05 11:46:42 -04002353 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002354 struct nfs4_exception exception = { };
2355 int err;
2356
2357 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002358 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002359 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2361 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002362 switch (err) {
2363 default:
2364 goto out;
2365 case -NFS4ERR_GRACE:
2366 case -NFS4ERR_DELAY:
2367 nfs4_handle_exception(server, err, &exception);
2368 err = 0;
2369 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002370 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002371out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002372 return err;
2373}
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Trond Myklebust864472e2006-01-03 09:55:15 +01002380 ctx = nfs4_state_find_open_context(state);
2381 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002382 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002383 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002384 put_nfs_open_context(ctx);
2385 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002388static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2389{
2390 nfs_remove_bad_delegation(state->inode);
2391 write_seqlock(&state->seqlock);
2392 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2393 write_sequnlock(&state->seqlock);
2394 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2395}
2396
2397static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2398{
2399 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2400 nfs_finish_clear_delegation_stateid(state);
2401}
2402
2403static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2404{
2405 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2406 nfs40_clear_delegation_stateid(state);
2407 return nfs4_open_expired(sp, state);
2408}
2409
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002410#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002411static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002412{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002413 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002414 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002415 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002416 struct rpc_cred *cred;
2417 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002418
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002419 /* Get the delegation credential for use by test/free_stateid */
2420 rcu_read_lock();
2421 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002422 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002423 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002424 return;
2425 }
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002426 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2427 rcu_read_unlock();
2428 nfs_finish_clear_delegation_stateid(state);
2429 return;
2430 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002431
2432 nfs4_stateid_copy(&stateid, &delegation->stateid);
2433 cred = get_rpccred(delegation->cred);
2434 rcu_read_unlock();
2435 status = nfs41_test_stateid(server, &stateid, cred);
2436 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002437
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002438 if (status != NFS_OK) {
2439 /* Free the stateid unless the server explicitly
2440 * informs us the stateid is unrecognized. */
2441 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002442 nfs41_free_stateid(server, &stateid, cred);
2443 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002444 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002445
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002446 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002447}
2448
2449/**
2450 * nfs41_check_open_stateid - possibly free an open stateid
2451 *
2452 * @state: NFSv4 state for an inode
2453 *
2454 * Returns NFS_OK if recovery for this stateid is now finished.
2455 * Otherwise a negative NFS4ERR value is returned.
2456 */
2457static int nfs41_check_open_stateid(struct nfs4_state *state)
2458{
2459 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002460 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002461 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002462 int status;
2463
2464 /* If a state reset has been done, test_stateid is unneeded */
2465 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2466 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2467 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2468 return -NFS4ERR_BAD_STATEID;
2469
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002470 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002471 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002472 if (status != NFS_OK) {
2473 /* Free the stateid unless the server explicitly
2474 * informs us the stateid is unrecognized. */
2475 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002476 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002477
2478 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2479 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2480 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002481 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002482 }
2483 return status;
2484}
2485
2486static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2487{
Chuck Levereb64cf92012-07-11 16:30:05 -04002488 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002489
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002490 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002491 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002492 if (status != NFS_OK)
2493 status = nfs4_open_expired(sp, state);
2494 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002495}
2496#endif
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002499 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2500 * fields corresponding to attributes that were used to store the verifier.
2501 * Make sure we clobber those fields in the later setattr call
2502 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002503static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2504 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002505{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002506 const u32 *attrset = opendata->o_res.attrset;
2507
2508 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002509 !(sattr->ia_valid & ATTR_ATIME_SET))
2510 sattr->ia_valid |= ATTR_ATIME;
2511
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002512 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002513 !(sattr->ia_valid & ATTR_MTIME_SET))
2514 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002515
2516 /* Except MODE, it seems harmless of setting twice. */
2517 if ((attrset[1] & FATTR4_WORD1_MODE))
2518 sattr->ia_valid &= ~ATTR_MODE;
2519
2520 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2521 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002522}
2523
Trond Myklebustc21443c2013-02-07 14:26:21 -05002524static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2525 fmode_t fmode,
2526 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002527 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002528{
2529 struct nfs4_state_owner *sp = opendata->owner;
2530 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002531 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002532 struct nfs4_state *state;
2533 unsigned int seq;
2534 int ret;
2535
2536 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2537
2538 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002539 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002540 goto out;
2541
2542 state = nfs4_opendata_to_nfs4_state(opendata);
2543 ret = PTR_ERR(state);
2544 if (IS_ERR(state))
2545 goto out;
2546 if (server->caps & NFS_CAP_POSIX_LOCK)
2547 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002548 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2549 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002550
Trond Myklebust275bb302013-05-29 13:11:28 -04002551 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002552 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002553 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002554 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002555 alias = d_exact_alias(dentry, state->inode);
2556 if (!alias)
2557 alias = d_splice_alias(igrab(state->inode), dentry);
2558 /* d_splice_alias() can't fail here - it's a non-directory */
2559 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002560 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002561 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002562 }
2563 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002564 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002565 }
2566
Trond Myklebustc21443c2013-02-07 14:26:21 -05002567 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2568 if (ret != 0)
2569 goto out;
2570
Trond Myklebust3efb9722013-05-29 13:17:04 -04002571 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002572 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002573 nfs_inode_attach_open_context(ctx);
2574 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2575 nfs4_schedule_stateid_recovery(server, state);
2576 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002577out:
2578 return ret;
2579}
2580
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002581/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002582 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 */
Andy Adamson82be4172012-05-23 05:02:35 -04002584static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002585 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002586 int flags,
2587 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002588 struct nfs4_label *label,
2589 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 struct nfs4_state_owner *sp;
2592 struct nfs4_state *state = NULL;
2593 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002594 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002595 struct dentry *dentry = ctx->dentry;
2596 struct rpc_cred *cred = ctx->cred;
2597 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2598 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002599 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002600 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002601 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002605 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2606 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2608 goto out_err;
2609 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002610 status = nfs4_recover_expired_lease(server);
2611 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002612 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002613 if (d_really_is_positive(dentry))
2614 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002615 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002616 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002617 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002618 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002619 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002620 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002621 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
David Quigley14c43f72013-05-22 12:50:43 -04002623 if (label) {
2624 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2625 if (IS_ERR(olabel)) {
2626 status = PTR_ERR(olabel);
2627 goto err_opendata_put;
2628 }
2629 }
2630
Trond Myklebuste911b812014-03-26 13:24:37 -07002631 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2632 if (!opendata->f_attr.mdsthreshold) {
2633 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2634 if (!opendata->f_attr.mdsthreshold)
2635 goto err_free_label;
2636 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002637 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002638 }
David Howells2b0143b2015-03-17 22:25:59 +00002639 if (d_really_is_positive(dentry))
2640 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002641
Trond Myklebust3efb9722013-05-29 13:17:04 -04002642 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002643 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002644 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002645 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002646
NeilBrownefcbc042015-07-30 13:00:56 +10002647 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002648 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002649 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002650 /*
2651 * send create attributes which was not set by open
2652 * with an extra setattr.
2653 */
2654 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2655 nfs_fattr_init(opendata->o_res.f_attr);
2656 status = nfs4_do_setattr(state->inode, cred,
2657 opendata->o_res.f_attr, sattr,
2658 state, label, olabel);
2659 if (status == 0) {
2660 nfs_setattr_update_inode(state->inode, sattr,
2661 opendata->o_res.f_attr);
2662 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2663 }
David Quigley1775fd32013-05-22 12:50:42 -04002664 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002665 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002666 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002667 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002668
Trond Myklebuste911b812014-03-26 13:24:37 -07002669 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002670 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002671 opendata->f_attr.mdsthreshold = NULL;
2672 }
Andy Adamson82be4172012-05-23 05:02:35 -04002673
David Quigley14c43f72013-05-22 12:50:43 -04002674 nfs4_label_free(olabel);
2675
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002676 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002679err_free_label:
2680 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002681err_opendata_put:
2682 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002683err_put_state_owner:
2684 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return status;
2687}
2688
2689
Andy Adamson82be4172012-05-23 05:02:35 -04002690static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002691 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002692 int flags,
2693 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002694 struct nfs4_label *label,
2695 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002697 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct nfs4_exception exception = { };
2699 struct nfs4_state *res;
2700 int status;
2701
2702 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002703 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002704 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002705 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (status == 0)
2707 break;
2708 /* NOTE: BAD_SEQID means the server and client disagree about the
2709 * book-keeping w.r.t. state-changing operations
2710 * (OPEN/CLOSE/LOCK/LOCKU...)
2711 * It is actually a sign of a bug on the client or on the server.
2712 *
2713 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002714 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * have unhashed the old state_owner for us, and that we can
2716 * therefore safely retry using a new one. We should still warn
2717 * the user though...
2718 */
2719 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002720 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002721 " returned a bad sequence-id error!\n",
2722 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 exception.retry = 1;
2724 continue;
2725 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002726 /*
2727 * BAD_STATEID on OPEN means that the server cancelled our
2728 * state before it received the OPEN_CONFIRM.
2729 * Recover by retrying the request as per the discussion
2730 * on Page 181 of RFC3530.
2731 */
2732 if (status == -NFS4ERR_BAD_STATEID) {
2733 exception.retry = 1;
2734 continue;
2735 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002736 if (status == -EAGAIN) {
2737 /* We must have found a delegation */
2738 exception.retry = 1;
2739 continue;
2740 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002741 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2742 continue;
2743 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 status, &exception));
2745 } while (exception.retry);
2746 return res;
2747}
2748
Trond Myklebust8487c472016-06-26 08:44:35 -04002749static int _nfs4_do_setattr(struct inode *inode,
2750 struct nfs_setattrargs *arg,
2751 struct nfs_setattrres *res,
2752 struct rpc_cred *cred,
2753 struct nfs4_state *state)
2754{
2755 struct nfs_server *server = NFS_SERVER(inode);
2756 struct rpc_message msg = {
2757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2758 .rpc_argp = arg,
2759 .rpc_resp = res,
2760 .rpc_cred = cred,
2761 };
2762 struct rpc_cred *delegation_cred = NULL;
2763 unsigned long timestamp = jiffies;
2764 fmode_t fmode;
2765 bool truncate;
2766 int status;
2767
2768 nfs_fattr_init(res->fattr);
2769
2770 /* Servers should only apply open mode checks for file size changes */
2771 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2772 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2773
2774 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2775 /* Use that stateid */
2776 } else if (truncate && state != NULL) {
2777 struct nfs_lockowner lockowner = {
2778 .l_owner = current->files,
2779 .l_pid = current->tgid,
2780 };
2781 if (!nfs4_valid_open_stateid(state))
2782 return -EBADF;
2783 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2784 &arg->stateid, &delegation_cred) == -EIO)
2785 return -EBADF;
2786 } else
2787 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2788 if (delegation_cred)
2789 msg.rpc_cred = delegation_cred;
2790
2791 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2792
2793 put_rpccred(delegation_cred);
2794 if (status == 0 && state != NULL)
2795 renew_lease(server, timestamp);
2796 trace_nfs4_setattr(inode, &arg->stateid, status);
2797 return status;
2798}
2799
2800static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2801 struct nfs_fattr *fattr, struct iattr *sattr,
2802 struct nfs4_state *state, struct nfs4_label *ilabel,
2803 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002805 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002807 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .iap = sattr,
2809 .server = server,
2810 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002811 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 };
2813 struct nfs_setattrres res = {
2814 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002815 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 .server = server,
2817 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002818 struct nfs4_exception exception = {
2819 .state = state,
2820 .inode = inode,
2821 .stateid = &arg.stateid,
2822 };
2823 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
David Quigleyaa9c2662013-05-22 12:50:44 -04002825 arg.bitmask = nfs4_bitmask(server, ilabel);
2826 if (ilabel)
2827 arg.bitmask = nfs4_bitmask(server, olabel);
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002830 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002831 switch (err) {
2832 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002833 if (!(sattr->ia_valid & ATTR_SIZE)) {
2834 pr_warn_once("NFSv4: server %s is incorrectly "
2835 "applying open mode checks to "
2836 "a SETATTR that is not "
2837 "changing file size.\n",
2838 server->nfs_client->cl_hostname);
2839 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002840 if (state && !(state->state & FMODE_WRITE)) {
2841 err = -EBADF;
2842 if (sattr->ia_valid & ATTR_OPEN)
2843 err = -EACCES;
2844 goto out;
2845 }
2846 }
2847 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002849out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return err;
2851}
2852
Peng Tao500d7012015-09-22 11:35:22 +08002853static bool
2854nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2855{
2856 if (inode == NULL || !nfs_have_layout(inode))
2857 return false;
2858
2859 return pnfs_wait_on_layoutreturn(inode, task);
2860}
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862struct nfs4_closedata {
2863 struct inode *inode;
2864 struct nfs4_state *state;
2865 struct nfs_closeargs arg;
2866 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002867 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002868 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002869 bool roc;
2870 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871};
2872
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002873static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002874{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002875 struct nfs4_closedata *calldata = data;
2876 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002877 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002878
Fred Isamanf7e89172011-01-06 11:36:32 +00002879 if (calldata->roc)
2880 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002881 nfs4_put_open_state(calldata->state);
2882 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002883 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002884 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002885 kfree(calldata);
2886}
2887
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002888static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002890 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002893 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Chuck Levera3ca5652012-03-01 17:00:40 -05002895 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002896 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2897 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002898 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 /* hmm. we are done with the inode, and in the process of freeing
2900 * the state_owner. we keep this around to process errors
2901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 switch (task->tk_status) {
2903 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002904 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002905 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002906 pnfs_roc_set_barrier(state->inode,
2907 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002908 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002909 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002910 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002912 case -NFS4ERR_OLD_STATEID:
2913 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002915 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002916 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002917 rpc_restart_call_prepare(task);
2918 goto out_release;
2919 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002920 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002923 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002924 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002925 goto out_release;
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002928 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2929 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002930out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002931 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002932 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002933 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002936static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002938 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002939 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002940 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002941 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002942 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002943
Chuck Levera3ca5652012-03-01 17:00:40 -05002944 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002945 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002946 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002947
Trond Myklebust88069f72009-12-08 08:33:16 -05002948 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002949 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002950 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2951 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2952 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002953 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002954 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002955 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002956 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002957 if (state->n_rdonly == 0)
2958 call_close |= is_rdonly;
2959 else if (is_rdonly)
2960 calldata->arg.fmode |= FMODE_READ;
2961 if (state->n_wronly == 0)
2962 call_close |= is_wronly;
2963 else if (is_wronly)
2964 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002965 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2966 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002967 } else if (is_rdwr)
2968 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2969
Trond Myklebust5d422302013-03-14 16:57:48 -04002970 if (!nfs4_valid_open_stateid(state))
2971 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002972 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002973
2974 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002975 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002976 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002977 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002978
Peng Tao500d7012015-09-22 11:35:22 +08002979 if (nfs4_wait_on_layoutreturn(inode, task)) {
2980 nfs_release_seqid(calldata->arg.seqid);
2981 goto out_wait;
2982 }
2983
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002984 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002985 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002986 if (calldata->roc)
2987 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2988
Trond Myklebust6ae37332015-01-30 14:21:14 -05002989 calldata->arg.share_access =
2990 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2991 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002992
Trond Myklebust516a6af2005-10-27 22:12:41 -04002993 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002994 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002995 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002996 &calldata->arg.seq_args,
2997 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002998 task) != 0)
2999 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003000 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003001 return;
3002out_no_action:
3003 task->tk_action = NULL;
3004out_wait:
3005 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003008static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003009 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003010 .rpc_call_done = nfs4_close_done,
3011 .rpc_release = nfs4_free_closedata,
3012};
3013
Peng Taofe08c542014-07-03 13:05:01 +08003014static bool nfs4_roc(struct inode *inode)
3015{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003016 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003017 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003018 return pnfs_roc(inode);
3019}
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021/*
3022 * It is possible for data to be read/written from a mem-mapped file
3023 * after the sys_close call (which hits the vfs layer as a flush).
3024 * This means that we can't safely call nfsv4 close on a file until
3025 * the inode is cleared. This in turn means that we are not good
3026 * NFSv4 citizens - we do not indicate to the server to update the file's
3027 * share state even when we are done with one of the three share
3028 * stateid's in the inode.
3029 *
3030 * NOTE: Caller must be holding the sp->so_owner semaphore!
3031 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003032int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003034 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003035 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003037 struct nfs4_state_owner *sp = state->owner;
3038 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003039 struct rpc_message msg = {
3040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3041 .rpc_cred = state->owner->so_cred,
3042 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003043 struct rpc_task_setup task_setup_data = {
3044 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003045 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003046 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003047 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003048 .flags = RPC_TASK_ASYNC,
3049 };
Trond Myklebust95121352005-10-18 14:20:12 -07003050 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003052 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3053 &task_setup_data.rpc_client, &msg);
3054
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003055 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003057 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003058 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003059 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003061 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003063 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3064 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003065 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003066 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003067 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003068 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003069 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003070 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003071 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003072 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003073 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003074
Trond Myklebust1174dd12010-12-21 10:52:24 -05003075 msg.rpc_argp = &calldata->arg;
3076 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003077 task_setup_data.callback_data = calldata;
3078 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003079 if (IS_ERR(task))
3080 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003081 status = 0;
3082 if (wait)
3083 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003084 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003085 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003086out_free_calldata:
3087 kfree(calldata);
3088out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003089 nfs4_put_open_state(state);
3090 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003091 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Trond Myklebust2b484292010-09-17 10:56:51 -04003094static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003095nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3096 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003099 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3100
3101 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Trond Myklebust565277f2007-10-15 18:17:53 -04003103 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003104 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003105
3106 nfs4_label_release_security(label);
3107
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003108 if (IS_ERR(state))
3109 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003110 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Trond Myklebust1185a552009-12-03 15:54:02 -05003113static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003114{
3115 if (ctx->state == NULL)
3116 return;
3117 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003118 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003119 else
Al Viro643168c2011-06-22 18:20:23 -04003120 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Trond Myklebustb944dba2013-11-04 15:20:20 -05003123#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3124#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003125#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3128{
Kinglong Mee8c612822015-08-26 21:12:58 +08003129 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003130 struct nfs4_server_caps_arg args = {
3131 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003132 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct nfs4_server_caps_res res = {};
3135 struct rpc_message msg = {
3136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003137 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 .rpc_resp = &res,
3139 };
3140 int status;
3141
Kinglong Mee8c612822015-08-26 21:12:58 +08003142 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3143 FATTR4_WORD0_FH_EXPIRE_TYPE |
3144 FATTR4_WORD0_LINK_SUPPORT |
3145 FATTR4_WORD0_SYMLINK_SUPPORT |
3146 FATTR4_WORD0_ACLSUPPORT;
3147 if (minorversion)
3148 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3149
Bryan Schumaker7c513052011-03-24 17:12:24 +00003150 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003152 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003153 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003154 case 0:
3155 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3156 res.attr_bitmask[2] = 0;
3157 break;
3158 case 1:
3159 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3160 break;
3161 case 2:
3162 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003165 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3166 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3167 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3168 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003169 NFS_CAP_CTIME|NFS_CAP_MTIME|
3170 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003171 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3172 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 server->caps |= NFS_CAP_ACLS;
3174 if (res.has_links != 0)
3175 server->caps |= NFS_CAP_HARDLINKS;
3176 if (res.has_symlinks != 0)
3177 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003178 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3179 server->caps |= NFS_CAP_FILEID;
3180 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3181 server->caps |= NFS_CAP_MODE;
3182 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3183 server->caps |= NFS_CAP_NLINK;
3184 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3185 server->caps |= NFS_CAP_OWNER;
3186 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3187 server->caps |= NFS_CAP_OWNER_GROUP;
3188 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3189 server->caps |= NFS_CAP_ATIME;
3190 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3191 server->caps |= NFS_CAP_CTIME;
3192 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3193 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3195 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3196 server->caps |= NFS_CAP_SECURITY_LABEL;
3197#endif
3198 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3199 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003200 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003201
Trond Myklebusta65318b2009-03-11 14:10:28 -04003202 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3203 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3204 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003205 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003206 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3207 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003209 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return status;
3213}
3214
Trond Myklebust55a97592006-06-09 09:34:19 -04003215int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
3217 struct nfs4_exception exception = { };
3218 int err;
3219 do {
3220 err = nfs4_handle_exception(server,
3221 _nfs4_server_capabilities(server, fhandle),
3222 &exception);
3223 } while (exception.retry);
3224 return err;
3225}
3226
3227static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3228 struct nfs_fsinfo *info)
3229{
David Quigleyaa9c2662013-05-22 12:50:44 -04003230 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003232 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 };
3234 struct nfs4_lookup_res res = {
3235 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003236 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 .fh = fhandle,
3238 };
3239 struct rpc_message msg = {
3240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3241 .rpc_argp = &args,
3242 .rpc_resp = &res,
3243 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003244
David Quigleyaa9c2662013-05-22 12:50:44 -04003245 bitmask[0] = nfs4_fattr_bitmap[0];
3246 bitmask[1] = nfs4_fattr_bitmap[1];
3247 /*
3248 * Process the label in the upcoming getfattr
3249 */
3250 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3251
Trond Myklebust0e574af2005-10-27 22:12:38 -04003252 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003253 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
3256static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3257 struct nfs_fsinfo *info)
3258{
3259 struct nfs4_exception exception = { };
3260 int err;
3261 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003262 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003263 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003264 switch (err) {
3265 case 0:
3266 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003267 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003268 default:
3269 err = nfs4_handle_exception(server, err, &exception);
3270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003272out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return err;
3274}
3275
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003276static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3277 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3278{
Trond Myklebustc2190662013-08-26 19:23:04 -04003279 struct rpc_auth_create_args auth_args = {
3280 .pseudoflavor = flavor,
3281 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003282 struct rpc_auth *auth;
3283 int ret;
3284
Trond Myklebustc2190662013-08-26 19:23:04 -04003285 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003286 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003287 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003288 goto out;
3289 }
3290 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003291out:
3292 return ret;
3293}
3294
Chuck Lever9a744ba2013-03-16 15:56:02 -04003295/*
3296 * Retry pseudoroot lookup with various security flavors. We do this when:
3297 *
3298 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3299 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3300 *
3301 * Returns zero on success, or a negative NFS4ERR value, or a
3302 * negative errno value.
3303 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003304static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003305 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003307 /* Per 3530bis 15.33.5 */
3308 static const rpc_authflavor_t flav_array[] = {
3309 RPC_AUTH_GSS_KRB5P,
3310 RPC_AUTH_GSS_KRB5I,
3311 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003312 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003313 RPC_AUTH_NULL,
3314 };
3315 int status = -EPERM;
3316 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003318 if (server->auth_info.flavor_len > 0) {
3319 /* try each flavor specified by user */
3320 for (i = 0; i < server->auth_info.flavor_len; i++) {
3321 status = nfs4_lookup_root_sec(server, fhandle, info,
3322 server->auth_info.flavors[i]);
3323 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3324 continue;
3325 break;
3326 }
3327 } else {
3328 /* no flavors specified by user, try default list */
3329 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3330 status = nfs4_lookup_root_sec(server, fhandle, info,
3331 flav_array[i]);
3332 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3333 continue;
3334 break;
3335 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003336 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003337
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003338 /*
3339 * -EACCESS could mean that the user doesn't have correct permissions
3340 * to access the mount. It could also mean that we tried to mount
3341 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3342 * existing mount programs don't handle -EACCES very well so it should
3343 * be mapped to -EPERM instead.
3344 */
3345 if (status == -EACCES)
3346 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003347 return status;
3348}
3349
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003350/**
3351 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3352 * @server: initialized nfs_server handle
3353 * @fhandle: we fill in the pseudo-fs root file handle
3354 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003355 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003356 *
3357 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003358 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003359int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003360 struct nfs_fsinfo *info,
3361 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003362{
Andre Przywarac7757072015-04-23 17:17:40 +01003363 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003364
Andre Przywarac7757072015-04-23 17:17:40 +01003365 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003366 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003367
3368 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003369 status = server->nfs_client->cl_mvops->find_root_sec(server,
3370 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (status == 0)
3373 status = nfs4_server_capabilities(server, fhandle);
3374 if (status == 0)
3375 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003376
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003377 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
Bryan Schumakerbae36242012-05-10 15:07:31 -04003380static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3381 struct nfs_fsinfo *info)
3382{
3383 int error;
3384 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003385 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003386
3387 error = nfs4_server_capabilities(server, mntfh);
3388 if (error < 0) {
3389 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3390 return error;
3391 }
3392
David Quigley14c43f72013-05-22 12:50:43 -04003393 label = nfs4_label_alloc(server, GFP_KERNEL);
3394 if (IS_ERR(label))
3395 return PTR_ERR(label);
3396
David Quigley1775fd32013-05-22 12:50:42 -04003397 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003398 if (error < 0) {
3399 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003400 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003401 }
3402
3403 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3404 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3405 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3406
David Quigley14c43f72013-05-22 12:50:43 -04003407err_free_label:
3408 nfs4_label_free(label);
3409
Bryan Schumakerbae36242012-05-10 15:07:31 -04003410 return error;
3411}
3412
Manoj Naik6b97fd32006-06-09 09:34:29 -04003413/*
3414 * Get locations and (maybe) other attributes of a referral.
3415 * Note that we'll actually follow the referral later when
3416 * we detect fsid mismatch in inode revalidation
3417 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003418static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3419 const struct qstr *name, struct nfs_fattr *fattr,
3420 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003421{
3422 int status = -ENOMEM;
3423 struct page *page = NULL;
3424 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003425
3426 page = alloc_page(GFP_KERNEL);
3427 if (page == NULL)
3428 goto out;
3429 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3430 if (locations == NULL)
3431 goto out;
3432
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003433 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003434 if (status != 0)
3435 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003436
3437 /*
3438 * If the fsid didn't change, this is a migration event, not a
3439 * referral. Cause us to drop into the exception handler, which
3440 * will kick off migration recovery.
3441 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003442 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003443 dprintk("%s: server did not return a different fsid for"
3444 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003445 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003446 goto out;
3447 }
Andy Adamson533eb462011-06-13 18:25:56 -04003448 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3449 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003450
Andy Adamson533eb462011-06-13 18:25:56 -04003451 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003452 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003453 memset(fhandle, 0, sizeof(struct nfs_fh));
3454out:
3455 if (page)
3456 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003457 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003458 return status;
3459}
3460
David Quigley1775fd32013-05-22 12:50:42 -04003461static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3462 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 struct nfs4_getattr_arg args = {
3465 .fh = fhandle,
3466 .bitmask = server->attr_bitmask,
3467 };
3468 struct nfs4_getattr_res res = {
3469 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003470 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 .server = server,
3472 };
3473 struct rpc_message msg = {
3474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3475 .rpc_argp = &args,
3476 .rpc_resp = &res,
3477 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003478
3479 args.bitmask = nfs4_bitmask(server, label);
3480
Trond Myklebust0e574af2005-10-27 22:12:38 -04003481 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003482 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
David Quigley1775fd32013-05-22 12:50:42 -04003485static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3486 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
3488 struct nfs4_exception exception = { };
3489 int err;
3490 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003491 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3492 trace_nfs4_getattr(server, fhandle, fattr, err);
3493 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 &exception);
3495 } while (exception.retry);
3496 return err;
3497}
3498
3499/*
3500 * The file is not closed if it is opened due to the a request to change
3501 * the size of the file. The open call will not be needed once the
3502 * VFS layer lookup-intents are implemented.
3503 *
3504 * Close is called when the inode is destroyed.
3505 * If we haven't opened the file for O_WRONLY, we
3506 * need to in the size_change case to obtain a stateid.
3507 *
3508 * Got race?
3509 * Because OPEN is always done by name in nfsv4, it is
3510 * possible that we opened a different file by the same
3511 * name. We can recognize this race condition, but we
3512 * can't do anything about it besides returning an error.
3513 *
3514 * This will be fixed with VFS changes (lookup-intent).
3515 */
3516static int
3517nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3518 struct iattr *sattr)
3519{
David Howells2b0143b2015-03-17 22:25:59 +00003520 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003521 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003522 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003523 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 int status;
3525
Peng Tao88ac8152014-09-12 11:04:10 +08003526 if (pnfs_ld_layoutret_on_setattr(inode) &&
3527 sattr->ia_valid & ATTR_SIZE &&
3528 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003529 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003530
Trond Myklebust0e574af2005-10-27 22:12:38 -04003531 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Andy Adamson26699402012-05-30 16:12:24 -04003533 /* Deal with open(O_TRUNC) */
3534 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003535 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003536
3537 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003538 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003539 return 0;
3540
Trond Myklebustd5308382005-11-04 15:33:38 -05003541 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003542 if (sattr->ia_valid & ATTR_FILE) {
3543 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003544
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003545 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003546 if (ctx) {
3547 cred = ctx->cred;
3548 state = ctx->state;
3549 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003550 }
3551
David Quigley14c43f72013-05-22 12:50:43 -04003552 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3553 if (IS_ERR(label))
3554 return PTR_ERR(label);
3555
3556 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003557 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003558 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003559 nfs_setsecurity(inode, fattr, label);
3560 }
David Quigley14c43f72013-05-22 12:50:43 -04003561 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return status;
3563}
3564
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003565static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3566 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003567 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003568{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003569 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003570 int status;
3571 struct nfs4_lookup_arg args = {
3572 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003573 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003574 .name = name,
3575 };
3576 struct nfs4_lookup_res res = {
3577 .server = server,
3578 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003579 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003580 .fh = fhandle,
3581 };
3582 struct rpc_message msg = {
3583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3584 .rpc_argp = &args,
3585 .rpc_resp = &res,
3586 };
3587
David Quigleyaa9c2662013-05-22 12:50:44 -04003588 args.bitmask = nfs4_bitmask(server, label);
3589
David Howells2b3de442006-08-22 20:06:09 -04003590 nfs_fattr_init(fattr);
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003593 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 dprintk("NFS reply lookup: %d\n", status);
3595 return status;
3596}
3597
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003598static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003599{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003600 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003601 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003602 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3603 fattr->nlink = 2;
3604}
3605
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003606static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003607 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003608 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003609{
3610 struct nfs4_exception exception = { };
3611 struct rpc_clnt *client = *clnt;
3612 int err;
3613 do {
David Quigley1775fd32013-05-22 12:50:42 -04003614 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003615 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003616 switch (err) {
3617 case -NFS4ERR_BADNAME:
3618 err = -ENOENT;
3619 goto out;
3620 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003621 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003622 if (err == -NFS4ERR_MOVED)
3623 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003624 goto out;
3625 case -NFS4ERR_WRONGSEC:
3626 err = -EPERM;
3627 if (client != *clnt)
3628 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003629 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003630 if (IS_ERR(client))
3631 return PTR_ERR(client);
3632
3633 exception.retry = 1;
3634 break;
3635 default:
3636 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3637 }
3638 } while (exception.retry);
3639
3640out:
3641 if (err == 0)
3642 *clnt = client;
3643 else if (client != *clnt)
3644 rpc_shutdown_client(client);
3645
3646 return err;
3647}
3648
Al Virobeffb8f2016-07-20 16:34:42 -04003649static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003650 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3651 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003653 int status;
3654 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003655
David Quigley1775fd32013-05-22 12:50:42 -04003656 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003657 if (client != NFS_CLIENT(dir)) {
3658 rpc_shutdown_client(client);
3659 nfs_fixup_secinfo_attributes(fattr);
3660 }
3661 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003664struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003665nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003666 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3667{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003668 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003669 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003670
David Quigley1775fd32013-05-22 12:50:42 -04003671 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003672 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003673 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003674 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003675}
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3678{
Trond Myklebust76b32992007-08-10 17:45:11 -04003679 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 struct nfs4_accessargs args = {
3681 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003682 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003684 struct nfs4_accessres res = {
3685 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003686 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 struct rpc_message msg = {
3688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3689 .rpc_argp = &args,
3690 .rpc_resp = &res,
3691 .rpc_cred = entry->cred,
3692 };
3693 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003694 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
3696 /*
3697 * Determine which access bits we want to ask for...
3698 */
3699 if (mode & MAY_READ)
3700 args.access |= NFS4_ACCESS_READ;
3701 if (S_ISDIR(inode->i_mode)) {
3702 if (mode & MAY_WRITE)
3703 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3704 if (mode & MAY_EXEC)
3705 args.access |= NFS4_ACCESS_LOOKUP;
3706 } else {
3707 if (mode & MAY_WRITE)
3708 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3709 if (mode & MAY_EXEC)
3710 args.access |= NFS4_ACCESS_EXECUTE;
3711 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003712
3713 res.fattr = nfs_alloc_fattr();
3714 if (res.fattr == NULL)
3715 return -ENOMEM;
3716
Bryan Schumaker7c513052011-03-24 17:12:24 +00003717 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003719 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003720 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003722 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return status;
3724}
3725
3726static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3727{
3728 struct nfs4_exception exception = { };
3729 int err;
3730 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003731 err = _nfs4_proc_access(inode, entry);
3732 trace_nfs4_access(inode, err);
3733 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 &exception);
3735 } while (exception.retry);
3736 return err;
3737}
3738
3739/*
3740 * TODO: For the time being, we don't try to get any attributes
3741 * along with any of the zero-copy operations READ, READDIR,
3742 * READLINK, WRITE.
3743 *
3744 * In the case of the first three, we want to put the GETATTR
3745 * after the read-type operation -- this is because it is hard
3746 * to predict the length of a GETATTR response in v4, and thus
3747 * align the READ data correctly. This means that the GETATTR
3748 * may end up partially falling into the page cache, and we should
3749 * shift it into the 'tail' of the xdr_buf before processing.
3750 * To do this efficiently, we need to know the total length
3751 * of data received, which doesn't seem to be available outside
3752 * of the RPC layer.
3753 *
3754 * In the case of WRITE, we also want to put the GETATTR after
3755 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003756 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 *
3758 * Both of these changes to the XDR layer would in fact be quite
3759 * minor, but I decided to leave them for a subsequent patch.
3760 */
3761static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3762 unsigned int pgbase, unsigned int pglen)
3763{
3764 struct nfs4_readlink args = {
3765 .fh = NFS_FH(inode),
3766 .pgbase = pgbase,
3767 .pglen = pglen,
3768 .pages = &page,
3769 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003770 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 struct rpc_message msg = {
3772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3773 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003774 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 };
3776
Bryan Schumaker7c513052011-03-24 17:12:24 +00003777 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 -07003778}
3779
3780static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3781 unsigned int pgbase, unsigned int pglen)
3782{
3783 struct nfs4_exception exception = { };
3784 int err;
3785 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003786 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3787 trace_nfs4_readlink(inode, err);
3788 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 &exception);
3790 } while (exception.retry);
3791 return err;
3792}
3793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003795 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797static int
3798nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003799 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
David Quigleyaa9c2662013-05-22 12:50:44 -04003801 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003802 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 int status = 0;
3805
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003806 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3807 if (IS_ERR(ctx))
3808 return PTR_ERR(ctx);
3809
David Quigleyaa9c2662013-05-22 12:50:44 -04003810 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3811
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003812 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003813 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (IS_ERR(state)) {
3815 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003816 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003819 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003820 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return status;
3822}
3823
Al Virobeffb8f2016-07-20 16:34:42 -04003824static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Trond Myklebust16e42952005-10-27 22:12:44 -04003826 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003827 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003829 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003831 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003832 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3836 .rpc_argp = &args,
3837 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003839 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003840
Bryan Schumaker7c513052011-03-24 17:12:24 +00003841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003842 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003843 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 return status;
3845}
3846
Al Virobeffb8f2016-07-20 16:34:42 -04003847static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
3849 struct nfs4_exception exception = { };
3850 int err;
3851 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003852 err = _nfs4_proc_remove(dir, name);
3853 trace_nfs4_remove(dir, name, err);
3854 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 &exception);
3856 } while (exception.retry);
3857 return err;
3858}
3859
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003860static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003862 struct nfs_server *server = NFS_SERVER(dir);
3863 struct nfs_removeargs *args = msg->rpc_argp;
3864 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003866 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003868 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003869
3870 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871}
3872
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003873static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3874{
Al Viro884be172016-04-28 23:56:31 -04003875 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003876 &data->args.seq_args,
3877 &data->res.seq_res,
3878 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003881static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003883 struct nfs_unlinkdata *data = task->tk_calldata;
3884 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003885
Trond Myklebust14516c32010-07-31 14:29:06 -04003886 if (!nfs4_sequence_done(task, &res->seq_res))
3887 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003888 if (nfs4_async_handle_error(task, res->server, NULL,
3889 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003890 return 0;
3891 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003892 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894
Jeff Laytond3d41522010-09-17 17:31:57 -04003895static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3896{
3897 struct nfs_server *server = NFS_SERVER(dir);
3898 struct nfs_renameargs *arg = msg->rpc_argp;
3899 struct nfs_renameres *res = msg->rpc_resp;
3900
3901 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003902 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003903 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003904}
3905
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003906static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3907{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003908 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3909 &data->args.seq_args,
3910 &data->res.seq_res,
3911 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003912}
3913
3914static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3915 struct inode *new_dir)
3916{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003917 struct nfs_renamedata *data = task->tk_calldata;
3918 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003919
3920 if (!nfs4_sequence_done(task, &res->seq_res))
3921 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003922 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003923 return 0;
3924
3925 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003926 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003927 return 1;
3928}
3929
Al Virobeffb8f2016-07-20 16:34:42 -04003930static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003932 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct nfs4_link_arg arg = {
3934 .fh = NFS_FH(inode),
3935 .dir_fh = NFS_FH(dir),
3936 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003937 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003939 struct nfs4_link_res res = {
3940 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003941 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003942 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 struct rpc_message msg = {
3944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3945 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003946 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003948 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Trond Myklebust136f2622010-04-16 16:22:49 -04003950 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003951 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003952 goto out;
3953
David Quigley14c43f72013-05-22 12:50:43 -04003954 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3955 if (IS_ERR(res.label)) {
3956 status = PTR_ERR(res.label);
3957 goto out;
3958 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003959 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003960
Bryan Schumaker7c513052011-03-24 17:12:24 +00003961 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003962 if (!status) {
3963 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003964 status = nfs_post_op_update_inode(inode, res.fattr);
3965 if (!status)
3966 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003967 }
David Quigley14c43f72013-05-22 12:50:43 -04003968
3969
3970 nfs4_label_free(res.label);
3971
Trond Myklebust136f2622010-04-16 16:22:49 -04003972out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003973 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 return status;
3975}
3976
Al Virobeffb8f2016-07-20 16:34:42 -04003977static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
3979 struct nfs4_exception exception = { };
3980 int err;
3981 do {
3982 err = nfs4_handle_exception(NFS_SERVER(inode),
3983 _nfs4_proc_link(inode, dir, name),
3984 &exception);
3985 } while (exception.retry);
3986 return err;
3987}
3988
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003989struct nfs4_createdata {
3990 struct rpc_message msg;
3991 struct nfs4_create_arg arg;
3992 struct nfs4_create_res res;
3993 struct nfs_fh fh;
3994 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003995 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003996};
3997
3998static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003999 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004000{
4001 struct nfs4_createdata *data;
4002
4003 data = kzalloc(sizeof(*data), GFP_KERNEL);
4004 if (data != NULL) {
4005 struct nfs_server *server = NFS_SERVER(dir);
4006
David Quigley14c43f72013-05-22 12:50:43 -04004007 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4008 if (IS_ERR(data->label))
4009 goto out_free;
4010
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004011 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4012 data->msg.rpc_argp = &data->arg;
4013 data->msg.rpc_resp = &data->res;
4014 data->arg.dir_fh = NFS_FH(dir);
4015 data->arg.server = server;
4016 data->arg.name = name;
4017 data->arg.attrs = sattr;
4018 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004019 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004020 data->res.server = server;
4021 data->res.fh = &data->fh;
4022 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004023 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004024 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004025 }
4026 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004027out_free:
4028 kfree(data);
4029 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004030}
4031
4032static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4033{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004034 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004035 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004036 if (status == 0) {
4037 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004038 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004039 }
4040 return status;
4041}
4042
4043static void nfs4_free_createdata(struct nfs4_createdata *data)
4044{
David Quigley14c43f72013-05-22 12:50:43 -04004045 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004046 kfree(data);
4047}
4048
Chuck Lever4f390c12006-08-22 20:06:22 -04004049static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004050 struct page *page, unsigned int len, struct iattr *sattr,
4051 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004053 struct nfs4_createdata *data;
4054 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Chuck Lever94a6d752006-08-22 20:06:23 -04004056 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004057 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004058
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004059 status = -ENOMEM;
4060 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4061 if (data == NULL)
4062 goto out;
4063
4064 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4065 data->arg.u.symlink.pages = &page;
4066 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004067 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004069 status = nfs4_do_create(dir, dentry, data);
4070
4071 nfs4_free_createdata(data);
4072out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return status;
4074}
4075
Chuck Lever4f390c12006-08-22 20:06:22 -04004076static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004077 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004080 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004082
4083 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004086 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4087 trace_nfs4_symlink(dir, &dentry->d_name, err);
4088 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 &exception);
4090 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004091
4092 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return err;
4094}
4095
4096static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004097 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004099 struct nfs4_createdata *data;
4100 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004102 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4103 if (data == NULL)
4104 goto out;
4105
David Quigley1775fd32013-05-22 12:50:42 -04004106 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004107 status = nfs4_do_create(dir, dentry, data);
4108
4109 nfs4_free_createdata(data);
4110out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 return status;
4112}
4113
4114static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4115 struct iattr *sattr)
4116{
4117 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004118 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004120
David Quigleyaa9c2662013-05-22 12:50:44 -04004121 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4122
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004123 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004125 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4126 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4127 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 &exception);
4129 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004130 nfs4_label_release_security(label);
4131
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return err;
4133}
4134
4135static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004136 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
David Howells2b0143b2015-03-17 22:25:59 +00004138 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct nfs4_readdir_arg args = {
4140 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004141 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 .pgbase = 0,
4143 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004144 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004145 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 };
4147 struct nfs4_readdir_res res;
4148 struct rpc_message msg = {
4149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4150 .rpc_argp = &args,
4151 .rpc_resp = &res,
4152 .rpc_cred = cred,
4153 };
4154 int status;
4155
Al Viro6de14722013-09-16 10:53:17 -04004156 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4157 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004158 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004159 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004161 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 -05004162 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004163 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004164 status += args.pgbase;
4165 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004166
4167 nfs_invalidate_atime(dir);
4168
Harvey Harrison3110ff82008-05-02 13:42:44 -07004169 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return status;
4171}
4172
4173static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004174 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 struct nfs4_exception exception = { };
4177 int err;
4178 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004179 err = _nfs4_proc_readdir(dentry, cred, cookie,
4180 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004181 trace_nfs4_readdir(d_inode(dentry), err);
4182 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 &exception);
4184 } while (exception.retry);
4185 return err;
4186}
4187
4188static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004189 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191 struct nfs4_createdata *data;
4192 int mode = sattr->ia_mode;
4193 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004195 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4196 if (data == NULL)
4197 goto out;
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004200 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004202 data->arg.ftype = NF4BLK;
4203 data->arg.u.device.specdata1 = MAJOR(rdev);
4204 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
4206 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 data->arg.ftype = NF4CHR;
4208 data->arg.u.device.specdata1 = MAJOR(rdev);
4209 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004210 } else if (!S_ISSOCK(mode)) {
4211 status = -EINVAL;
4212 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
David Quigley1775fd32013-05-22 12:50:42 -04004214
David Quigleyaa9c2662013-05-22 12:50:44 -04004215 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004217out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004218 nfs4_free_createdata(data);
4219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return status;
4221}
4222
4223static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4224 struct iattr *sattr, dev_t rdev)
4225{
4226 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004227 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004229
David Quigleyaa9c2662013-05-22 12:50:44 -04004230 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4231
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004232 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004234 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4235 trace_nfs4_mknod(dir, &dentry->d_name, err);
4236 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 &exception);
4238 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004239
4240 nfs4_label_release_security(label);
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return err;
4243}
4244
4245static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4246 struct nfs_fsstat *fsstat)
4247{
4248 struct nfs4_statfs_arg args = {
4249 .fh = fhandle,
4250 .bitmask = server->attr_bitmask,
4251 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004252 struct nfs4_statfs_res res = {
4253 .fsstat = fsstat,
4254 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 struct rpc_message msg = {
4256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4257 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004258 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 };
4260
Trond Myklebust0e574af2005-10-27 22:12:38 -04004261 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004262 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
4265static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4266{
4267 struct nfs4_exception exception = { };
4268 int err;
4269 do {
4270 err = nfs4_handle_exception(server,
4271 _nfs4_proc_statfs(server, fhandle, fsstat),
4272 &exception);
4273 } while (exception.retry);
4274 return err;
4275}
4276
4277static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4278 struct nfs_fsinfo *fsinfo)
4279{
4280 struct nfs4_fsinfo_arg args = {
4281 .fh = fhandle,
4282 .bitmask = server->attr_bitmask,
4283 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004284 struct nfs4_fsinfo_res res = {
4285 .fsinfo = fsinfo,
4286 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 struct rpc_message msg = {
4288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4289 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004290 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 };
4292
Bryan Schumaker7c513052011-03-24 17:12:24 +00004293 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
4296static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4297{
4298 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004299 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 int err;
4301
4302 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004303 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004304 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004305 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004306 nfs4_set_lease_period(server->nfs_client,
4307 fsinfo->lease_time * HZ,
4308 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004309 break;
4310 }
4311 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 } while (exception.retry);
4313 return err;
4314}
4315
4316static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4317{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004318 int error;
4319
Trond Myklebust0e574af2005-10-27 22:12:38 -04004320 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004321 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004322 if (error == 0) {
4323 /* block layout checks this! */
4324 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004325 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004326 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004327
4328 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
4330
4331static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4332 struct nfs_pathconf *pathconf)
4333{
4334 struct nfs4_pathconf_arg args = {
4335 .fh = fhandle,
4336 .bitmask = server->attr_bitmask,
4337 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004338 struct nfs4_pathconf_res res = {
4339 .pathconf = pathconf,
4340 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 struct rpc_message msg = {
4342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4343 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004344 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 };
4346
4347 /* None of the pathconf attributes are mandatory to implement */
4348 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4349 memset(pathconf, 0, sizeof(*pathconf));
4350 return 0;
4351 }
4352
Trond Myklebust0e574af2005-10-27 22:12:38 -04004353 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004354 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
4357static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4358 struct nfs_pathconf *pathconf)
4359{
4360 struct nfs4_exception exception = { };
4361 int err;
4362
4363 do {
4364 err = nfs4_handle_exception(server,
4365 _nfs4_proc_pathconf(server, fhandle, pathconf),
4366 &exception);
4367 } while (exception.retry);
4368 return err;
4369}
4370
Trond Myklebust5521abf2013-03-16 20:54:34 -04004371int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004372 const struct nfs_open_context *ctx,
4373 const struct nfs_lock_context *l_ctx,
4374 fmode_t fmode)
4375{
4376 const struct nfs_lockowner *lockowner = NULL;
4377
4378 if (l_ctx != NULL)
4379 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004380 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004381}
4382EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4383
Trond Myklebust5521abf2013-03-16 20:54:34 -04004384static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4385 const struct nfs_open_context *ctx,
4386 const struct nfs_lock_context *l_ctx,
4387 fmode_t fmode)
4388{
4389 nfs4_stateid current_stateid;
4390
Trond Myklebuste1253be2014-03-05 08:44:23 -05004391 /* If the current stateid represents a lost lock, then exit */
4392 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4393 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004394 return nfs4_stateid_match(stateid, &current_stateid);
4395}
4396
4397static bool nfs4_error_stateid_expired(int err)
4398{
4399 switch (err) {
4400 case -NFS4ERR_DELEG_REVOKED:
4401 case -NFS4ERR_ADMIN_REVOKED:
4402 case -NFS4ERR_BAD_STATEID:
4403 case -NFS4ERR_STALE_STATEID:
4404 case -NFS4ERR_OLD_STATEID:
4405 case -NFS4ERR_OPENMODE:
4406 case -NFS4ERR_EXPIRED:
4407 return true;
4408 }
4409 return false;
4410}
4411
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004412void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004413{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004414 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004415}
4416
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004417static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004419 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004421 trace_nfs4_read(hdr, task->tk_status);
4422 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004423 hdr->args.context->state,
4424 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004425 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004426 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004428
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004429 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004431 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
Trond Myklebust5521abf2013-03-16 20:54:34 -04004435static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004436 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004437{
4438
4439 if (!nfs4_error_stateid_expired(task->tk_status) ||
4440 nfs4_stateid_is_current(&args->stateid,
4441 args->context,
4442 args->lock_context,
4443 FMODE_READ))
4444 return false;
4445 rpc_restart_call_prepare(task);
4446 return true;
4447}
4448
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004449static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004450{
4451
4452 dprintk("--> %s\n", __func__);
4453
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004454 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004455 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004456 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004457 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004458 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4459 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004460}
4461
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004462static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4463 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004466 if (!hdr->pgio_done_cb)
4467 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004468 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004469 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470}
4471
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004472static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4473 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004474{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004475 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4476 &hdr->args.seq_args,
4477 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004478 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004479 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004480 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4481 hdr->args.lock_context,
4482 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004483 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004484 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004485 return -EIO;
4486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004489static int nfs4_write_done_cb(struct rpc_task *task,
4490 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004492 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004493
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004494 trace_nfs4_write(hdr, task->tk_status);
4495 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004496 hdr->args.context->state,
4497 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004498 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004499 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004501 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004502 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004503 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004504 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
Trond Myklebust5521abf2013-03-16 20:54:34 -04004508static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004509 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004510{
4511
4512 if (!nfs4_error_stateid_expired(task->tk_status) ||
4513 nfs4_stateid_is_current(&args->stateid,
4514 args->context,
4515 args->lock_context,
4516 FMODE_WRITE))
4517 return false;
4518 rpc_restart_call_prepare(task);
4519 return true;
4520}
4521
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004522static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004523{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004524 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004525 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004526 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004527 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004528 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4529 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004530}
4531
Trond Myklebust5a37f852012-04-28 14:55:16 -04004532static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004533bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004534{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004535 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004536 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004537 return false;
4538 /* Otherwise, request attributes if and only if we don't hold
4539 * a delegation
4540 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004541 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004542}
Fred Isamana69aef12011-03-03 15:13:47 +00004543
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4545 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004547 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004548
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004549 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4550 hdr->args.bitmask = NULL;
4551 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004552 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004553 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004554
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004555 if (!hdr->pgio_done_cb)
4556 hdr->pgio_done_cb = nfs4_write_done_cb;
4557 hdr->res.server = server;
4558 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004560 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004561 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Fred Isaman0b7c0152012-04-20 14:47:39 -04004564static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4565{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004566 nfs4_setup_sequence(NFS_SERVER(data->inode),
4567 &data->args.seq_args,
4568 &data->res.seq_res,
4569 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
4571
Fred Isaman0b7c0152012-04-20 14:47:39 -04004572static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004575
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004576 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004577 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4578 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004579 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004580 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583}
4584
Fred Isaman0b7c0152012-04-20 14:47:39 -04004585static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004586{
4587 if (!nfs4_sequence_done(task, &data->res.seq_res))
4588 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004589 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004590}
4591
Fred Isaman0b7c0152012-04-20 14:47:39 -04004592static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004594 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004595
Fred Isaman0b7c0152012-04-20 14:47:39 -04004596 if (data->commit_done_cb == NULL)
4597 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004598 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004600 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004603struct nfs4_renewdata {
4604 struct nfs_client *client;
4605 unsigned long timestamp;
4606};
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608/*
4609 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4610 * standalone procedure for queueing an asynchronous RENEW.
4611 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004612static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004613{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004614 struct nfs4_renewdata *data = calldata;
4615 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004616
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004617 if (atomic_read(&clp->cl_count) > 1)
4618 nfs4_schedule_state_renewal(clp);
4619 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004620 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004621}
4622
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004623static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004625 struct nfs4_renewdata *data = calldata;
4626 struct nfs_client *clp = data->client;
4627 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004629 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004630 switch (task->tk_status) {
4631 case 0:
4632 break;
4633 case -NFS4ERR_LEASE_MOVED:
4634 nfs4_schedule_lease_moved_recovery(clp);
4635 break;
4636 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004637 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004638 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4639 return;
4640 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004641 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004642 return;
4643 }
4644 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004646 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
4648
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004649static const struct rpc_call_ops nfs4_renew_ops = {
4650 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004651 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004652};
4653
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004654static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
4656 struct rpc_message msg = {
4657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4658 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004659 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004661 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004663 if (renew_flags == 0)
4664 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004665 if (!atomic_inc_not_zero(&clp->cl_count))
4666 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004667 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004668 if (data == NULL)
4669 return -ENOMEM;
4670 data->client = clp;
4671 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004672 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004673 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
4675
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004676static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
4678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4680 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004681 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 };
4683 unsigned long now = jiffies;
4684 int status;
4685
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004686 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 if (status < 0)
4688 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004689 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 return 0;
4691}
4692
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004693static inline int nfs4_server_supports_acls(struct nfs_server *server)
4694{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004695 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004696}
4697
Trond Myklebust21f498c2012-08-24 10:59:25 -04004698/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4699 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004700 * the stack.
4701 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004702#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004703
Neil Hormane9e3d722011-03-04 19:26:03 -05004704static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004705 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004706{
4707 struct page *newpage, **spages;
4708 int rc = 0;
4709 size_t len;
4710 spages = pages;
4711
4712 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004713 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004714 newpage = alloc_page(GFP_KERNEL);
4715
4716 if (newpage == NULL)
4717 goto unwind;
4718 memcpy(page_address(newpage), buf, len);
4719 buf += len;
4720 buflen -= len;
4721 *pages++ = newpage;
4722 rc++;
4723 } while (buflen != 0);
4724
4725 return rc;
4726
4727unwind:
4728 for(; rc > 0; rc--)
4729 __free_page(spages[rc-1]);
4730 return -ENOMEM;
4731}
4732
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004733struct nfs4_cached_acl {
4734 int cached;
4735 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004736 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004737};
4738
4739static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004740{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004741 struct nfs_inode *nfsi = NFS_I(inode);
4742
4743 spin_lock(&inode->i_lock);
4744 kfree(nfsi->nfs4_acl);
4745 nfsi->nfs4_acl = acl;
4746 spin_unlock(&inode->i_lock);
4747}
4748
4749static void nfs4_zap_acl_attr(struct inode *inode)
4750{
4751 nfs4_set_cached_acl(inode, NULL);
4752}
4753
4754static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4755{
4756 struct nfs_inode *nfsi = NFS_I(inode);
4757 struct nfs4_cached_acl *acl;
4758 int ret = -ENOENT;
4759
4760 spin_lock(&inode->i_lock);
4761 acl = nfsi->nfs4_acl;
4762 if (acl == NULL)
4763 goto out;
4764 if (buf == NULL) /* user is just asking for length */
4765 goto out_len;
4766 if (acl->cached == 0)
4767 goto out;
4768 ret = -ERANGE; /* see getxattr(2) man page */
4769 if (acl->len > buflen)
4770 goto out;
4771 memcpy(buf, acl->data, acl->len);
4772out_len:
4773 ret = acl->len;
4774out:
4775 spin_unlock(&inode->i_lock);
4776 return ret;
4777}
4778
Sachin Prabhu5794d212012-04-17 14:36:40 +01004779static 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 +00004780{
4781 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004782 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004783
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004784 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004785 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004786 if (acl == NULL)
4787 goto out;
4788 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004789 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004790 } else {
4791 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4792 if (acl == NULL)
4793 goto out;
4794 acl->cached = 0;
4795 }
4796 acl->len = acl_len;
4797out:
4798 nfs4_set_cached_acl(inode, acl);
4799}
4800
Andy Adamsonbf118a32011-12-07 11:55:27 -05004801/*
4802 * The getxattr API returns the required buffer length when called with a
4803 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4804 * the required buf. On a NULL buf, we send a page of data to the server
4805 * guessing that the ACL request can be serviced by a page. If so, we cache
4806 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4807 * the cache. If not so, we throw away the page, and cache the required
4808 * length. The next getxattr call will then produce another round trip to
4809 * the server, this time with the input buf of the required size.
4810 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004811static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004812{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004813 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004814 struct nfs_getaclargs args = {
4815 .fh = NFS_FH(inode),
4816 .acl_pages = pages,
4817 .acl_len = buflen,
4818 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004819 struct nfs_getaclres res = {
4820 .acl_len = buflen,
4821 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4824 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004825 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004826 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004827 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4828 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004829
Andy Adamsonbf118a32011-12-07 11:55:27 -05004830 /* As long as we're doing a round trip to the server anyway,
4831 * let's be prepared for a page of acl data. */
4832 if (npages == 0)
4833 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004834 if (npages > ARRAY_SIZE(pages))
4835 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004836
Andy Adamsonbf118a32011-12-07 11:55:27 -05004837 for (i = 0; i < npages; i++) {
4838 pages[i] = alloc_page(GFP_KERNEL);
4839 if (!pages[i])
4840 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004841 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004842
4843 /* for decoding across pages */
4844 res.acl_scratch = alloc_page(GFP_KERNEL);
4845 if (!res.acl_scratch)
4846 goto out_free;
4847
Andy Adamsonbf118a32011-12-07 11:55:27 -05004848 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004849
Peng Taode040be2012-01-10 22:42:47 +08004850 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004851 __func__, buf, buflen, npages, args.acl_len);
4852 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4853 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004854 if (ret)
4855 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004856
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004857 /* Handle the case where the passed-in buffer is too short */
4858 if (res.acl_flags & NFS4_ACL_TRUNC) {
4859 /* Did the user only issue a request for the acl length? */
4860 if (buf == NULL)
4861 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004862 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004863 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004864 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004865 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004866 if (buf) {
4867 if (res.acl_len > buflen) {
4868 ret = -ERANGE;
4869 goto out_free;
4870 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004871 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004872 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004873out_ok:
4874 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004875out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004876 for (i = 0; i < npages; i++)
4877 if (pages[i])
4878 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004879 if (res.acl_scratch)
4880 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004881 return ret;
4882}
4883
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004884static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4885{
4886 struct nfs4_exception exception = { };
4887 ssize_t ret;
4888 do {
4889 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004890 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004891 if (ret >= 0)
4892 break;
4893 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4894 } while (exception.retry);
4895 return ret;
4896}
4897
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004898static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4899{
4900 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004901 int ret;
4902
4903 if (!nfs4_server_supports_acls(server))
4904 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004905 ret = nfs_revalidate_inode(server, inode);
4906 if (ret < 0)
4907 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004908 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4909 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004910 ret = nfs4_read_cached_acl(inode, buf, buflen);
4911 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004912 /* -ENOENT is returned if there is no ACL or if there is an ACL
4913 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004914 return ret;
4915 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004916}
4917
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004918static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004919{
4920 struct nfs_server *server = NFS_SERVER(inode);
4921 struct page *pages[NFS4ACL_MAXPAGES];
4922 struct nfs_setaclargs arg = {
4923 .fh = NFS_FH(inode),
4924 .acl_pages = pages,
4925 .acl_len = buflen,
4926 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004927 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004928 struct rpc_message msg = {
4929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4930 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004931 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004932 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004933 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004934 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004935
4936 if (!nfs4_server_supports_acls(server))
4937 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004938 if (npages > ARRAY_SIZE(pages))
4939 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004940 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004941 if (i < 0)
4942 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004943 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004944 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004945
4946 /*
4947 * Free each page after tx, so the only ref left is
4948 * held by the network stack
4949 */
4950 for (; i > 0; i--)
4951 put_page(pages[i-1]);
4952
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004953 /*
4954 * Acl update can result in inode attribute update.
4955 * so mark the attribute cache invalid.
4956 */
4957 spin_lock(&inode->i_lock);
4958 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4959 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004960 nfs_access_zap_cache(inode);
4961 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004962 return ret;
4963}
4964
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004965static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4966{
4967 struct nfs4_exception exception = { };
4968 int err;
4969 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004970 err = __nfs4_proc_set_acl(inode, buf, buflen);
4971 trace_nfs4_set_acl(inode, err);
4972 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004973 &exception);
4974 } while (exception.retry);
4975 return err;
4976}
4977
David Quigleyaa9c2662013-05-22 12:50:44 -04004978#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4979static int _nfs4_get_security_label(struct inode *inode, void *buf,
4980 size_t buflen)
4981{
4982 struct nfs_server *server = NFS_SERVER(inode);
4983 struct nfs_fattr fattr;
4984 struct nfs4_label label = {0, 0, buflen, buf};
4985
4986 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004987 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004988 .fh = NFS_FH(inode),
4989 .bitmask = bitmask,
4990 };
4991 struct nfs4_getattr_res res = {
4992 .fattr = &fattr,
4993 .label = &label,
4994 .server = server,
4995 };
4996 struct rpc_message msg = {
4997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004998 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004999 .rpc_resp = &res,
5000 };
5001 int ret;
5002
5003 nfs_fattr_init(&fattr);
5004
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005005 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005006 if (ret)
5007 return ret;
5008 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5009 return -ENOENT;
5010 if (buflen < label.len)
5011 return -ERANGE;
5012 return 0;
5013}
5014
5015static int nfs4_get_security_label(struct inode *inode, void *buf,
5016 size_t buflen)
5017{
5018 struct nfs4_exception exception = { };
5019 int err;
5020
5021 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5022 return -EOPNOTSUPP;
5023
5024 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005025 err = _nfs4_get_security_label(inode, buf, buflen);
5026 trace_nfs4_get_security_label(inode, err);
5027 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005028 &exception);
5029 } while (exception.retry);
5030 return err;
5031}
5032
5033static int _nfs4_do_set_security_label(struct inode *inode,
5034 struct nfs4_label *ilabel,
5035 struct nfs_fattr *fattr,
5036 struct nfs4_label *olabel)
5037{
5038
5039 struct iattr sattr = {0};
5040 struct nfs_server *server = NFS_SERVER(inode);
5041 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005042 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005043 .fh = NFS_FH(inode),
5044 .iap = &sattr,
5045 .server = server,
5046 .bitmask = bitmask,
5047 .label = ilabel,
5048 };
5049 struct nfs_setattrres res = {
5050 .fattr = fattr,
5051 .label = olabel,
5052 .server = server,
5053 };
5054 struct rpc_message msg = {
5055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005056 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005057 .rpc_resp = &res,
5058 };
5059 int status;
5060
Jeff Layton12207f62013-11-01 10:49:32 -04005061 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005062
Jeff Layton12207f62013-11-01 10:49:32 -04005063 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005064 if (status)
5065 dprintk("%s failed: %d\n", __func__, status);
5066
5067 return status;
5068}
5069
5070static int nfs4_do_set_security_label(struct inode *inode,
5071 struct nfs4_label *ilabel,
5072 struct nfs_fattr *fattr,
5073 struct nfs4_label *olabel)
5074{
5075 struct nfs4_exception exception = { };
5076 int err;
5077
5078 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005079 err = _nfs4_do_set_security_label(inode, ilabel,
5080 fattr, olabel);
5081 trace_nfs4_set_security_label(inode, err);
5082 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005083 &exception);
5084 } while (exception.retry);
5085 return err;
5086}
5087
5088static int
Al Viro59301222016-05-27 10:19:30 -04005089nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005090{
5091 struct nfs4_label ilabel, *olabel = NULL;
5092 struct nfs_fattr fattr;
5093 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005094 int status;
5095
5096 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5097 return -EOPNOTSUPP;
5098
5099 nfs_fattr_init(&fattr);
5100
5101 ilabel.pi = 0;
5102 ilabel.lfs = 0;
5103 ilabel.label = (char *)buf;
5104 ilabel.len = buflen;
5105
5106 cred = rpc_lookup_cred();
5107 if (IS_ERR(cred))
5108 return PTR_ERR(cred);
5109
5110 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5111 if (IS_ERR(olabel)) {
5112 status = -PTR_ERR(olabel);
5113 goto out;
5114 }
5115
5116 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5117 if (status == 0)
5118 nfs_setsecurity(inode, &fattr, olabel);
5119
5120 nfs4_label_free(olabel);
5121out:
5122 put_rpccred(cred);
5123 return status;
5124}
5125#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5126
5127
Chuck Leverf0920752012-05-21 22:45:41 -04005128static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5129 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005130{
5131 __be32 verf[2];
5132
Chuck Lever2c820d92012-05-21 22:45:33 -04005133 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5134 /* An impossible timestamp guarantees this value
5135 * will never match a generated boot time. */
5136 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005137 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005138 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005139 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005140 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5141 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005142 }
Chuck Levercd937102012-03-02 17:14:31 -05005143 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5144}
5145
Jeff Laytona3192682015-06-09 19:43:59 -04005146static int
5147nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005148{
Jeff Laytona3192682015-06-09 19:43:59 -04005149 size_t len;
5150 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005151
Trond Myklebustceb3a162015-01-03 15:16:04 -05005152 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005153 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005154
Jeff Laytona3192682015-06-09 19:43:59 -04005155 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005156 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005157 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5158 1 +
5159 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5160 1;
5161 rcu_read_unlock();
5162
5163 if (len > NFS4_OPAQUE_LIMIT + 1)
5164 return -EINVAL;
5165
5166 /*
5167 * Since this string is allocated at mount time, and held until the
5168 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5169 * about a memory-reclaim deadlock.
5170 */
5171 str = kmalloc(len, GFP_KERNEL);
5172 if (!str)
5173 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005174
Chuck Levere984a552012-09-14 17:24:21 -04005175 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005176 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005177 clp->cl_ipaddr,
5178 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5179 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005180 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005181
Jeff Laytona3192682015-06-09 19:43:59 -04005182 clp->cl_owner_id = str;
5183 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005184}
5185
Jeff Layton873e3852015-06-09 19:44:00 -04005186static int
5187nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005188{
Jeff Layton873e3852015-06-09 19:44:00 -04005189 size_t len;
5190 char *str;
5191
5192 len = 10 + 10 + 1 + 10 + 1 +
5193 strlen(nfs4_client_id_uniquifier) + 1 +
5194 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5195
5196 if (len > NFS4_OPAQUE_LIMIT + 1)
5197 return -EINVAL;
5198
5199 /*
5200 * Since this string is allocated at mount time, and held until the
5201 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5202 * about a memory-reclaim deadlock.
5203 */
5204 str = kmalloc(len, GFP_KERNEL);
5205 if (!str)
5206 return -ENOMEM;
5207
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005208 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005209 clp->rpc_ops->version, clp->cl_minorversion,
5210 nfs4_client_id_uniquifier,
5211 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005212 clp->cl_owner_id = str;
5213 return 0;
5214}
5215
5216static int
5217nfs4_init_uniform_client_string(struct nfs_client *clp)
5218{
Jeff Layton873e3852015-06-09 19:44:00 -04005219 size_t len;
5220 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005221
5222 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005223 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005224
5225 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005226 return nfs4_init_uniquifier_client_string(clp);
5227
5228 len = 10 + 10 + 1 + 10 + 1 +
5229 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5230
5231 if (len > NFS4_OPAQUE_LIMIT + 1)
5232 return -EINVAL;
5233
5234 /*
5235 * Since this string is allocated at mount time, and held until the
5236 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5237 * about a memory-reclaim deadlock.
5238 */
5239 str = kmalloc(len, GFP_KERNEL);
5240 if (!str)
5241 return -ENOMEM;
5242
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005243 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005244 clp->rpc_ops->version, clp->cl_minorversion,
5245 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005246 clp->cl_owner_id = str;
5247 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005248}
5249
Chuck Lever706cb8d2014-03-12 12:51:47 -04005250/*
5251 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5252 * services. Advertise one based on the address family of the
5253 * clientaddr.
5254 */
5255static unsigned int
5256nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5257{
5258 if (strchr(clp->cl_ipaddr, ':') != NULL)
5259 return scnprintf(buf, len, "tcp6");
5260 else
5261 return scnprintf(buf, len, "tcp");
5262}
5263
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005264static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5265{
5266 struct nfs4_setclientid *sc = calldata;
5267
5268 if (task->tk_status == 0)
5269 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5270}
5271
5272static const struct rpc_call_ops nfs4_setclientid_ops = {
5273 .rpc_call_done = nfs4_setclientid_done,
5274};
5275
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005276/**
5277 * nfs4_proc_setclientid - Negotiate client ID
5278 * @clp: state data structure
5279 * @program: RPC program for NFSv4 callback service
5280 * @port: IP port number for NFS4 callback service
5281 * @cred: RPC credential to use for this call
5282 * @res: where to place the result
5283 *
5284 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5285 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005286int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5287 unsigned short port, struct rpc_cred *cred,
5288 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
5290 nfs4_verifier sc_verifier;
5291 struct nfs4_setclientid setclientid = {
5292 .sc_verifier = &sc_verifier,
5293 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005294 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 };
5296 struct rpc_message msg = {
5297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5298 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005299 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005300 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005302 struct rpc_task *task;
5303 struct rpc_task_setup task_setup_data = {
5304 .rpc_client = clp->cl_rpcclient,
5305 .rpc_message = &msg,
5306 .callback_ops = &nfs4_setclientid_ops,
5307 .callback_data = &setclientid,
5308 .flags = RPC_TASK_TIMEOUT,
5309 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005310 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
Chuck Leverde734832012-07-11 16:30:50 -04005312 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005313 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005314
5315 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5316 status = nfs4_init_uniform_client_string(clp);
5317 else
Jeff Laytona3192682015-06-09 19:43:59 -04005318 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005319
5320 if (status)
5321 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005322
Chuck Leverde734832012-07-11 16:30:50 -04005323 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005324 setclientid.sc_netid_len =
5325 nfs4_init_callback_netid(clp,
5326 setclientid.sc_netid,
5327 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005328 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005329 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 clp->cl_ipaddr, port >> 8, port & 255);
5331
Jeff Layton3a6bb732015-06-09 19:43:57 -04005332 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005333 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005334 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005335 task = rpc_run_task(&task_setup_data);
5336 if (IS_ERR(task)) {
5337 status = PTR_ERR(task);
5338 goto out;
5339 }
5340 status = task->tk_status;
5341 if (setclientid.sc_cred) {
5342 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5343 put_rpccred(setclientid.sc_cred);
5344 }
5345 rpc_put_task(task);
5346out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005347 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005348 dprintk("NFS reply setclientid: %d\n", status);
5349 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350}
5351
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005352/**
5353 * nfs4_proc_setclientid_confirm - Confirm client ID
5354 * @clp: state data structure
5355 * @res: result of a previous SETCLIENTID
5356 * @cred: RPC credential to use for this call
5357 *
5358 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5359 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005360int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005361 struct nfs4_setclientid_res *arg,
5362 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 struct rpc_message msg = {
5365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005366 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005367 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 int status;
5370
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005371 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5372 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5373 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005374 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005375 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005376 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return status;
5378}
5379
Trond Myklebustfe650402006-01-03 09:55:18 +01005380struct nfs4_delegreturndata {
5381 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005382 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005383 struct nfs_fh fh;
5384 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005385 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005386 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005387 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005388 struct inode *inode;
5389 bool roc;
5390 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005391};
5392
Trond Myklebustfe650402006-01-03 09:55:18 +01005393static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5394{
5395 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005396
Trond Myklebust14516c32010-07-31 14:29:06 -04005397 if (!nfs4_sequence_done(task, &data->res.seq_res))
5398 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005399
Trond Myklebustca8acf82013-08-13 10:36:56 -04005400 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005401 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005402 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005403 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005404 case -NFS4ERR_ADMIN_REVOKED:
5405 case -NFS4ERR_DELEG_REVOKED:
5406 case -NFS4ERR_BAD_STATEID:
5407 case -NFS4ERR_OLD_STATEID:
5408 case -NFS4ERR_STALE_STATEID:
5409 case -NFS4ERR_EXPIRED:
5410 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005411 if (data->roc)
5412 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005413 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005414 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005415 if (nfs4_async_handle_error(task, data->res.server,
5416 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005417 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005418 return;
5419 }
5420 }
5421 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005422}
5423
5424static void nfs4_delegreturn_release(void *calldata)
5425{
Peng Tao039b7562014-07-03 13:05:02 +08005426 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005427 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005428
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005429 if (inode) {
5430 if (data->roc)
5431 pnfs_roc_release(inode);
5432 nfs_iput_and_deactive(inode);
5433 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005434 kfree(calldata);
5435}
5436
Andy Adamson938e1012009-04-01 09:22:28 -04005437static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5438{
5439 struct nfs4_delegreturndata *d_data;
5440
5441 d_data = (struct nfs4_delegreturndata *)data;
5442
Peng Tao500d7012015-09-22 11:35:22 +08005443 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5444 return;
5445
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005446 if (d_data->roc)
5447 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005448
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005449 nfs4_setup_sequence(d_data->res.server,
5450 &d_data->args.seq_args,
5451 &d_data->res.seq_res,
5452 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005453}
Andy Adamson938e1012009-04-01 09:22:28 -04005454
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005455static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005456 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005457 .rpc_call_done = nfs4_delegreturn_done,
5458 .rpc_release = nfs4_delegreturn_release,
5459};
5460
Trond Myklebuste6f81072008-01-24 18:14:34 -05005461static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005462{
5463 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005464 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005465 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005466 struct rpc_message msg = {
5467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5468 .rpc_cred = cred,
5469 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005470 struct rpc_task_setup task_setup_data = {
5471 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005472 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005473 .callback_ops = &nfs4_delegreturn_ops,
5474 .flags = RPC_TASK_ASYNC,
5475 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005476 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005477
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005478 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005479 if (data == NULL)
5480 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005481 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005482
5483 nfs4_state_protect(server->nfs_client,
5484 NFS_SP4_MACH_CRED_CLEANUP,
5485 &task_setup_data.rpc_client, &msg);
5486
Trond Myklebustfe650402006-01-03 09:55:18 +01005487 data->args.fhandle = &data->fh;
5488 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005489 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005490 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005491 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005492 data->res.fattr = &data->fattr;
5493 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005494 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005495 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005496 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005497 data->inode = nfs_igrab_and_active(inode);
5498 if (data->inode)
5499 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005500
Trond Myklebustc970aa82007-07-14 15:39:59 -04005501 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005502 msg.rpc_argp = &data->args;
5503 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005504 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005505 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005506 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005507 if (!issync)
5508 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005509 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005510 if (status != 0)
5511 goto out;
5512 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005513 if (status == 0)
5514 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5515 else
5516 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005517out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005518 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005519 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520}
5521
Trond Myklebuste6f81072008-01-24 18:14:34 -05005522int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
5524 struct nfs_server *server = NFS_SERVER(inode);
5525 struct nfs4_exception exception = { };
5526 int err;
5527 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005528 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005529 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 switch (err) {
5531 case -NFS4ERR_STALE_STATEID:
5532 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 case 0:
5534 return 0;
5535 }
5536 err = nfs4_handle_exception(server, err, &exception);
5537 } while (exception.retry);
5538 return err;
5539}
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5542{
5543 struct inode *inode = state->inode;
5544 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005545 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005546 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005548 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005550 struct nfs_lockt_res res = {
5551 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 };
5553 struct rpc_message msg = {
5554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5555 .rpc_argp = &arg,
5556 .rpc_resp = &res,
5557 .rpc_cred = state->owner->so_cred,
5558 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 struct nfs4_lock_state *lsp;
5560 int status;
5561
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005562 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005563 status = nfs4_set_lock_state(state, request);
5564 if (status != 0)
5565 goto out;
5566 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005567 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005568 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005569 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005570 switch (status) {
5571 case 0:
5572 request->fl_type = F_UNLCK;
5573 break;
5574 case -NFS4ERR_DENIED:
5575 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005577 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005578 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005579out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 return status;
5581}
5582
5583static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5584{
5585 struct nfs4_exception exception = { };
5586 int err;
5587
5588 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005589 err = _nfs4_proc_getlk(state, cmd, request);
5590 trace_nfs4_get_lock(request, state, cmd, err);
5591 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 &exception);
5593 } while (exception.retry);
5594 return err;
5595}
5596
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005597struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005598 struct nfs_locku_args arg;
5599 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005600 struct nfs4_lock_state *lsp;
5601 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005602 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005603 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005604 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605};
5606
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005607static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5608 struct nfs_open_context *ctx,
5609 struct nfs4_lock_state *lsp,
5610 struct nfs_seqid *seqid)
5611{
5612 struct nfs4_unlockdata *p;
5613 struct inode *inode = lsp->ls_state->inode;
5614
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005615 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005616 if (p == NULL)
5617 return NULL;
5618 p->arg.fh = NFS_FH(inode);
5619 p->arg.fl = &p->fl;
5620 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005621 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005622 p->lsp = lsp;
5623 atomic_inc(&lsp->ls_count);
5624 /* Ensure we don't close file until we're done freeing locks! */
5625 p->ctx = get_nfs_open_context(ctx);
5626 memcpy(&p->fl, fl, sizeof(p->fl));
5627 p->server = NFS_SERVER(inode);
5628 return p;
5629}
5630
Trond Myklebust06f814a2006-01-03 09:55:07 +01005631static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005632{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005633 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005634 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005635 nfs4_put_lock_state(calldata->lsp);
5636 put_nfs_open_context(calldata->ctx);
5637 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005638}
5639
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005640static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005641{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005642 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005643
Trond Myklebust14516c32010-07-31 14:29:06 -04005644 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5645 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005646 switch (task->tk_status) {
5647 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005648 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005649 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005650 if (nfs4_update_lock_stateid(calldata->lsp,
5651 &calldata->res.stateid))
5652 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005653 case -NFS4ERR_BAD_STATEID:
5654 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005655 case -NFS4ERR_STALE_STATEID:
5656 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005657 if (!nfs4_stateid_match(&calldata->arg.stateid,
5658 &calldata->lsp->ls_stateid))
5659 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005660 break;
5661 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005662 if (nfs4_async_handle_error(task, calldata->server,
5663 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005664 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005665 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005666 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005667}
5668
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005669static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005670{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005671 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005673 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005674 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005675 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005676 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005677 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005678 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005679 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005680 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005681 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005682 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005683 &calldata->res.seq_res,
5684 task) != 0)
5685 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005686 return;
5687out_no_action:
5688 task->tk_action = NULL;
5689out_wait:
5690 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005693static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005694 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005695 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005696 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005697};
5698
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005699static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5700 struct nfs_open_context *ctx,
5701 struct nfs4_lock_state *lsp,
5702 struct nfs_seqid *seqid)
5703{
5704 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005705 struct rpc_message msg = {
5706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5707 .rpc_cred = ctx->cred,
5708 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005709 struct rpc_task_setup task_setup_data = {
5710 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005711 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005712 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005713 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005714 .flags = RPC_TASK_ASYNC,
5715 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005716
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005717 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5718 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5719
Frank Filz137d6ac2007-07-09 15:32:29 -07005720 /* Ensure this is an unlock - when canceling a lock, the
5721 * canceled lock is passed in, and it won't be an unlock.
5722 */
5723 fl->fl_type = F_UNLCK;
5724
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005725 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5726 if (data == NULL) {
5727 nfs_free_seqid(seqid);
5728 return ERR_PTR(-ENOMEM);
5729 }
5730
Chuck Levera9c92d62013-08-09 12:48:18 -04005731 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005732 msg.rpc_argp = &data->arg;
5733 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005734 task_setup_data.callback_data = data;
5735 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005736}
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5739{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005740 struct inode *inode = state->inode;
5741 struct nfs4_state_owner *sp = state->owner;
5742 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005744 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005745 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005746 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005747 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005748 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
Trond Myklebust9b073572006-06-29 16:38:34 -04005750 status = nfs4_set_lock_state(state, request);
5751 /* Unlock _before_ we do the RPC call */
5752 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005753 /* Exclude nfs_delegation_claim_locks() */
5754 mutex_lock(&sp->so_delegreturn_mutex);
5755 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005756 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005757 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005758 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005759 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005760 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005761 }
5762 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005763 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005764 if (status != 0)
5765 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005766 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005767 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005768 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5769 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005770 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5771 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005772 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005773 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005774 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005775 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005776 status = PTR_ERR(task);
5777 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005778 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005779 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005780 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005781out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005782 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005783 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785}
5786
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005787struct nfs4_lockdata {
5788 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005789 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005790 struct nfs4_lock_state *lsp;
5791 struct nfs_open_context *ctx;
5792 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005793 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005794 int rpc_status;
5795 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005796 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005797};
5798
5799static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005800 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5801 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005802{
5803 struct nfs4_lockdata *p;
5804 struct inode *inode = lsp->ls_state->inode;
5805 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005806 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005807
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005808 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005809 if (p == NULL)
5810 return NULL;
5811
5812 p->arg.fh = NFS_FH(inode);
5813 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005814 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005815 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005816 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005817 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5818 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005819 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005820 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005821 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005822 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005823 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005824 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005825 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005826 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005827 atomic_inc(&lsp->ls_count);
5828 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005829 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005830 memcpy(&p->fl, fl, sizeof(p->fl));
5831 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005832out_free_seqid:
5833 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005834out_free:
5835 kfree(p);
5836 return NULL;
5837}
5838
5839static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5840{
5841 struct nfs4_lockdata *data = calldata;
5842 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Harvey Harrison3110ff82008-05-02 13:42:44 -07005844 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005845 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005846 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005847 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005848 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005849 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005850 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005851 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005852 nfs4_stateid_copy(&data->arg.open_stateid,
5853 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005854 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005855 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005856 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005857 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005858 nfs4_stateid_copy(&data->arg.lock_stateid,
5859 &data->lsp->ls_stateid);
5860 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005861 if (!nfs4_valid_open_stateid(state)) {
5862 data->rpc_status = -EBADF;
5863 task->tk_action = NULL;
5864 goto out_release_open_seqid;
5865 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005866 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005867 if (nfs4_setup_sequence(data->server,
5868 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005869 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005870 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005871 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005872out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005873 nfs_release_seqid(data->arg.open_seqid);
5874out_release_lock_seqid:
5875 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005876out_wait:
5877 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005878 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005879}
5880
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5882{
5883 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005884 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005885
Harvey Harrison3110ff82008-05-02 13:42:44 -07005886 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005887
Trond Myklebust14516c32010-07-31 14:29:06 -04005888 if (!nfs4_sequence_done(task, &data->res.seq_res))
5889 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005890
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005891 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005892 switch (task->tk_status) {
5893 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005894 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005895 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005896 if (data->arg.new_lock) {
5897 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04005898 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005899 rpc_restart_call_prepare(task);
5900 break;
5901 }
5902 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005903 if (data->arg.new_lock_owner != 0) {
5904 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5905 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5906 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5907 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5908 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005909 break;
5910 case -NFS4ERR_BAD_STATEID:
5911 case -NFS4ERR_OLD_STATEID:
5912 case -NFS4ERR_STALE_STATEID:
5913 case -NFS4ERR_EXPIRED:
5914 if (data->arg.new_lock_owner != 0) {
5915 if (!nfs4_stateid_match(&data->arg.open_stateid,
5916 &lsp->ls_state->open_stateid))
5917 rpc_restart_call_prepare(task);
5918 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5919 &lsp->ls_stateid))
5920 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005921 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005922 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005923}
5924
5925static void nfs4_lock_release(void *calldata)
5926{
5927 struct nfs4_lockdata *data = calldata;
5928
Harvey Harrison3110ff82008-05-02 13:42:44 -07005929 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005930 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005931 if (data->cancelled != 0) {
5932 struct rpc_task *task;
5933 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5934 data->arg.lock_seqid);
5935 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005936 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005937 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005938 } else
5939 nfs_free_seqid(data->arg.lock_seqid);
5940 nfs4_put_lock_state(data->lsp);
5941 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005942 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005943 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005944 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005945}
5946
5947static const struct rpc_call_ops nfs4_lock_ops = {
5948 .rpc_call_prepare = nfs4_lock_prepare,
5949 .rpc_call_done = nfs4_lock_done,
5950 .rpc_release = nfs4_lock_release,
5951};
5952
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005953static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5954{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005955 switch (error) {
5956 case -NFS4ERR_ADMIN_REVOKED:
5957 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005958 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005959 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005960 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005961 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005962 break;
5963 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005964 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005965 case -NFS4ERR_EXPIRED:
5966 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005967 };
5968}
5969
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005970static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005971{
5972 struct nfs4_lockdata *data;
5973 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005974 struct rpc_message msg = {
5975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5976 .rpc_cred = state->owner->so_cred,
5977 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005978 struct rpc_task_setup task_setup_data = {
5979 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005980 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005981 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005982 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005983 .flags = RPC_TASK_ASYNC,
5984 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005985 int ret;
5986
Harvey Harrison3110ff82008-05-02 13:42:44 -07005987 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005988 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005989 fl->fl_u.nfs4_fl.owner,
5990 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991 if (data == NULL)
5992 return -ENOMEM;
5993 if (IS_SETLKW(cmd))
5994 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005995 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005996 msg.rpc_argp = &data->arg;
5997 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005998 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005999 if (recovery_type > NFS_LOCK_NEW) {
6000 if (recovery_type == NFS_LOCK_RECLAIM)
6001 data->arg.reclaim = NFS_LOCK_RECLAIM;
6002 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006003 } else
6004 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006005 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006006 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008 ret = nfs4_wait_for_completion_rpc_task(task);
6009 if (ret == 0) {
6010 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006011 if (ret)
6012 nfs4_handle_setlk_error(data->server, data->lsp,
6013 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006014 } else
6015 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006016 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006017 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006018 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020}
6021
6022static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6023{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006024 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006025 struct nfs4_exception exception = {
6026 .inode = state->inode,
6027 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006028 int err;
6029
6030 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006031 /* Cache the lock if possible... */
6032 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6033 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006034 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006035 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006036 break;
6037 nfs4_handle_exception(server, err, &exception);
6038 } while (exception.retry);
6039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040}
6041
6042static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6043{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006044 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006045 struct nfs4_exception exception = {
6046 .inode = state->inode,
6047 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006048 int err;
6049
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006050 err = nfs4_set_lock_state(state, request);
6051 if (err != 0)
6052 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006053 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006054 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6055 return 0;
6056 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006057 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006058 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6059 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006060 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006061 switch (err) {
6062 default:
6063 goto out;
6064 case -NFS4ERR_GRACE:
6065 case -NFS4ERR_DELAY:
6066 nfs4_handle_exception(server, err, &exception);
6067 err = 0;
6068 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006069 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006070out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072}
6073
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006074#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006075/**
6076 * nfs41_check_expired_locks - possibly free a lock stateid
6077 *
6078 * @state: NFSv4 state for an inode
6079 *
6080 * Returns NFS_OK if recovery for this stateid is now finished.
6081 * Otherwise a negative NFS4ERR value is returned.
6082 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006083static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006084{
Chuck Levereb64cf92012-07-11 16:30:05 -04006085 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006086 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006087 struct nfs_server *server = NFS_SERVER(state->inode);
6088
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006089 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006090 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006091 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6092
6093 status = nfs41_test_stateid(server,
6094 &lsp->ls_stateid,
6095 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006096 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006097 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006098 /* Free the stateid unless the server
6099 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006100 if (status != -NFS4ERR_BAD_STATEID)
6101 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006102 &lsp->ls_stateid,
6103 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006104 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006105 ret = status;
6106 }
6107 }
6108 };
6109
6110 return ret;
6111}
6112
6113static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6114{
6115 int status = NFS_OK;
6116
6117 if (test_bit(LK_STATE_IN_USE, &state->flags))
6118 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006119 if (status != NFS_OK)
6120 status = nfs4_lock_expired(state, request);
6121 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006122}
6123#endif
6124
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6126{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006127 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006128 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006129 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006130 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Trond Myklebust01c3b862006-06-29 16:38:39 -04006132 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006133 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006134 if (status < 0)
6135 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006136 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006137 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006138 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006139 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006140 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006141 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006142 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006143 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006144 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006145 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006146 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006147 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006148 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006149 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006150out:
6151 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 return status;
6153}
6154
6155static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6156{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006157 struct nfs4_exception exception = {
6158 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006159 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006160 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 int err;
6162
6163 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006164 err = _nfs4_proc_setlk(state, cmd, request);
6165 if (err == -NFS4ERR_DENIED)
6166 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006168 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 } while (exception.retry);
6170 return err;
6171}
6172
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006173#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6174#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6175
6176static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006177nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6178 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006179{
6180 int status = -ERESTARTSYS;
6181 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6182
6183 while(!signalled()) {
6184 status = nfs4_proc_setlk(state, cmd, request);
6185 if ((status != -EAGAIN) || IS_SETLK(cmd))
6186 break;
6187 freezable_schedule_timeout_interruptible(timeout);
6188 timeout *= 2;
6189 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6190 status = -ERESTARTSYS;
6191 }
6192 return status;
6193}
6194
Jeff Laytona1d617d82016-09-17 18:17:39 -04006195#ifdef CONFIG_NFS_V4_1
6196struct nfs4_lock_waiter {
6197 struct task_struct *task;
6198 struct inode *inode;
6199 struct nfs_lowner *owner;
6200 bool notified;
6201};
6202
6203static int
6204nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6205{
6206 int ret;
6207 struct cb_notify_lock_args *cbnl = key;
6208 struct nfs4_lock_waiter *waiter = wait->private;
6209 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6210 *wowner = waiter->owner;
6211
6212 /* Only wake if the callback was for the same owner */
6213 if (lowner->clientid != wowner->clientid ||
6214 lowner->id != wowner->id ||
6215 lowner->s_dev != wowner->s_dev)
6216 return 0;
6217
6218 /* Make sure it's for the right inode */
6219 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6220 return 0;
6221
6222 waiter->notified = true;
6223
6224 /* override "private" so we can use default_wake_function */
6225 wait->private = waiter->task;
6226 ret = autoremove_wake_function(wait, mode, flags, key);
6227 wait->private = waiter;
6228 return ret;
6229}
6230
6231static int
6232nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6233{
6234 int status = -ERESTARTSYS;
6235 unsigned long flags;
6236 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6237 struct nfs_server *server = NFS_SERVER(state->inode);
6238 struct nfs_client *clp = server->nfs_client;
6239 wait_queue_head_t *q = &clp->cl_lock_waitq;
6240 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6241 .id = lsp->ls_seqid.owner_id,
6242 .s_dev = server->s_dev };
6243 struct nfs4_lock_waiter waiter = { .task = current,
6244 .inode = state->inode,
6245 .owner = &owner,
6246 .notified = false };
6247 wait_queue_t wait;
6248
6249 /* Don't bother with waitqueue if we don't expect a callback */
6250 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6251 return nfs4_retry_setlk_simple(state, cmd, request);
6252
6253 init_wait(&wait);
6254 wait.private = &waiter;
6255 wait.func = nfs4_wake_lock_waiter;
6256 add_wait_queue(q, &wait);
6257
6258 while(!signalled()) {
6259 status = nfs4_proc_setlk(state, cmd, request);
6260 if ((status != -EAGAIN) || IS_SETLK(cmd))
6261 break;
6262
6263 status = -ERESTARTSYS;
6264 spin_lock_irqsave(&q->lock, flags);
6265 if (waiter.notified) {
6266 spin_unlock_irqrestore(&q->lock, flags);
6267 continue;
6268 }
6269 set_current_state(TASK_INTERRUPTIBLE);
6270 spin_unlock_irqrestore(&q->lock, flags);
6271
6272 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6273 }
6274
6275 finish_wait(q, &wait);
6276 return status;
6277}
6278#else /* !CONFIG_NFS_V4_1 */
6279static inline int
6280nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6281{
6282 return nfs4_retry_setlk_simple(state, cmd, request);
6283}
6284#endif
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286static int
6287nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6288{
6289 struct nfs_open_context *ctx;
6290 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 int status;
6292
6293 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006294 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 state = ctx->state;
6296
6297 if (request->fl_start < 0 || request->fl_end < 0)
6298 return -EINVAL;
6299
Trond Myklebustd9531262009-07-21 19:22:38 -04006300 if (IS_GETLK(cmd)) {
6301 if (state != NULL)
6302 return nfs4_proc_getlk(state, F_GETLK, request);
6303 return 0;
6304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
6306 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6307 return -EINVAL;
6308
Trond Myklebustd9531262009-07-21 19:22:38 -04006309 if (request->fl_type == F_UNLCK) {
6310 if (state != NULL)
6311 return nfs4_proc_unlck(state, cmd, request);
6312 return 0;
6313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Trond Myklebustd9531262009-07-21 19:22:38 -04006315 if (state == NULL)
6316 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006317
6318 if ((request->fl_flags & FL_POSIX) &&
6319 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6320 return -ENOLCK;
6321
Trond Myklebust55725512012-04-18 12:48:35 -04006322 /*
6323 * Don't rely on the VFS having checked the file open mode,
6324 * since it won't do this for flock() locks.
6325 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006326 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006327 case F_RDLCK:
6328 if (!(filp->f_mode & FMODE_READ))
6329 return -EBADF;
6330 break;
6331 case F_WRLCK:
6332 if (!(filp->f_mode & FMODE_WRITE))
6333 return -EBADF;
6334 }
6335
Jeff Layton1ea67db2016-09-17 18:17:37 -04006336 status = nfs4_set_lock_state(state, request);
6337 if (status != 0)
6338 return status;
6339
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006340 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341}
6342
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006343int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006344{
6345 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006346 int err;
6347
6348 err = nfs4_set_lock_state(state, fl);
6349 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006350 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006351 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006352 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006353}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006354
Trond Myklebustcf470c32012-03-07 13:49:12 -05006355struct nfs_release_lockowner_data {
6356 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006357 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006358 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006359 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006360 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006361};
6362
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006363static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6364{
6365 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006366 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006367 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6368 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006369 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006370 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006371}
6372
6373static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6374{
6375 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006376 struct nfs_server *server = data->server;
6377
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006378 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006379
6380 switch (task->tk_status) {
6381 case 0:
6382 renew_lease(server, data->timestamp);
6383 break;
6384 case -NFS4ERR_STALE_CLIENTID:
6385 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006386 nfs4_schedule_lease_recovery(server->nfs_client);
6387 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006388 case -NFS4ERR_LEASE_MOVED:
6389 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006390 if (nfs4_async_handle_error(task, server,
6391 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006392 rpc_restart_call_prepare(task);
6393 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006394}
6395
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006396static void nfs4_release_lockowner_release(void *calldata)
6397{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006398 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006399 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006400 kfree(calldata);
6401}
6402
Trond Myklebust17280172012-03-11 13:11:00 -04006403static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006404 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6405 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006406 .rpc_release = nfs4_release_lockowner_release,
6407};
6408
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006409static void
6410nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006411{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006412 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006413 struct rpc_message msg = {
6414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6415 };
6416
6417 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006418 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006419
Trond Myklebustcf470c32012-03-07 13:49:12 -05006420 data = kmalloc(sizeof(*data), GFP_NOFS);
6421 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006422 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006423 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006424 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006425 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6426 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6427 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006428
Trond Myklebustcf470c32012-03-07 13:49:12 -05006429 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006430 msg.rpc_resp = &data->res;
6431 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006432 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006433}
6434
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006435#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6436
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006437static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006438 struct dentry *unused, struct inode *inode,
6439 const char *key, const void *buf,
6440 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006441{
Al Viro59301222016-05-27 10:19:30 -04006442 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006443}
6444
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006445static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006446 struct dentry *unused, struct inode *inode,
6447 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006448{
Al Virob2968212016-04-10 20:48:24 -04006449 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006450}
6451
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006452static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006453{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006454 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006455}
6456
David Quigleyc9bccef2013-05-22 12:50:45 -04006457#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006458
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006459static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006460 struct dentry *unused, struct inode *inode,
6461 const char *key, const void *buf,
6462 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006463{
6464 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006465 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006466
6467 return -EOPNOTSUPP;
6468}
6469
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006470static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006471 struct dentry *unused, struct inode *inode,
6472 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006473{
6474 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006475 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006476 return -EOPNOTSUPP;
6477}
6478
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006479static ssize_t
6480nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006481{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006482 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006483
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006484 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6485 len = security_inode_listsecurity(inode, list, list_len);
6486 if (list_len && len > list_len)
6487 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006488 }
6489 return len;
6490}
6491
6492static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6493 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006494 .get = nfs4_xattr_get_nfs4_label,
6495 .set = nfs4_xattr_set_nfs4_label,
6496};
David Quigleyc9bccef2013-05-22 12:50:45 -04006497
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006498#else
6499
6500static ssize_t
6501nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6502{
6503 return 0;
6504}
6505
6506#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006507
Andy Adamson533eb462011-06-13 18:25:56 -04006508/*
6509 * nfs_fhget will use either the mounted_on_fileid or the fileid
6510 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006511static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6512{
Andy Adamson533eb462011-06-13 18:25:56 -04006513 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6514 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6515 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006516 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006517 return;
6518
6519 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006520 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006521 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6522 fattr->nlink = 2;
6523}
6524
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006525static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6526 const struct qstr *name,
6527 struct nfs4_fs_locations *fs_locations,
6528 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006529{
6530 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006531 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006532 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006533 };
6534 struct nfs4_fs_locations_arg args = {
6535 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006536 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006537 .page = page,
6538 .bitmask = bitmask,
6539 };
Benny Halevy22958462009-04-01 09:22:02 -04006540 struct nfs4_fs_locations_res res = {
6541 .fs_locations = fs_locations,
6542 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006543 struct rpc_message msg = {
6544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6545 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006546 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006547 };
6548 int status;
6549
Harvey Harrison3110ff82008-05-02 13:42:44 -07006550 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006551
6552 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6553 * is not supported */
6554 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6555 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6556 else
6557 bitmask[0] |= FATTR4_WORD0_FILEID;
6558
Trond Myklebustc228fd32007-01-13 02:28:11 -05006559 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006560 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006561 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006562 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006563 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006564 return status;
6565}
6566
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006567int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6568 const struct qstr *name,
6569 struct nfs4_fs_locations *fs_locations,
6570 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006571{
6572 struct nfs4_exception exception = { };
6573 int err;
6574 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006575 err = _nfs4_proc_fs_locations(client, dir, name,
6576 fs_locations, page);
6577 trace_nfs4_get_fs_locations(dir, name, err);
6578 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006579 &exception);
6580 } while (exception.retry);
6581 return err;
6582}
6583
Chuck Leverb03d7352013-10-17 14:12:50 -04006584/*
6585 * This operation also signals the server that this client is
6586 * performing migration recovery. The server can stop returning
6587 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6588 * appended to this compound to identify the client ID which is
6589 * performing recovery.
6590 */
6591static int _nfs40_proc_get_locations(struct inode *inode,
6592 struct nfs4_fs_locations *locations,
6593 struct page *page, struct rpc_cred *cred)
6594{
6595 struct nfs_server *server = NFS_SERVER(inode);
6596 struct rpc_clnt *clnt = server->client;
6597 u32 bitmask[2] = {
6598 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6599 };
6600 struct nfs4_fs_locations_arg args = {
6601 .clientid = server->nfs_client->cl_clientid,
6602 .fh = NFS_FH(inode),
6603 .page = page,
6604 .bitmask = bitmask,
6605 .migration = 1, /* skip LOOKUP */
6606 .renew = 1, /* append RENEW */
6607 };
6608 struct nfs4_fs_locations_res res = {
6609 .fs_locations = locations,
6610 .migration = 1,
6611 .renew = 1,
6612 };
6613 struct rpc_message msg = {
6614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6615 .rpc_argp = &args,
6616 .rpc_resp = &res,
6617 .rpc_cred = cred,
6618 };
6619 unsigned long now = jiffies;
6620 int status;
6621
6622 nfs_fattr_init(&locations->fattr);
6623 locations->server = server;
6624 locations->nlocations = 0;
6625
6626 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6627 nfs4_set_sequence_privileged(&args.seq_args);
6628 status = nfs4_call_sync_sequence(clnt, server, &msg,
6629 &args.seq_args, &res.seq_res);
6630 if (status)
6631 return status;
6632
6633 renew_lease(server, now);
6634 return 0;
6635}
6636
6637#ifdef CONFIG_NFS_V4_1
6638
6639/*
6640 * This operation also signals the server that this client is
6641 * performing migration recovery. The server can stop asserting
6642 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6643 * performing this operation is identified in the SEQUENCE
6644 * operation in this compound.
6645 *
6646 * When the client supports GETATTR(fs_locations_info), it can
6647 * be plumbed in here.
6648 */
6649static int _nfs41_proc_get_locations(struct inode *inode,
6650 struct nfs4_fs_locations *locations,
6651 struct page *page, struct rpc_cred *cred)
6652{
6653 struct nfs_server *server = NFS_SERVER(inode);
6654 struct rpc_clnt *clnt = server->client;
6655 u32 bitmask[2] = {
6656 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6657 };
6658 struct nfs4_fs_locations_arg args = {
6659 .fh = NFS_FH(inode),
6660 .page = page,
6661 .bitmask = bitmask,
6662 .migration = 1, /* skip LOOKUP */
6663 };
6664 struct nfs4_fs_locations_res res = {
6665 .fs_locations = locations,
6666 .migration = 1,
6667 };
6668 struct rpc_message msg = {
6669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6670 .rpc_argp = &args,
6671 .rpc_resp = &res,
6672 .rpc_cred = cred,
6673 };
6674 int status;
6675
6676 nfs_fattr_init(&locations->fattr);
6677 locations->server = server;
6678 locations->nlocations = 0;
6679
6680 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6681 nfs4_set_sequence_privileged(&args.seq_args);
6682 status = nfs4_call_sync_sequence(clnt, server, &msg,
6683 &args.seq_args, &res.seq_res);
6684 if (status == NFS4_OK &&
6685 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6686 status = -NFS4ERR_LEASE_MOVED;
6687 return status;
6688}
6689
6690#endif /* CONFIG_NFS_V4_1 */
6691
6692/**
6693 * nfs4_proc_get_locations - discover locations for a migrated FSID
6694 * @inode: inode on FSID that is migrating
6695 * @locations: result of query
6696 * @page: buffer
6697 * @cred: credential to use for this operation
6698 *
6699 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6700 * operation failed, or a negative errno if a local error occurred.
6701 *
6702 * On success, "locations" is filled in, but if the server has
6703 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6704 * asserted.
6705 *
6706 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6707 * from this client that require migration recovery.
6708 */
6709int nfs4_proc_get_locations(struct inode *inode,
6710 struct nfs4_fs_locations *locations,
6711 struct page *page, struct rpc_cred *cred)
6712{
6713 struct nfs_server *server = NFS_SERVER(inode);
6714 struct nfs_client *clp = server->nfs_client;
6715 const struct nfs4_mig_recovery_ops *ops =
6716 clp->cl_mvops->mig_recovery_ops;
6717 struct nfs4_exception exception = { };
6718 int status;
6719
6720 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6721 (unsigned long long)server->fsid.major,
6722 (unsigned long long)server->fsid.minor,
6723 clp->cl_hostname);
6724 nfs_display_fhandle(NFS_FH(inode), __func__);
6725
6726 do {
6727 status = ops->get_locations(inode, locations, page, cred);
6728 if (status != -NFS4ERR_DELAY)
6729 break;
6730 nfs4_handle_exception(server, status, &exception);
6731 } while (exception.retry);
6732 return status;
6733}
6734
Chuck Lever44c99932013-10-17 14:13:30 -04006735/*
6736 * This operation also signals the server that this client is
6737 * performing "lease moved" recovery. The server can stop
6738 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6739 * is appended to this compound to identify the client ID which is
6740 * performing recovery.
6741 */
6742static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6743{
6744 struct nfs_server *server = NFS_SERVER(inode);
6745 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6746 struct rpc_clnt *clnt = server->client;
6747 struct nfs4_fsid_present_arg args = {
6748 .fh = NFS_FH(inode),
6749 .clientid = clp->cl_clientid,
6750 .renew = 1, /* append RENEW */
6751 };
6752 struct nfs4_fsid_present_res res = {
6753 .renew = 1,
6754 };
6755 struct rpc_message msg = {
6756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6757 .rpc_argp = &args,
6758 .rpc_resp = &res,
6759 .rpc_cred = cred,
6760 };
6761 unsigned long now = jiffies;
6762 int status;
6763
6764 res.fh = nfs_alloc_fhandle();
6765 if (res.fh == NULL)
6766 return -ENOMEM;
6767
6768 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6769 nfs4_set_sequence_privileged(&args.seq_args);
6770 status = nfs4_call_sync_sequence(clnt, server, &msg,
6771 &args.seq_args, &res.seq_res);
6772 nfs_free_fhandle(res.fh);
6773 if (status)
6774 return status;
6775
6776 do_renew_lease(clp, now);
6777 return 0;
6778}
6779
6780#ifdef CONFIG_NFS_V4_1
6781
6782/*
6783 * This operation also signals the server that this client is
6784 * performing "lease moved" recovery. The server can stop asserting
6785 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6786 * this operation is identified in the SEQUENCE operation in this
6787 * compound.
6788 */
6789static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6790{
6791 struct nfs_server *server = NFS_SERVER(inode);
6792 struct rpc_clnt *clnt = server->client;
6793 struct nfs4_fsid_present_arg args = {
6794 .fh = NFS_FH(inode),
6795 };
6796 struct nfs4_fsid_present_res res = {
6797 };
6798 struct rpc_message msg = {
6799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6800 .rpc_argp = &args,
6801 .rpc_resp = &res,
6802 .rpc_cred = cred,
6803 };
6804 int status;
6805
6806 res.fh = nfs_alloc_fhandle();
6807 if (res.fh == NULL)
6808 return -ENOMEM;
6809
6810 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6811 nfs4_set_sequence_privileged(&args.seq_args);
6812 status = nfs4_call_sync_sequence(clnt, server, &msg,
6813 &args.seq_args, &res.seq_res);
6814 nfs_free_fhandle(res.fh);
6815 if (status == NFS4_OK &&
6816 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6817 status = -NFS4ERR_LEASE_MOVED;
6818 return status;
6819}
6820
6821#endif /* CONFIG_NFS_V4_1 */
6822
6823/**
6824 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6825 * @inode: inode on FSID to check
6826 * @cred: credential to use for this operation
6827 *
6828 * Server indicates whether the FSID is present, moved, or not
6829 * recognized. This operation is necessary to clear a LEASE_MOVED
6830 * condition for this client ID.
6831 *
6832 * Returns NFS4_OK if the FSID is present on this server,
6833 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6834 * NFS4ERR code if some error occurred on the server, or a
6835 * negative errno if a local failure occurred.
6836 */
6837int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6838{
6839 struct nfs_server *server = NFS_SERVER(inode);
6840 struct nfs_client *clp = server->nfs_client;
6841 const struct nfs4_mig_recovery_ops *ops =
6842 clp->cl_mvops->mig_recovery_ops;
6843 struct nfs4_exception exception = { };
6844 int status;
6845
6846 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6847 (unsigned long long)server->fsid.major,
6848 (unsigned long long)server->fsid.minor,
6849 clp->cl_hostname);
6850 nfs_display_fhandle(NFS_FH(inode), __func__);
6851
6852 do {
6853 status = ops->fsid_present(inode, cred);
6854 if (status != -NFS4ERR_DELAY)
6855 break;
6856 nfs4_handle_exception(server, status, &exception);
6857 } while (exception.retry);
6858 return status;
6859}
6860
Andy Adamson5ec16a82013-08-08 10:57:55 -04006861/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006862 * If 'use_integrity' is true and the state managment nfs_client
6863 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6864 * and the machine credential as per RFC3530bis and RFC5661 Security
6865 * Considerations sections. Otherwise, just use the user cred with the
6866 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006867 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006868static 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 +00006869{
6870 int status;
6871 struct nfs4_secinfo_arg args = {
6872 .dir_fh = NFS_FH(dir),
6873 .name = name,
6874 };
6875 struct nfs4_secinfo_res res = {
6876 .flavors = flavors,
6877 };
6878 struct rpc_message msg = {
6879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6880 .rpc_argp = &args,
6881 .rpc_resp = &res,
6882 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006883 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006884 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006885
6886 if (use_integrity) {
6887 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006888 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6889 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006890 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006891
6892 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006893
6894 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6895 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6896
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006897 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6898 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006899 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006900
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006901 if (cred)
6902 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006903
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006904 return status;
6905}
6906
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006907int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6908 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006909{
6910 struct nfs4_exception exception = { };
6911 int err;
6912 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006913 err = -NFS4ERR_WRONGSEC;
6914
6915 /* try to use integrity protection with machine cred */
6916 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6917 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6918
6919 /*
6920 * if unable to use integrity protection, or SECINFO with
6921 * integrity protection returns NFS4ERR_WRONGSEC (which is
6922 * disallowed by spec, but exists in deployed servers) use
6923 * the current filesystem's rpc_client and the user cred.
6924 */
6925 if (err == -NFS4ERR_WRONGSEC)
6926 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6927
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006928 trace_nfs4_secinfo(dir, name, err);
6929 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006930 &exception);
6931 } while (exception.retry);
6932 return err;
6933}
6934
Andy Adamson557134a2009-04-01 09:21:53 -04006935#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006936/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006937 * Check the exchange flags returned by the server for invalid flags, having
6938 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6939 * DS flags set.
6940 */
6941static int nfs4_check_cl_exchange_flags(u32 flags)
6942{
6943 if (flags & ~EXCHGID4_FLAG_MASK_R)
6944 goto out_inval;
6945 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6946 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6947 goto out_inval;
6948 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6949 goto out_inval;
6950 return NFS_OK;
6951out_inval:
6952 return -NFS4ERR_INVAL;
6953}
6954
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006955static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006956nfs41_same_server_scope(struct nfs41_server_scope *a,
6957 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006958{
6959 if (a->server_scope_sz == b->server_scope_sz &&
6960 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6961 return true;
6962
6963 return false;
6964}
6965
Andy Adamson02a95de2016-02-05 16:08:37 -05006966static void
6967nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6968{
6969}
6970
6971static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6972 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6973};
6974
Andy Adamson357f54d2010-12-14 10:11:57 -05006975/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006976 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006977 *
6978 * The 4.1 client currently uses the same TCP connection for the
6979 * fore and backchannel.
6980 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006981static
6982int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6983 struct rpc_xprt *xprt,
6984 struct nfs_client *clp,
6985 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006986{
6987 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006988 struct nfs41_bind_conn_to_session_args args = {
6989 .client = clp,
6990 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6991 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006992 struct nfs41_bind_conn_to_session_res res;
6993 struct rpc_message msg = {
6994 .rpc_proc =
6995 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006996 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006997 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006998 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006999 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007000 struct rpc_task_setup task_setup_data = {
7001 .rpc_client = clnt,
7002 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007003 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007004 .rpc_message = &msg,
7005 .flags = RPC_TASK_TIMEOUT,
7006 };
7007 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007008
7009 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007010
Trond Myklebust71a097c2015-02-18 09:27:18 -08007011 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7012 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7013 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007014
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007015 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7016 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7017 args.dir = NFS4_CDFC4_FORE;
7018
7019 task = rpc_run_task(&task_setup_data);
7020 if (!IS_ERR(task)) {
7021 status = task->tk_status;
7022 rpc_put_task(task);
7023 } else
7024 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007025 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007026 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007027 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007028 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7029 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7030 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007031 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007032 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007033 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007034 dprintk("NFS: %s: Unexpected direction from server\n",
7035 __func__);
7036 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007037 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007038 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007039 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007040 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7041 __func__);
7042 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007043 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007044 }
7045 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007046out:
7047 dprintk("<-- %s status= %d\n", __func__, status);
7048 return status;
7049}
7050
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007051struct rpc_bind_conn_calldata {
7052 struct nfs_client *clp;
7053 struct rpc_cred *cred;
7054};
7055
7056static int
7057nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7058 struct rpc_xprt *xprt,
7059 void *calldata)
7060{
7061 struct rpc_bind_conn_calldata *p = calldata;
7062
7063 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7064}
7065
7066int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7067{
7068 struct rpc_bind_conn_calldata data = {
7069 .clp = clp,
7070 .cred = cred,
7071 };
7072 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7073 nfs4_proc_bind_conn_to_session_callback, &data);
7074}
7075
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007076/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007077 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7078 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007079 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007080static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7081 .how = SP4_MACH_CRED,
7082 .enforce.u.words = {
7083 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7084 1 << (OP_EXCHANGE_ID - 32) |
7085 1 << (OP_CREATE_SESSION - 32) |
7086 1 << (OP_DESTROY_SESSION - 32) |
7087 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007088 },
7089 .allow.u.words = {
7090 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007091 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007092 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007093 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007094 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007095 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007096 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007097 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007098 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007099 1 << (OP_FREE_STATEID - 32) |
7100 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007101 }
7102};
7103
7104/*
7105 * Select the state protection mode for client `clp' given the server results
7106 * from exchange_id in `sp'.
7107 *
7108 * Returns 0 on success, negative errno otherwise.
7109 */
7110static int nfs4_sp4_select_mode(struct nfs_client *clp,
7111 struct nfs41_state_protection *sp)
7112{
7113 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7114 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7115 1 << (OP_EXCHANGE_ID - 32) |
7116 1 << (OP_CREATE_SESSION - 32) |
7117 1 << (OP_DESTROY_SESSION - 32) |
7118 1 << (OP_DESTROY_CLIENTID - 32)
7119 };
7120 unsigned int i;
7121
7122 if (sp->how == SP4_MACH_CRED) {
7123 /* Print state protect result */
7124 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7125 for (i = 0; i <= LAST_NFS4_OP; i++) {
7126 if (test_bit(i, sp->enforce.u.longs))
7127 dfprintk(MOUNT, " enforce op %d\n", i);
7128 if (test_bit(i, sp->allow.u.longs))
7129 dfprintk(MOUNT, " allow op %d\n", i);
7130 }
7131
7132 /* make sure nothing is on enforce list that isn't supported */
7133 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7134 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7135 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7136 return -EINVAL;
7137 }
7138 }
7139
7140 /*
7141 * Minimal mode - state operations are allowed to use machine
7142 * credential. Note this already happens by default, so the
7143 * client doesn't have to do anything more than the negotiation.
7144 *
7145 * NOTE: we don't care if EXCHANGE_ID is in the list -
7146 * we're already using the machine cred for exchange_id
7147 * and will never use a different cred.
7148 */
7149 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7150 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7151 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7152 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7153 dfprintk(MOUNT, "sp4_mach_cred:\n");
7154 dfprintk(MOUNT, " minimal mode enabled\n");
7155 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7156 } else {
7157 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7158 return -EINVAL;
7159 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007160
7161 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007162 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7163 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007164 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7165 dfprintk(MOUNT, " cleanup mode enabled\n");
7166 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7167 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007168
Andrew Elble99ade3c2015-12-02 09:39:51 -05007169 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7170 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7171 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7172 &clp->cl_sp4_flags);
7173 }
7174
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007175 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7176 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7177 dfprintk(MOUNT, " secinfo mode enabled\n");
7178 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7179 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007180
7181 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7182 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7183 dfprintk(MOUNT, " stateid mode enabled\n");
7184 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7185 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007186
7187 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7188 dfprintk(MOUNT, " write mode enabled\n");
7189 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7190 }
7191
7192 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7193 dfprintk(MOUNT, " commit mode enabled\n");
7194 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7195 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007196 }
7197
7198 return 0;
7199}
7200
Andy Adamson8d89bd72016-09-09 09:22:18 -04007201struct nfs41_exchange_id_data {
7202 struct nfs41_exchange_id_res res;
7203 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007204 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007205 int rpc_status;
7206};
7207
7208static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7209{
7210 struct nfs41_exchange_id_data *cdata =
7211 (struct nfs41_exchange_id_data *)data;
7212 struct nfs_client *clp = cdata->args.client;
7213 int status = task->tk_status;
7214
7215 trace_nfs4_exchange_id(clp, status);
7216
7217 if (status == 0)
7218 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007219
7220 if (cdata->xprt && status == 0) {
7221 status = nfs4_detect_session_trunking(clp, &cdata->res,
7222 cdata->xprt);
7223 goto out;
7224 }
7225
Andy Adamson8d89bd72016-09-09 09:22:18 -04007226 if (status == 0)
7227 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7228
7229 if (status == 0) {
7230 clp->cl_clientid = cdata->res.clientid;
7231 clp->cl_exchange_flags = cdata->res.flags;
7232 /* Client ID is not confirmed */
7233 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7234 clear_bit(NFS4_SESSION_ESTABLISHED,
7235 &clp->cl_session->session_state);
7236 clp->cl_seqid = cdata->res.seqid;
7237 }
7238
7239 kfree(clp->cl_serverowner);
7240 clp->cl_serverowner = cdata->res.server_owner;
7241 cdata->res.server_owner = NULL;
7242
7243 /* use the most recent implementation id */
7244 kfree(clp->cl_implid);
7245 clp->cl_implid = cdata->res.impl_id;
7246 cdata->res.impl_id = NULL;
7247
7248 if (clp->cl_serverscope != NULL &&
7249 !nfs41_same_server_scope(clp->cl_serverscope,
7250 cdata->res.server_scope)) {
7251 dprintk("%s: server_scope mismatch detected\n",
7252 __func__);
7253 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7254 kfree(clp->cl_serverscope);
7255 clp->cl_serverscope = NULL;
7256 }
7257
7258 if (clp->cl_serverscope == NULL) {
7259 clp->cl_serverscope = cdata->res.server_scope;
7260 cdata->res.server_scope = NULL;
7261 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007262 /* Save the EXCHANGE_ID verifier session trunk tests */
7263 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7264 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007265 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007266out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007267 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007268 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007269}
7270
7271static void nfs4_exchange_id_release(void *data)
7272{
7273 struct nfs41_exchange_id_data *cdata =
7274 (struct nfs41_exchange_id_data *)data;
7275
7276 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007277 if (cdata->xprt) {
7278 xprt_put(cdata->xprt);
7279 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7280 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007281 kfree(cdata->res.impl_id);
7282 kfree(cdata->res.server_scope);
7283 kfree(cdata->res.server_owner);
7284 kfree(cdata);
7285}
7286
7287static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7288 .rpc_call_done = nfs4_exchange_id_done,
7289 .rpc_release = nfs4_exchange_id_release,
7290};
7291
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007292/*
7293 * _nfs4_proc_exchange_id()
7294 *
7295 * Wrapper for EXCHANGE_ID operation.
7296 */
7297static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007298 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007299{
7300 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007301 struct rpc_message msg = {
7302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007303 .rpc_cred = cred,
7304 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007305 struct rpc_task_setup task_setup_data = {
7306 .rpc_client = clp->cl_rpcclient,
7307 .callback_ops = &nfs4_exchange_id_call_ops,
7308 .rpc_message = &msg,
7309 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7310 };
7311 struct nfs41_exchange_id_data *calldata;
7312 struct rpc_task *task;
7313 int status = -EIO;
7314
7315 if (!atomic_inc_not_zero(&clp->cl_count))
7316 goto out;
7317
7318 status = -ENOMEM;
7319 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7320 if (!calldata)
7321 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007322
Andy Adamsonad0849a2016-09-09 09:22:28 -04007323 if (!xprt)
7324 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007325
7326 status = nfs4_init_uniform_client_string(clp);
7327 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007328 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007329
7330 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007331 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007332 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007333
Andy Adamson8d89bd72016-09-09 09:22:18 -04007334 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7335 GFP_NOFS);
7336 status = -ENOMEM;
7337 if (unlikely(calldata->res.server_owner == NULL))
7338 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007339
Andy Adamson8d89bd72016-09-09 09:22:18 -04007340 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007341 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007342 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007343 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007344
Andy Adamson8d89bd72016-09-09 09:22:18 -04007345 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7346 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007347 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007348
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007349 switch (sp4_how) {
7350 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007351 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007352 break;
7353
7354 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007355 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007356 break;
7357
7358 default:
7359 /* unsupported! */
7360 WARN_ON_ONCE(1);
7361 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007362 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007363 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007364 if (xprt) {
7365 calldata->xprt = xprt;
7366 task_setup_data.rpc_xprt = xprt;
7367 task_setup_data.flags =
7368 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7369 calldata->args.verifier = &clp->cl_confirm;
7370 } else {
7371 calldata->args.verifier = &verifier;
7372 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007373 calldata->args.client = clp;
7374#ifdef CONFIG_NFS_V4_1_MIGRATION
7375 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7376 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7377 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7378#else
7379 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7380 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7381#endif
7382 msg.rpc_argp = &calldata->args;
7383 msg.rpc_resp = &calldata->res;
7384 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007385
Andy Adamson8d89bd72016-09-09 09:22:18 -04007386 task = rpc_run_task(&task_setup_data);
7387 if (IS_ERR(task)) {
7388 status = PTR_ERR(task);
7389 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007390 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007391
Andy Adamsonad0849a2016-09-09 09:22:28 -04007392 if (!xprt) {
7393 status = rpc_wait_for_completion_task(task);
7394 if (!status)
7395 status = calldata->rpc_status;
7396 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007397 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007398
Andy Adamson8d89bd72016-09-09 09:22:18 -04007399 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007400out:
Chuck Lever177313f2012-05-21 22:44:58 -04007401 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007402 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007403 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007404 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007405 clp->cl_implid->date.seconds,
7406 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007407 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007408 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007409
7410out_impl_id:
7411 kfree(calldata->res.impl_id);
7412out_server_scope:
7413 kfree(calldata->res.server_scope);
7414out_server_owner:
7415 kfree(calldata->res.server_owner);
7416out_calldata:
7417 kfree(calldata);
7418 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007419}
7420
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007421/*
7422 * nfs4_proc_exchange_id()
7423 *
7424 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7425 *
7426 * Since the clientid has expired, all compounds using sessions
7427 * associated with the stale clientid will be returning
7428 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7429 * be in some phase of session reset.
7430 *
7431 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7432 */
7433int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7434{
7435 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7436 int status;
7437
7438 /* try SP4_MACH_CRED if krb5i/p */
7439 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7440 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007441 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007442 if (!status)
7443 return 0;
7444 }
7445
7446 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007447 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007448}
7449
Andy Adamson04fa2c62016-09-09 09:22:29 -04007450/**
7451 * nfs4_test_session_trunk
7452 *
7453 * This is an add_xprt_test() test function called from
7454 * rpc_clnt_setup_test_and_add_xprt.
7455 *
7456 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7457 * and is dereferrenced in nfs4_exchange_id_release
7458 *
7459 * Upon success, add the new transport to the rpc_clnt
7460 *
7461 * @clnt: struct rpc_clnt to get new transport
7462 * @xprt: the rpc_xprt to test
7463 * @data: call data for _nfs4_proc_exchange_id.
7464 */
7465int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7466 void *data)
7467{
7468 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7469 u32 sp4_how;
7470
7471 dprintk("--> %s try %s\n", __func__,
7472 xprt->address_strings[RPC_DISPLAY_ADDR]);
7473
7474 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7475
7476 /* Test connection for session trunking. Async exchange_id call */
7477 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7478}
7479EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7480
Trond Myklebust66245532012-05-25 17:18:09 -04007481static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7482 struct rpc_cred *cred)
7483{
7484 struct rpc_message msg = {
7485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7486 .rpc_argp = clp,
7487 .rpc_cred = cred,
7488 };
7489 int status;
7490
7491 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007492 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007493 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007494 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007495 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7496 return status;
7497}
7498
7499static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7500 struct rpc_cred *cred)
7501{
7502 unsigned int loop;
7503 int ret;
7504
7505 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7506 ret = _nfs4_proc_destroy_clientid(clp, cred);
7507 switch (ret) {
7508 case -NFS4ERR_DELAY:
7509 case -NFS4ERR_CLIENTID_BUSY:
7510 ssleep(1);
7511 break;
7512 default:
7513 return ret;
7514 }
7515 }
7516 return 0;
7517}
7518
7519int nfs4_destroy_clientid(struct nfs_client *clp)
7520{
7521 struct rpc_cred *cred;
7522 int ret = 0;
7523
7524 if (clp->cl_mvops->minor_version < 1)
7525 goto out;
7526 if (clp->cl_exchange_flags == 0)
7527 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007528 if (clp->cl_preserve_clid)
7529 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007530 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007531 ret = nfs4_proc_destroy_clientid(clp, cred);
7532 if (cred)
7533 put_rpccred(cred);
7534 switch (ret) {
7535 case 0:
7536 case -NFS4ERR_STALE_CLIENTID:
7537 clp->cl_exchange_flags = 0;
7538 }
7539out:
7540 return ret;
7541}
7542
Andy Adamson2050f0c2009-04-01 09:22:30 -04007543struct nfs4_get_lease_time_data {
7544 struct nfs4_get_lease_time_args *args;
7545 struct nfs4_get_lease_time_res *res;
7546 struct nfs_client *clp;
7547};
7548
7549static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7550 void *calldata)
7551{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007552 struct nfs4_get_lease_time_data *data =
7553 (struct nfs4_get_lease_time_data *)calldata;
7554
7555 dprintk("--> %s\n", __func__);
7556 /* just setup sequence, do not trigger session recovery
7557 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007558 nfs41_setup_sequence(data->clp->cl_session,
7559 &data->args->la_seq_args,
7560 &data->res->lr_seq_res,
7561 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007562 dprintk("<-- %s\n", __func__);
7563}
7564
7565/*
7566 * Called from nfs4_state_manager thread for session setup, so don't recover
7567 * from sequence operation or clientid errors.
7568 */
7569static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7570{
7571 struct nfs4_get_lease_time_data *data =
7572 (struct nfs4_get_lease_time_data *)calldata;
7573
7574 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007575 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7576 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007577 switch (task->tk_status) {
7578 case -NFS4ERR_DELAY:
7579 case -NFS4ERR_GRACE:
7580 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7581 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7582 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007583 /* fall through */
7584 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007585 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007586 return;
7587 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007588 dprintk("<-- %s\n", __func__);
7589}
7590
Trond Myklebust17280172012-03-11 13:11:00 -04007591static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007592 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7593 .rpc_call_done = nfs4_get_lease_time_done,
7594};
7595
7596int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7597{
7598 struct rpc_task *task;
7599 struct nfs4_get_lease_time_args args;
7600 struct nfs4_get_lease_time_res res = {
7601 .lr_fsinfo = fsinfo,
7602 };
7603 struct nfs4_get_lease_time_data data = {
7604 .args = &args,
7605 .res = &res,
7606 .clp = clp,
7607 };
7608 struct rpc_message msg = {
7609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7610 .rpc_argp = &args,
7611 .rpc_resp = &res,
7612 };
7613 struct rpc_task_setup task_setup = {
7614 .rpc_client = clp->cl_rpcclient,
7615 .rpc_message = &msg,
7616 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007617 .callback_data = &data,
7618 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007619 };
7620 int status;
7621
Chuck Levera9c92d62013-08-09 12:48:18 -04007622 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007623 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007624 dprintk("--> %s\n", __func__);
7625 task = rpc_run_task(&task_setup);
7626
7627 if (IS_ERR(task))
7628 status = PTR_ERR(task);
7629 else {
7630 status = task->tk_status;
7631 rpc_put_task(task);
7632 }
7633 dprintk("<-- %s return %d\n", __func__, status);
7634
7635 return status;
7636}
7637
Andy Adamsonfc931582009-04-01 09:22:31 -04007638/*
7639 * Initialize the values to be used by the client in CREATE_SESSION
7640 * If nfs4_init_session set the fore channel request and response sizes,
7641 * use them.
7642 *
7643 * Set the back channel max_resp_sz_cached to zero to force the client to
7644 * always set csa_cachethis to FALSE because the current implementation
7645 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7646 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007647static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7648 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007649{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007650 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007651 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007652
Andy Adamson18aad3d2013-06-26 12:21:49 -04007653 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7654 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7655
Andy Adamsonfc931582009-04-01 09:22:31 -04007656 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007657 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7658 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007659 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007660 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007661
7662 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007663 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007664 __func__,
7665 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007666 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007667
7668 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007669 args->bc_attrs.max_rqst_sz = max_bc_payload;
7670 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007671 args->bc_attrs.max_resp_sz_cached = 0;
7672 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007673 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007674
7675 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7676 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7677 __func__,
7678 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7679 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7680 args->bc_attrs.max_reqs);
7681}
7682
Trond Myklebust79969dd2015-02-18 11:30:18 -08007683static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7684 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007685{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007686 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007687 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007688
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007689 if (rcvd->max_resp_sz > sent->max_resp_sz)
7690 return -EINVAL;
7691 /*
7692 * Our requested max_ops is the minimum we need; we're not
7693 * prepared to break up compounds into smaller pieces than that.
7694 * So, no point even trying to continue if the server won't
7695 * cooperate:
7696 */
7697 if (rcvd->max_ops < sent->max_ops)
7698 return -EINVAL;
7699 if (rcvd->max_reqs == 0)
7700 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007701 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7702 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007703 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007704}
7705
Trond Myklebust79969dd2015-02-18 11:30:18 -08007706static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7707 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007708{
7709 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007710 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007711
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007712 if (!(res->flags & SESSION4_BACK_CHAN))
7713 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007714 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7715 return -EINVAL;
7716 if (rcvd->max_resp_sz < sent->max_resp_sz)
7717 return -EINVAL;
7718 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7719 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007720 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007721 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007722 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007723 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007724out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007725 return 0;
7726}
Andy Adamson8d353012009-04-01 09:22:32 -04007727
Andy Adamson8d353012009-04-01 09:22:32 -04007728static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007729 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007730{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007731 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007732
Trond Myklebust79969dd2015-02-18 11:30:18 -08007733 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007734 if (ret)
7735 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007736 return nfs4_verify_back_channel_attrs(args, res);
7737}
7738
7739static void nfs4_update_session(struct nfs4_session *session,
7740 struct nfs41_create_session_res *res)
7741{
7742 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007743 /* Mark client id and session as being confirmed */
7744 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7745 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007746 session->flags = res->flags;
7747 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007748 if (res->flags & SESSION4_BACK_CHAN)
7749 memcpy(&session->bc_attrs, &res->bc_attrs,
7750 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007751}
7752
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007753static int _nfs4_proc_create_session(struct nfs_client *clp,
7754 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007755{
7756 struct nfs4_session *session = clp->cl_session;
7757 struct nfs41_create_session_args args = {
7758 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007759 .clientid = clp->cl_clientid,
7760 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007761 .cb_program = NFS4_CALLBACK,
7762 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007763 struct nfs41_create_session_res res;
7764
Andy Adamsonfc931582009-04-01 09:22:31 -04007765 struct rpc_message msg = {
7766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7767 .rpc_argp = &args,
7768 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007769 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007770 };
7771 int status;
7772
Chuck Lever6b26cc82016-05-02 14:40:40 -04007773 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007774 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007775
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007776 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007777 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007778
Trond Myklebustb519d402016-09-11 14:50:01 -04007779 switch (status) {
7780 case -NFS4ERR_STALE_CLIENTID:
7781 case -NFS4ERR_DELAY:
7782 case -ETIMEDOUT:
7783 case -EACCES:
7784 case -EAGAIN:
7785 goto out;
7786 };
7787
7788 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007789 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007790 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007791 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007792 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007793 if (status)
7794 goto out;
7795 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007796 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007797out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007798 return status;
7799}
7800
7801/*
7802 * Issues a CREATE_SESSION operation to the server.
7803 * It is the responsibility of the caller to verify the session is
7804 * expired before calling this routine.
7805 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007806int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007807{
7808 int status;
7809 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007810 struct nfs4_session *session = clp->cl_session;
7811
7812 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7813
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007814 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007815 if (status)
7816 goto out;
7817
Andy Adamsonaacd5532011-11-09 13:58:21 -05007818 /* Init or reset the session slot tables */
7819 status = nfs4_setup_session_slot_tables(session);
7820 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007821 if (status)
7822 goto out;
7823
7824 ptr = (unsigned *)&session->sess_id.data[0];
7825 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7826 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007827out:
7828 dprintk("<-- %s\n", __func__);
7829 return status;
7830}
7831
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007832/*
7833 * Issue the over-the-wire RPC DESTROY_SESSION.
7834 * The caller must serialize access to this routine.
7835 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007836int nfs4_proc_destroy_session(struct nfs4_session *session,
7837 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007838{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007839 struct rpc_message msg = {
7840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7841 .rpc_argp = session,
7842 .rpc_cred = cred,
7843 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007844 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007845
7846 dprintk("--> nfs4_proc_destroy_session\n");
7847
7848 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007849 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7850 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007851
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007852 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007853 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007854
7855 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007856 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007857 "Session has been destroyed regardless...\n", status);
7858
7859 dprintk("<-- nfs4_proc_destroy_session\n");
7860 return status;
7861}
7862
Trond Myklebust7b38c362012-05-23 13:23:31 -04007863/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007864 * Renew the cl_session lease.
7865 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007866struct nfs4_sequence_data {
7867 struct nfs_client *clp;
7868 struct nfs4_sequence_args args;
7869 struct nfs4_sequence_res res;
7870};
7871
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007872static void nfs41_sequence_release(void *data)
7873{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007874 struct nfs4_sequence_data *calldata = data;
7875 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007876
Alexandros Batsakis71358402010-02-05 03:45:05 -08007877 if (atomic_read(&clp->cl_count) > 1)
7878 nfs4_schedule_state_renewal(clp);
7879 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007880 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007881}
7882
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007883static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7884{
7885 switch(task->tk_status) {
7886 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007887 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7888 return -EAGAIN;
7889 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007890 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007891 }
7892 return 0;
7893}
7894
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007895static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007896{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007897 struct nfs4_sequence_data *calldata = data;
7898 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007899
Trond Myklebust14516c32010-07-31 14:29:06 -04007900 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7901 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007902
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007903 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007904 if (task->tk_status < 0) {
7905 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007906 if (atomic_read(&clp->cl_count) == 1)
7907 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007908
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007909 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7910 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007911 return;
7912 }
7913 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007914 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007915out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007916 dprintk("<-- %s\n", __func__);
7917}
7918
7919static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7920{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007921 struct nfs4_sequence_data *calldata = data;
7922 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007923 struct nfs4_sequence_args *args;
7924 struct nfs4_sequence_res *res;
7925
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007926 args = task->tk_msg.rpc_argp;
7927 res = task->tk_msg.rpc_resp;
7928
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007929 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007930}
7931
7932static const struct rpc_call_ops nfs41_sequence_ops = {
7933 .rpc_call_done = nfs41_sequence_call_done,
7934 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007935 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007936};
7937
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007938static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7939 struct rpc_cred *cred,
7940 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007941{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007942 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007943 struct rpc_message msg = {
7944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7945 .rpc_cred = cred,
7946 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007947 struct rpc_task_setup task_setup_data = {
7948 .rpc_client = clp->cl_rpcclient,
7949 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007950 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007951 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007952 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007953
Alexandros Batsakis71358402010-02-05 03:45:05 -08007954 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007955 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007956 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007957 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007958 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007959 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007960 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007961 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007962 if (is_privileged)
7963 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007964 msg.rpc_argp = &calldata->args;
7965 msg.rpc_resp = &calldata->res;
7966 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007967 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007968
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007969 return rpc_run_task(&task_setup_data);
7970}
7971
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007972static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007973{
7974 struct rpc_task *task;
7975 int ret = 0;
7976
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007977 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007978 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007979 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007980 if (IS_ERR(task))
7981 ret = PTR_ERR(task);
7982 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007983 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007984 dprintk("<-- %s status=%d\n", __func__, ret);
7985 return ret;
7986}
7987
7988static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7989{
7990 struct rpc_task *task;
7991 int ret;
7992
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007993 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007994 if (IS_ERR(task)) {
7995 ret = PTR_ERR(task);
7996 goto out;
7997 }
7998 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007999 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008000 ret = task->tk_status;
8001 rpc_put_task(task);
8002out:
8003 dprintk("<-- %s status=%d\n", __func__, ret);
8004 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008005}
8006
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008007struct nfs4_reclaim_complete_data {
8008 struct nfs_client *clp;
8009 struct nfs41_reclaim_complete_args arg;
8010 struct nfs41_reclaim_complete_res res;
8011};
8012
8013static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8014{
8015 struct nfs4_reclaim_complete_data *calldata = data;
8016
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008017 nfs41_setup_sequence(calldata->clp->cl_session,
8018 &calldata->arg.seq_args,
8019 &calldata->res.seq_res,
8020 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008021}
8022
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008023static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8024{
8025 switch(task->tk_status) {
8026 case 0:
8027 case -NFS4ERR_COMPLETE_ALREADY:
8028 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8029 break;
8030 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008031 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008032 /* fall through */
8033 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008034 return -EAGAIN;
8035 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008036 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008037 }
8038 return 0;
8039}
8040
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008041static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8042{
8043 struct nfs4_reclaim_complete_data *calldata = data;
8044 struct nfs_client *clp = calldata->clp;
8045 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8046
8047 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008048 if (!nfs41_sequence_done(task, res))
8049 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008050
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008051 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008052 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8053 rpc_restart_call_prepare(task);
8054 return;
8055 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008056 dprintk("<-- %s\n", __func__);
8057}
8058
8059static void nfs4_free_reclaim_complete_data(void *data)
8060{
8061 struct nfs4_reclaim_complete_data *calldata = data;
8062
8063 kfree(calldata);
8064}
8065
8066static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8067 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8068 .rpc_call_done = nfs4_reclaim_complete_done,
8069 .rpc_release = nfs4_free_reclaim_complete_data,
8070};
8071
8072/*
8073 * Issue a global reclaim complete.
8074 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008075static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8076 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008077{
8078 struct nfs4_reclaim_complete_data *calldata;
8079 struct rpc_task *task;
8080 struct rpc_message msg = {
8081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008082 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008083 };
8084 struct rpc_task_setup task_setup_data = {
8085 .rpc_client = clp->cl_rpcclient,
8086 .rpc_message = &msg,
8087 .callback_ops = &nfs4_reclaim_complete_call_ops,
8088 .flags = RPC_TASK_ASYNC,
8089 };
8090 int status = -ENOMEM;
8091
8092 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008093 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008094 if (calldata == NULL)
8095 goto out;
8096 calldata->clp = clp;
8097 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008098
Chuck Levera9c92d62013-08-09 12:48:18 -04008099 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008100 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008101 msg.rpc_argp = &calldata->arg;
8102 msg.rpc_resp = &calldata->res;
8103 task_setup_data.callback_data = calldata;
8104 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008105 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008106 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008107 goto out;
8108 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008109 status = nfs4_wait_for_completion_rpc_task(task);
8110 if (status == 0)
8111 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008112 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008113 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008114out:
8115 dprintk("<-- %s status=%d\n", __func__, status);
8116 return status;
8117}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008118
8119static void
8120nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8121{
8122 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008123 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008124 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008125
8126 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008127 nfs41_setup_sequence(session, &lgp->args.seq_args,
8128 &lgp->res.seq_res, task);
8129 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008130}
8131
8132static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8133{
8134 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008135
8136 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008137 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008138 dprintk("<-- %s\n", __func__);
8139}
8140
8141static int
8142nfs4_layoutget_handle_exception(struct rpc_task *task,
8143 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8144{
Trond Myklebustee314c22012-10-01 17:25:48 -07008145 struct inode *inode = lgp->args.inode;
8146 struct nfs_server *server = NFS_SERVER(inode);
8147 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008148 int nfs4err = task->tk_status;
8149 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008150 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008151
Boaz Harroshed7e5422014-01-22 20:34:54 +02008152 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008153
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008154 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008155 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008156 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008157
8158 /*
8159 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8160 * on the file. set tk_status to -ENODATA to tell upper layer to
8161 * retry go inband.
8162 */
8163 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008164 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008165 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008166 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008167 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8168 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8169 */
8170 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008171 status = -EOVERFLOW;
8172 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008173 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008174 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008175 * (or clients) writing to the same RAID stripe except when
8176 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008177 *
8178 * Treat it like we would RECALLCONFLICT -- we retry for a little
8179 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008180 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008181 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008182 if (lgp->args.minlength == 0) {
8183 status = -EOVERFLOW;
8184 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008185 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008186 status = -EBUSY;
8187 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008188 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008189 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008190 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07008191 case -NFS4ERR_EXPIRED:
8192 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008193 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008194 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008195 lo = NFS_I(inode)->layout;
8196 /* If the open stateid was bad, then recover it. */
8197 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8198 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008199 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008200 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008201 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04008202 break;
8203 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008204
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008205 /*
8206 * Mark the bad layout state as invalid, then retry
8207 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008208 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008209 spin_unlock(&inode->i_lock);
8210 pnfs_free_lseg_list(&head);
8211 status = -EAGAIN;
8212 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008213 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008214
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008215 err = nfs4_handle_exception(server, nfs4err, exception);
8216 if (!status) {
8217 if (exception->retry)
8218 status = -EAGAIN;
8219 else
8220 status = err;
8221 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008222out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008223 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008224 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008225}
8226
Idan Kedar85541162012-08-02 11:47:10 +03008227static size_t max_response_pages(struct nfs_server *server)
8228{
8229 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8230 return nfs_page_array_len(0, max_resp_sz);
8231}
8232
8233static void nfs4_free_pages(struct page **pages, size_t size)
8234{
8235 int i;
8236
8237 if (!pages)
8238 return;
8239
8240 for (i = 0; i < size; i++) {
8241 if (!pages[i])
8242 break;
8243 __free_page(pages[i]);
8244 }
8245 kfree(pages);
8246}
8247
8248static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8249{
8250 struct page **pages;
8251 int i;
8252
8253 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8254 if (!pages) {
8255 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8256 return NULL;
8257 }
8258
8259 for (i = 0; i < size; i++) {
8260 pages[i] = alloc_page(gfp_flags);
8261 if (!pages[i]) {
8262 dprintk("%s: failed to allocate page\n", __func__);
8263 nfs4_free_pages(pages, size);
8264 return NULL;
8265 }
8266 }
8267
8268 return pages;
8269}
8270
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008271static void nfs4_layoutget_release(void *calldata)
8272{
8273 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008274 struct inode *inode = lgp->args.inode;
8275 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008276 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008277
8278 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008279 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008280 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008281 put_nfs_open_context(lgp->args.ctx);
8282 kfree(calldata);
8283 dprintk("<-- %s\n", __func__);
8284}
8285
8286static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8287 .rpc_call_prepare = nfs4_layoutget_prepare,
8288 .rpc_call_done = nfs4_layoutget_done,
8289 .rpc_release = nfs4_layoutget_release,
8290};
8291
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008292struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008293nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008294{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008295 struct inode *inode = lgp->args.inode;
8296 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008297 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008298 struct rpc_task *task;
8299 struct rpc_message msg = {
8300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8301 .rpc_argp = &lgp->args,
8302 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008303 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008304 };
8305 struct rpc_task_setup task_setup_data = {
8306 .rpc_client = server->client,
8307 .rpc_message = &msg,
8308 .callback_ops = &nfs4_layoutget_call_ops,
8309 .callback_data = lgp,
8310 .flags = RPC_TASK_ASYNC,
8311 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008312 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008313 struct nfs4_exception exception = {
8314 .inode = inode,
8315 .timeout = *timeout,
8316 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008317 int status = 0;
8318
8319 dprintk("--> %s\n", __func__);
8320
Peng Tao4bd5a982014-11-17 11:05:17 +08008321 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8322 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8323
Idan Kedar85541162012-08-02 11:47:10 +03008324 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8325 if (!lgp->args.layout.pages) {
8326 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008327 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008328 }
8329 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8330
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008331 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008332 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008333 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008334
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008335 task = rpc_run_task(&task_setup_data);
8336 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008337 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008338 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008339 if (status == 0) {
8340 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8341 *timeout = exception.timeout;
8342 }
8343
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008344 trace_nfs4_layoutget(lgp->args.ctx,
8345 &lgp->args.range,
8346 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008347 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008348 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008349
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008350 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8351 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008352 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008353 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008354 rpc_put_task(task);
8355 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008356 if (status)
8357 return ERR_PTR(status);
8358 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008359}
8360
Benny Halevycbe82602011-05-22 19:52:37 +03008361static void
8362nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8363{
8364 struct nfs4_layoutreturn *lrp = calldata;
8365
8366 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008367 nfs41_setup_sequence(lrp->clp->cl_session,
8368 &lrp->args.seq_args,
8369 &lrp->res.seq_res,
8370 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008371}
8372
8373static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8374{
8375 struct nfs4_layoutreturn *lrp = calldata;
8376 struct nfs_server *server;
8377
8378 dprintk("--> %s\n", __func__);
8379
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008380 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008381 return;
8382
8383 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008384 switch (task->tk_status) {
8385 default:
8386 task->tk_status = 0;
8387 case 0:
8388 break;
8389 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008390 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008391 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008392 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008393 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008394 return;
8395 }
Benny Halevycbe82602011-05-22 19:52:37 +03008396 dprintk("<-- %s\n", __func__);
8397}
8398
8399static void nfs4_layoutreturn_release(void *calldata)
8400{
8401 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008402 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008403 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008404
8405 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008406 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008407 if (lrp->res.lrs_present) {
8408 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8409 &lrp->args.range,
8410 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008411 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008412 } else
8413 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008414 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008415 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008416 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008417 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008418 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008419 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008420 kfree(calldata);
8421 dprintk("<-- %s\n", __func__);
8422}
8423
8424static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8425 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8426 .rpc_call_done = nfs4_layoutreturn_done,
8427 .rpc_release = nfs4_layoutreturn_release,
8428};
8429
Peng Tao6c166052014-11-17 09:30:40 +08008430int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008431{
8432 struct rpc_task *task;
8433 struct rpc_message msg = {
8434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8435 .rpc_argp = &lrp->args,
8436 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008437 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008438 };
8439 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008440 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008441 .rpc_message = &msg,
8442 .callback_ops = &nfs4_layoutreturn_call_ops,
8443 .callback_data = lrp,
8444 };
Peng Tao6c166052014-11-17 09:30:40 +08008445 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008446
Andrew Elble99ade3c2015-12-02 09:39:51 -05008447 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8448 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8449 &task_setup_data.rpc_client, &msg);
8450
Benny Halevycbe82602011-05-22 19:52:37 +03008451 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008452 if (!sync) {
8453 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8454 if (!lrp->inode) {
8455 nfs4_layoutreturn_release(lrp);
8456 return -EAGAIN;
8457 }
8458 task_setup_data.flags |= RPC_TASK_ASYNC;
8459 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008460 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008461 task = rpc_run_task(&task_setup_data);
8462 if (IS_ERR(task))
8463 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008464 if (sync)
8465 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008466 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008467 dprintk("<-- %s status=%d\n", __func__, status);
8468 rpc_put_task(task);
8469 return status;
8470}
8471
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008473_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8474 struct pnfs_device *pdev,
8475 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008476{
8477 struct nfs4_getdeviceinfo_args args = {
8478 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008479 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8480 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008481 };
8482 struct nfs4_getdeviceinfo_res res = {
8483 .pdev = pdev,
8484 };
8485 struct rpc_message msg = {
8486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8487 .rpc_argp = &args,
8488 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008489 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 };
8491 int status;
8492
8493 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008494 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008495 if (res.notification & ~args.notify_types)
8496 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008497 if (res.notification != args.notify_types)
8498 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008499
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008500 dprintk("<-- %s status=%d\n", __func__, status);
8501
8502 return status;
8503}
8504
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008505int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8506 struct pnfs_device *pdev,
8507 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008508{
8509 struct nfs4_exception exception = { };
8510 int err;
8511
8512 do {
8513 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008514 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008515 &exception);
8516 } while (exception.retry);
8517 return err;
8518}
8519EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8520
Andy Adamson863a3c62011-03-23 13:27:54 +00008521static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8522{
8523 struct nfs4_layoutcommit_data *data = calldata;
8524 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008525 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008526
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008527 nfs41_setup_sequence(session,
8528 &data->args.seq_args,
8529 &data->res.seq_res,
8530 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008531}
8532
8533static void
8534nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8535{
8536 struct nfs4_layoutcommit_data *data = calldata;
8537 struct nfs_server *server = NFS_SERVER(data->args.inode);
8538
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008539 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008540 return;
8541
8542 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008543 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8544 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8545 case -NFS4ERR_BADLAYOUT: /* no layout */
8546 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008547 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008548 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008549 break;
8550 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008551 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008552 rpc_restart_call_prepare(task);
8553 return;
8554 }
8555 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008556}
8557
8558static void nfs4_layoutcommit_release(void *calldata)
8559{
8560 struct nfs4_layoutcommit_data *data = calldata;
8561
Andy Adamsondb29c082011-07-30 20:52:38 -04008562 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008563 nfs_post_op_update_inode_force_wcc(data->args.inode,
8564 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008565 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008566 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008567 kfree(data);
8568}
8569
8570static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8571 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8572 .rpc_call_done = nfs4_layoutcommit_done,
8573 .rpc_release = nfs4_layoutcommit_release,
8574};
8575
8576int
Andy Adamsonef311532011-03-12 02:58:10 -05008577nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008578{
8579 struct rpc_message msg = {
8580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8581 .rpc_argp = &data->args,
8582 .rpc_resp = &data->res,
8583 .rpc_cred = data->cred,
8584 };
8585 struct rpc_task_setup task_setup_data = {
8586 .task = &data->task,
8587 .rpc_client = NFS_CLIENT(data->args.inode),
8588 .rpc_message = &msg,
8589 .callback_ops = &nfs4_layoutcommit_ops,
8590 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008591 };
8592 struct rpc_task *task;
8593 int status = 0;
8594
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008595 dprintk("NFS: initiating layoutcommit call. sync %d "
8596 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008597 data->args.lastbytewritten,
8598 data->args.inode->i_ino);
8599
Trond Myklebust472e2592015-02-05 16:50:30 -05008600 if (!sync) {
8601 data->inode = nfs_igrab_and_active(data->args.inode);
8602 if (data->inode == NULL) {
8603 nfs4_layoutcommit_release(data);
8604 return -EAGAIN;
8605 }
8606 task_setup_data.flags = RPC_TASK_ASYNC;
8607 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008608 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008609 task = rpc_run_task(&task_setup_data);
8610 if (IS_ERR(task))
8611 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008612 if (sync)
8613 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008614 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008615 dprintk("%s: status %d\n", __func__, status);
8616 rpc_put_task(task);
8617 return status;
8618}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008619
Andy Adamson97431202013-08-08 10:57:56 -04008620/**
8621 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8622 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8623 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008624static int
8625_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008626 struct nfs_fsinfo *info,
8627 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008628{
8629 struct nfs41_secinfo_no_name_args args = {
8630 .style = SECINFO_STYLE_CURRENT_FH,
8631 };
8632 struct nfs4_secinfo_res res = {
8633 .flavors = flavors,
8634 };
8635 struct rpc_message msg = {
8636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8637 .rpc_argp = &args,
8638 .rpc_resp = &res,
8639 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008640 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008641 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008642 int status;
8643
8644 if (use_integrity) {
8645 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008646 cred = nfs4_get_clid_cred(server->nfs_client);
8647 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008648 }
8649
8650 dprintk("--> %s\n", __func__);
8651 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8652 &res.seq_res, 0);
8653 dprintk("<-- %s status=%d\n", __func__, status);
8654
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008655 if (cred)
8656 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008657
8658 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008659}
8660
8661static int
8662nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8663 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8664{
8665 struct nfs4_exception exception = { };
8666 int err;
8667 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008668 /* first try using integrity protection */
8669 err = -NFS4ERR_WRONGSEC;
8670
8671 /* try to use integrity protection with machine cred */
8672 if (_nfs4_is_integrity_protected(server->nfs_client))
8673 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8674 flavors, true);
8675
8676 /*
8677 * if unable to use integrity protection, or SECINFO with
8678 * integrity protection returns NFS4ERR_WRONGSEC (which is
8679 * disallowed by spec, but exists in deployed servers) use
8680 * the current filesystem's rpc_client and the user cred.
8681 */
8682 if (err == -NFS4ERR_WRONGSEC)
8683 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8684 flavors, false);
8685
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008686 switch (err) {
8687 case 0:
8688 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008689 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008690 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008691 default:
8692 err = nfs4_handle_exception(server, err, &exception);
8693 }
8694 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008695out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008696 return err;
8697}
8698
8699static int
8700nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8701 struct nfs_fsinfo *info)
8702{
8703 int err;
8704 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008705 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008706 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008707 struct nfs4_secinfo4 *secinfo;
8708 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008709
8710 page = alloc_page(GFP_KERNEL);
8711 if (!page) {
8712 err = -ENOMEM;
8713 goto out;
8714 }
8715
8716 flavors = page_address(page);
8717 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8718
8719 /*
8720 * Fall back on "guess and check" method if
8721 * the server doesn't support SECINFO_NO_NAME
8722 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008723 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008724 err = nfs4_find_root_sec(server, fhandle, info);
8725 goto out_freepage;
8726 }
8727 if (err)
8728 goto out_freepage;
8729
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008730 for (i = 0; i < flavors->num_flavors; i++) {
8731 secinfo = &flavors->flavors[i];
8732
8733 switch (secinfo->flavor) {
8734 case RPC_AUTH_NULL:
8735 case RPC_AUTH_UNIX:
8736 case RPC_AUTH_GSS:
8737 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8738 &secinfo->flavor_info);
8739 break;
8740 default:
8741 flavor = RPC_AUTH_MAXFLAVOR;
8742 break;
8743 }
8744
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008745 if (!nfs_auth_info_match(&server->auth_info, flavor))
8746 flavor = RPC_AUTH_MAXFLAVOR;
8747
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008748 if (flavor != RPC_AUTH_MAXFLAVOR) {
8749 err = nfs4_lookup_root_sec(server, fhandle,
8750 info, flavor);
8751 if (!err)
8752 break;
8753 }
8754 }
8755
8756 if (flavor == RPC_AUTH_MAXFLAVOR)
8757 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008758
8759out_freepage:
8760 put_page(page);
8761 if (err == -EACCES)
8762 return -EPERM;
8763out:
8764 return err;
8765}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008766
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008767static int _nfs41_test_stateid(struct nfs_server *server,
8768 nfs4_stateid *stateid,
8769 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008770{
8771 int status;
8772 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008773 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008774 };
8775 struct nfs41_test_stateid_res res;
8776 struct rpc_message msg = {
8777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8778 .rpc_argp = &args,
8779 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008780 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008781 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008782 struct rpc_clnt *rpc_client = server->client;
8783
8784 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8785 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008786
Chuck Lever38527b12012-07-11 16:30:23 -04008787 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008788 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008789 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008790 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008791 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008792 if (status != NFS_OK) {
8793 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008794 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008795 }
8796 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008797 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008798}
8799
Chuck Lever38527b12012-07-11 16:30:23 -04008800/**
8801 * nfs41_test_stateid - perform a TEST_STATEID operation
8802 *
8803 * @server: server / transport on which to perform the operation
8804 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008805 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008806 *
8807 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8808 * Otherwise a negative NFS4ERR value is returned if the operation
8809 * failed or the state ID is not currently valid.
8810 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008811static int nfs41_test_stateid(struct nfs_server *server,
8812 nfs4_stateid *stateid,
8813 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008814{
8815 struct nfs4_exception exception = { };
8816 int err;
8817 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008818 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008819 if (err != -NFS4ERR_DELAY)
8820 break;
8821 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008822 } while (exception.retry);
8823 return err;
8824}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008825
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008826struct nfs_free_stateid_data {
8827 struct nfs_server *server;
8828 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008829 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008830};
8831
8832static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8833{
8834 struct nfs_free_stateid_data *data = calldata;
8835 nfs41_setup_sequence(nfs4_get_session(data->server),
8836 &data->args.seq_args,
8837 &data->res.seq_res,
8838 task);
8839}
8840
8841static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8842{
8843 struct nfs_free_stateid_data *data = calldata;
8844
8845 nfs41_sequence_done(task, &data->res.seq_res);
8846
8847 switch (task->tk_status) {
8848 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008849 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008850 rpc_restart_call_prepare(task);
8851 }
8852}
8853
8854static void nfs41_free_stateid_release(void *calldata)
8855{
8856 kfree(calldata);
8857}
8858
Trond Myklebust17f26b12013-08-21 15:48:42 -04008859static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008860 .rpc_call_prepare = nfs41_free_stateid_prepare,
8861 .rpc_call_done = nfs41_free_stateid_done,
8862 .rpc_release = nfs41_free_stateid_release,
8863};
8864
8865static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8866 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008867 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008868 bool privileged)
8869{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008870 struct rpc_message msg = {
8871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008872 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008873 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008874 struct rpc_task_setup task_setup = {
8875 .rpc_client = server->client,
8876 .rpc_message = &msg,
8877 .callback_ops = &nfs41_free_stateid_ops,
8878 .flags = RPC_TASK_ASYNC,
8879 };
8880 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008881
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008882 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8883 &task_setup.rpc_client, &msg);
8884
Chuck Lever38527b12012-07-11 16:30:23 -04008885 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008886 data = kmalloc(sizeof(*data), GFP_NOFS);
8887 if (!data)
8888 return ERR_PTR(-ENOMEM);
8889 data->server = server;
8890 nfs4_stateid_copy(&data->args.stateid, stateid);
8891
8892 task_setup.callback_data = data;
8893
8894 msg.rpc_argp = &data->args;
8895 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008896 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008897 if (privileged)
8898 nfs4_set_sequence_privileged(&data->args.seq_args);
8899
8900 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008901}
8902
Chuck Lever38527b12012-07-11 16:30:23 -04008903/**
8904 * nfs41_free_stateid - perform a FREE_STATEID operation
8905 *
8906 * @server: server / transport on which to perform the operation
8907 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008908 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008909 *
8910 * Returns NFS_OK if the server freed "stateid". Otherwise a
8911 * negative NFS4ERR value is returned.
8912 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008913static int nfs41_free_stateid(struct nfs_server *server,
8914 nfs4_stateid *stateid,
8915 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008916{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008917 struct rpc_task *task;
8918 int ret;
8919
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008920 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008921 if (IS_ERR(task))
8922 return PTR_ERR(task);
8923 ret = rpc_wait_for_completion_task(task);
8924 if (!ret)
8925 ret = task->tk_status;
8926 rpc_put_task(task);
8927 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008928}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008929
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008930static void
8931nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008932{
8933 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008934 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008935
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008936 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008937 nfs4_free_lock_state(server, lsp);
8938 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008939 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008940 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008941}
8942
Trond Myklebust36281ca2012-03-04 18:13:56 -05008943static bool nfs41_match_stateid(const nfs4_stateid *s1,
8944 const nfs4_stateid *s2)
8945{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008946 if (s1->type != s2->type)
8947 return false;
8948
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008949 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008950 return false;
8951
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008952 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008953 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008954 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008955 return true;
8956
8957 return false;
8958}
8959
Andy Adamson557134a2009-04-01 09:21:53 -04008960#endif /* CONFIG_NFS_V4_1 */
8961
Trond Myklebust36281ca2012-03-04 18:13:56 -05008962static bool nfs4_match_stateid(const nfs4_stateid *s1,
8963 const nfs4_stateid *s2)
8964{
Trond Myklebustf597c532012-03-04 18:13:56 -05008965 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008966}
8967
8968
Trond Myklebust17280172012-03-11 13:11:00 -04008969static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008970 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008971 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 .recover_open = nfs4_open_reclaim,
8973 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008974 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008975 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976};
8977
Andy Adamson591d71c2009-04-01 09:22:47 -04008978#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008979static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008980 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8981 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8982 .recover_open = nfs4_open_reclaim,
8983 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008984 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008985 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008986 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008987};
8988#endif /* CONFIG_NFS_V4_1 */
8989
Trond Myklebust17280172012-03-11 13:11:00 -04008990static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008991 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008992 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008993 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008995 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996};
8997
Andy Adamson591d71c2009-04-01 09:22:47 -04008998#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008999static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009000 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9001 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009002 .recover_open = nfs41_open_expired,
9003 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009004 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009005};
9006#endif /* CONFIG_NFS_V4_1 */
9007
Trond Myklebust17280172012-03-11 13:11:00 -04009008static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009009 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009010 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009011 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009012};
9013
9014#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009015static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009016 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009017 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009018 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009019};
9020#endif
9021
Chuck Leverec011fe2013-10-17 14:12:39 -04009022static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009023 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009024 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009025};
9026
9027#if defined(CONFIG_NFS_V4_1)
9028static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009029 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009030 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009031};
9032#endif /* CONFIG_NFS_V4_1 */
9033
Trond Myklebust97dc1352010-06-16 09:52:26 -04009034static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9035 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009036 .init_caps = NFS_CAP_READDIRPLUS
9037 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009038 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009039 .init_client = nfs40_init_client,
9040 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009041 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009042 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009043 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009044 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009045 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009046 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9047 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9048 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009049 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009050};
9051
9052#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009053static struct nfs_seqid *
9054nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9055{
9056 return NULL;
9057}
9058
Trond Myklebust97dc1352010-06-16 09:52:26 -04009059static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9060 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009061 .init_caps = NFS_CAP_READDIRPLUS
9062 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009063 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009064 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009065 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009066 .init_client = nfs41_init_client,
9067 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009068 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009069 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009070 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009071 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009072 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009073 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009074 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9075 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9076 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009077 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009078};
9079#endif
9080
Steve Dickson42c2c422013-05-22 12:50:38 -04009081#if defined(CONFIG_NFS_V4_2)
9082static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9083 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009084 .init_caps = NFS_CAP_READDIRPLUS
9085 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009086 | NFS_CAP_POSIX_LOCK
9087 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009088 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009089 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009090 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009091 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009092 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009093 | NFS_CAP_LAYOUTSTATS
9094 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009095 .init_client = nfs41_init_client,
9096 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009097 .match_stateid = nfs41_match_stateid,
9098 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009099 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009100 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009101 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009102 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009103 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9104 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9105 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009106 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009107};
9108#endif
9109
Trond Myklebust97dc1352010-06-16 09:52:26 -04009110const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9111 [0] = &nfs_v4_0_minor_ops,
9112#if defined(CONFIG_NFS_V4_1)
9113 [1] = &nfs_v4_1_minor_ops,
9114#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009115#if defined(CONFIG_NFS_V4_2)
9116 [2] = &nfs_v4_2_minor_ops,
9117#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009118};
9119
Trond Myklebust13997822016-07-24 17:10:52 -04009120static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009121{
9122 ssize_t error, error2;
9123
9124 error = generic_listxattr(dentry, list, size);
9125 if (error < 0)
9126 return error;
9127 if (list) {
9128 list += error;
9129 size -= error;
9130 }
9131
9132 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9133 if (error2 < 0)
9134 return error2;
9135 return error + error2;
9136}
9137
Trond Myklebust17f26b12013-08-21 15:48:42 -04009138static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009139 .create = nfs_create,
9140 .lookup = nfs_lookup,
9141 .atomic_open = nfs_atomic_open,
9142 .link = nfs_link,
9143 .unlink = nfs_unlink,
9144 .symlink = nfs_symlink,
9145 .mkdir = nfs_mkdir,
9146 .rmdir = nfs_rmdir,
9147 .mknod = nfs_mknod,
9148 .rename = nfs_rename,
9149 .permission = nfs_permission,
9150 .getattr = nfs_getattr,
9151 .setattr = nfs_setattr,
9152 .getxattr = generic_getxattr,
9153 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009154 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009155 .removexattr = generic_removexattr,
9156};
9157
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009158static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009159 .permission = nfs_permission,
9160 .getattr = nfs_getattr,
9161 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009162 .getxattr = generic_getxattr,
9163 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009164 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009165 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009166};
9167
David Howells509de812006-08-22 20:06:11 -04009168const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009169 .version = 4, /* protocol version */
9170 .dentry_ops = &nfs4_dentry_operations,
9171 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009172 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009173 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009175 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009176 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177 .getattr = nfs4_proc_getattr,
9178 .setattr = nfs4_proc_setattr,
9179 .lookup = nfs4_proc_lookup,
9180 .access = nfs4_proc_access,
9181 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182 .create = nfs4_proc_create,
9183 .remove = nfs4_proc_remove,
9184 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009185 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009187 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009188 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009189 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 .link = nfs4_proc_link,
9191 .symlink = nfs4_proc_symlink,
9192 .mkdir = nfs4_proc_mkdir,
9193 .rmdir = nfs4_proc_remove,
9194 .readdir = nfs4_proc_readdir,
9195 .mknod = nfs4_proc_mknod,
9196 .statfs = nfs4_proc_statfs,
9197 .fsinfo = nfs4_proc_fsinfo,
9198 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009199 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009201 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009203 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009205 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009207 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009208 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009210 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009211 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009212 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009213 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009214 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009215 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009216 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009217 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009218 .create_server = nfs4_create_server,
9219 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220};
9221
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009222static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009223 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009224 .list = nfs4_xattr_list_nfs4_acl,
9225 .get = nfs4_xattr_get_nfs4_acl,
9226 .set = nfs4_xattr_set_nfs4_acl,
9227};
9228
9229const struct xattr_handler *nfs4_xattr_handlers[] = {
9230 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009231#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9232 &nfs4_xattr_nfs4_label_handler,
9233#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009234 NULL
9235};
9236
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237/*
9238 * Local variables:
9239 * c-basic-offset: 8
9240 * End:
9241 */