blob: de4a89d3d740fc332021223475563e3305fd3b95 [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
619 args->sa_slot = slot;
620 res->sr_slot = slot;
621
622out_start:
623 rpc_call_start(task);
624 return 0;
625
626out_sleep:
627 if (args->sa_privileged)
628 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
629 NULL, RPC_PRIORITY_PRIVILEGED);
630 else
631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
633 return -EAGAIN;
634}
Peng Taocb04ad22014-06-11 05:24:15 +0800635EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400636
637static int nfs40_sequence_done(struct rpc_task *task,
638 struct nfs4_sequence_res *res)
639{
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
642
Trond Myklebustcab92c12014-01-29 12:12:15 -0500643 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400644 goto out;
645
646 tbl = slot->table;
647 spin_lock(&tbl->slot_tbl_lock);
648 if (!nfs41_wake_and_assign_slot(tbl, slot))
649 nfs4_free_slot(tbl, slot);
650 spin_unlock(&tbl->slot_tbl_lock);
651
652 res->sr_slot = NULL;
653out:
654 return 1;
655}
656
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400657#if defined(CONFIG_NFS_V4_1)
658
Trond Myklebustd185a332010-06-16 09:52:25 -0400659static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400660{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500661 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400662 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500663 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500664 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400665
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500667 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500668
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400669 /* Bump the slot sequence number */
670 if (slot->seq_done)
671 slot->seq_nr++;
672 slot->seq_done = 0;
673
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500674 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500675 /* Be nice to the server: try to ensure that the last transmitted
676 * value for highest_user_slotid <= target_highest_slotid
677 */
678 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
679 send_new_highest_used_slotid = true;
680
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500681 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500682 send_new_highest_used_slotid = false;
683 goto out_unlock;
684 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500685 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500686
687 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
688 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500689out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500690 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400691 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500692 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400693 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400694 if (waitqueue_active(&tbl->slot_waitq))
695 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400696}
697
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500698int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400699{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500700 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500701 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400702 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500703 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500704 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400705
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500706 if (slot == NULL)
707 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400708 /* don't increment the sequence number if the task wasn't sent */
709 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400710 goto out;
711
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500712 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500713
Trond Myklebustac20d162012-12-15 15:36:07 -0500714 if (slot->interrupted) {
715 slot->interrupted = 0;
716 interrupted = true;
717 }
718
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400719 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500720 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 switch (res->sr_status) {
722 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400723 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400724 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500725 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500726 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500727 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400728 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500729 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400730 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500731 case 1:
732 /*
733 * sr_status remains 1 if an RPC level error occurred.
734 * The server may or may not have processed the sequence
735 * operation..
736 * Mark the slot as having hosted an interrupted RPC call.
737 */
738 slot->interrupted = 1;
739 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 case -NFS4ERR_DELAY:
741 /* The server detected a resend of the RPC call and
742 * returned NFS4ERR_DELAY as per Section 2.10.6.2
743 * of RFC5661.
744 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400746 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500747 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500748 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400749 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500750 case -NFS4ERR_BADSLOT:
751 /*
752 * The slot id we used was probably retired. Try again
753 * using a different slot id.
754 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500755 goto retry_nowait;
756 case -NFS4ERR_SEQ_MISORDERED:
757 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500758 * Was the last operation on this sequence interrupted?
759 * If so, retry after bumping the sequence number.
760 */
761 if (interrupted) {
762 ++slot->seq_nr;
763 goto retry_nowait;
764 }
765 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500766 * Could this slot have been previously retired?
767 * If so, then the server may be expecting seq_nr = 1!
768 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500769 if (slot->seq_nr != 1) {
770 slot->seq_nr = 1;
771 goto retry_nowait;
772 }
773 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500774 case -NFS4ERR_SEQ_FALSE_RETRY:
775 ++slot->seq_nr;
776 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 default:
778 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400779 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400780 }
781out:
782 /* The session may be reset by one of the error handlers. */
783 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400784 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500785out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500786 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500787retry_nowait:
788 if (rpc_restart_call_prepare(task)) {
789 task->tk_status = 0;
790 ret = 0;
791 }
792 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400793out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400794 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 goto out;
796 rpc_delay(task, NFS4_POLL_RETRY_MAX);
797 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400798}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500799EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400800
Peng Tao2c4b1312014-06-11 05:24:16 +0800801int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400802{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500803 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400804 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400805 if (!res->sr_slot->table->session)
806 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400807 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400808}
Peng Tao2c4b1312014-06-11 05:24:16 +0800809EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400810
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000811int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400812 struct nfs4_sequence_args *args,
813 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400814 struct rpc_task *task)
815{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400816 struct nfs4_slot *slot;
817 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400818
819 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400820 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400821 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400822 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400823
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400824 tbl = &session->fc_slot_table;
825
Trond Myklebust69d206b2012-11-22 13:21:02 -0500826 task->tk_timeout = 0;
827
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400828 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400829 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400830 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500831 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500832 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400833 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400834 }
835
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500836 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500837 if (IS_ERR(slot)) {
838 /* If out of memory, try again in 1/4 second */
839 if (slot == ERR_PTR(-ENOMEM))
840 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400841 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400842 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400843 }
844 spin_unlock(&tbl->slot_tbl_lock);
845
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500846 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400847
Chuck Levere8d92382013-08-09 12:47:51 -0400848 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500849 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400850
Benny Halevydfb4f3092010-09-24 09:17:01 -0400851 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500852 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400853 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400854 /*
855 * sr_status is only set in decode_sequence, and so will remain
856 * set to 1 if an rpc level failure occurs.
857 */
858 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400859 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400860out_success:
861 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400862 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400863out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400864 /* Privileged tasks are queued with top priority */
865 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400866 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
867 NULL, RPC_PRIORITY_PRIVILEGED);
868 else
869 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400870 spin_unlock(&tbl->slot_tbl_lock);
871 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400872}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000873EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400874
Chuck Lever5a580e02013-08-09 12:48:09 -0400875static int nfs4_setup_sequence(const struct nfs_server *server,
876 struct nfs4_sequence_args *args,
877 struct nfs4_sequence_res *res,
878 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400879{
Trond Myklebust035168a2010-06-16 09:52:26 -0400880 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400881 int ret = 0;
882
Chuck Lever3bd23842013-08-09 12:49:19 -0400883 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800884 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
885 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400886
Chuck Levere8d92382013-08-09 12:47:51 -0400887 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400888 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400889 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400890
Trond Myklebust9d12b212012-01-17 22:04:25 -0500891 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400892
Andy Adamsonce5039c2009-04-01 09:22:13 -0400893 dprintk("<-- %s status=%d\n", __func__, ret);
894 return ret;
895}
896
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
898{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400899 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400900 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400901
Trond Myklebust035168a2010-06-16 09:52:26 -0400902 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
903
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400904 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400905}
906
Andy Adamson69ab40c2009-04-01 09:22:19 -0400907static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
908{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400909 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400910
Trond Myklebust14516c32010-07-31 14:29:06 -0400911 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400912}
913
Trond Myklebust17280172012-03-11 13:11:00 -0400914static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400915 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400916 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400917};
918
Chuck Lever3bd23842013-08-09 12:49:19 -0400919#else /* !CONFIG_NFS_V4_1 */
920
Chuck Lever5a580e02013-08-09 12:48:09 -0400921static int nfs4_setup_sequence(const struct nfs_server *server,
922 struct nfs4_sequence_args *args,
923 struct nfs4_sequence_res *res,
924 struct rpc_task *task)
925{
Peng Taocb04ad22014-06-11 05:24:15 +0800926 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
927 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400928}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400929
Peng Tao2c4b1312014-06-11 05:24:16 +0800930int nfs4_sequence_done(struct rpc_task *task,
931 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400932{
Chuck Lever3bd23842013-08-09 12:49:19 -0400933 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400934}
Peng Tao2c4b1312014-06-11 05:24:16 +0800935EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400936
937#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400938
Chuck Lever9915ea72013-08-09 12:48:27 -0400939static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
940{
941 struct nfs4_call_sync_data *data = calldata;
942 nfs4_setup_sequence(data->seq_server,
943 data->seq_args, data->seq_res, task);
944}
945
946static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
947{
948 struct nfs4_call_sync_data *data = calldata;
949 nfs4_sequence_done(task, data->seq_res);
950}
951
952static const struct rpc_call_ops nfs40_call_sync_ops = {
953 .rpc_call_prepare = nfs40_call_sync_prepare,
954 .rpc_call_done = nfs40_call_sync_done,
955};
956
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400957static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400958 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 struct nfs4_sequence_args *args,
961 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400962{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100963 int ret;
964 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400965 struct nfs_client *clp = server->nfs_client;
966 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967 .seq_server = server,
968 .seq_args = args,
969 .seq_res = res,
970 };
971 struct rpc_task_setup task_setup = {
972 .rpc_client = clnt,
973 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400974 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100975 .callback_data = &data
976 };
977
978 task = rpc_run_task(&task_setup);
979 if (IS_ERR(task))
980 ret = PTR_ERR(task);
981 else {
982 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400983 rpc_put_task(task);
984 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100985 return ret;
986}
987
Bryan Schumaker7c513052011-03-24 17:12:24 +0000988int nfs4_call_sync(struct rpc_clnt *clnt,
989 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000990 struct rpc_message *msg,
991 struct nfs4_sequence_args *args,
992 struct nfs4_sequence_res *res,
993 int cache_reply)
994{
Chuck Levera9c92d62013-08-09 12:48:18 -0400995 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400996 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000997}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1000{
1001 struct nfs_inode *nfsi = NFS_I(dir);
1002
1003 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001004 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001005 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001007 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001008 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001009 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 spin_unlock(&dir->i_lock);
1011}
1012
1013struct nfs4_opendata {
1014 struct kref kref;
1015 struct nfs_openargs o_arg;
1016 struct nfs_openres o_res;
1017 struct nfs_open_confirmargs c_arg;
1018 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001019 struct nfs4_string owner_name;
1020 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001021 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001023 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001025 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 struct nfs4_state_owner *owner;
1027 struct nfs4_state *state;
1028 struct iattr attrs;
1029 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001030 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001031 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001032 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001033 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int cancelled;
1035};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001036
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001037static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1038 int err, struct nfs4_exception *exception)
1039{
1040 if (err != -EINVAL)
1041 return false;
1042 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1043 return false;
1044 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1045 exception->retry = 1;
1046 return true;
1047}
1048
Trond Myklebust6ae37332015-01-30 14:21:14 -05001049static u32
1050nfs4_map_atomic_open_share(struct nfs_server *server,
1051 fmode_t fmode, int openflags)
1052{
1053 u32 res = 0;
1054
1055 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1056 case FMODE_READ:
1057 res = NFS4_SHARE_ACCESS_READ;
1058 break;
1059 case FMODE_WRITE:
1060 res = NFS4_SHARE_ACCESS_WRITE;
1061 break;
1062 case FMODE_READ|FMODE_WRITE:
1063 res = NFS4_SHARE_ACCESS_BOTH;
1064 }
1065 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1066 goto out;
1067 /* Want no delegation if we're using O_DIRECT */
1068 if (openflags & O_DIRECT)
1069 res |= NFS4_SHARE_WANT_NO_DELEG;
1070out:
1071 return res;
1072}
1073
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001074static enum open_claim_type4
1075nfs4_map_atomic_open_claim(struct nfs_server *server,
1076 enum open_claim_type4 claim)
1077{
1078 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1079 return claim;
1080 switch (claim) {
1081 default:
1082 return claim;
1083 case NFS4_OPEN_CLAIM_FH:
1084 return NFS4_OPEN_CLAIM_NULL;
1085 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1086 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1087 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1088 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1089 }
1090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001093{
1094 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001095 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001096 p->o_res.seqid = p->o_arg.seqid;
1097 p->c_res.seqid = p->c_arg.seqid;
1098 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001099 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001100 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001101 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001102}
1103
Al Viro82a2c1b2011-06-22 18:30:55 -04001104static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001105 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001106 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001107 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001108 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001109 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110{
Al Viro82a2c1b2011-06-22 18:30:55 -04001111 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001112 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001113 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001114 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 struct nfs4_opendata *p;
1116
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001117 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001118 if (p == NULL)
1119 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001120
1121 p->f_label = nfs4_label_alloc(server, gfp_mask);
1122 if (IS_ERR(p->f_label))
1123 goto err_free_p;
1124
Kinglong Meea49c2692015-07-27 15:31:38 +08001125 p->a_label = nfs4_label_alloc(server, gfp_mask);
1126 if (IS_ERR(p->a_label))
1127 goto err_free_f;
1128
Trond Myklebust63f5f792015-01-23 19:19:25 -05001129 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1130 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001131 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001132 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001133 nfs_sb_active(dentry->d_sb);
1134 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001135 p->dir = parent;
1136 p->owner = sp;
1137 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001138 p->o_arg.open_flags = flags;
1139 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001140 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1141 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001142 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1143 * will return permission denied for all bits until close */
1144 if (!(flags & O_EXCL)) {
1145 /* ask server to check for all possible rights as results
1146 * are cached */
1147 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1148 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001149 }
David Howells7539bba2006-08-22 20:06:09 -04001150 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001151 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1152 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001153 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001154 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001155 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001156 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001157 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001158 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1159 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001160 case NFS4_OPEN_CLAIM_NULL:
1161 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1162 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1163 p->o_arg.fh = NFS_FH(dir);
1164 break;
1165 case NFS4_OPEN_CLAIM_PREVIOUS:
1166 case NFS4_OPEN_CLAIM_FH:
1167 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1168 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001169 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001170 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001171 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001172 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001173
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 p->o_arg.u.attrs = &p->attrs;
1175 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001176
1177 verf[0] = jiffies;
1178 verf[1] = current->pid;
1179 memcpy(p->o_arg.u.verifier.data, verf,
1180 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001181 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001182 p->c_arg.fh = &p->o_res.fh;
1183 p->c_arg.stateid = &p->o_res.stateid;
1184 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001186 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001188
1189err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001190 nfs4_label_free(p->a_label);
1191err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001192 nfs4_label_free(p->f_label);
1193err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 kfree(p);
1195err:
1196 dput(parent);
1197 return NULL;
1198}
1199
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001200static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001201{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001202 struct nfs4_opendata *p = container_of(kref,
1203 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001204 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205
1206 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001207 if (p->state != NULL)
1208 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001209 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001210
Kinglong Meea49c2692015-07-27 15:31:38 +08001211 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001212 nfs4_label_free(p->f_label);
1213
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001214 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001215 dput(p->dentry);
1216 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001217 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001218 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001219 kfree(p);
1220}
1221
1222static void nfs4_opendata_put(struct nfs4_opendata *p)
1223{
1224 if (p != NULL)
1225 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001226}
1227
Trond Myklebust06f814a2006-01-03 09:55:07 +01001228static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1229{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001230 int ret;
1231
Trond Myklebust06f814a2006-01-03 09:55:07 +01001232 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001233 return ret;
1234}
1235
Trond Myklebust24311f82015-09-20 10:50:17 -04001236static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1237 fmode_t fmode)
1238{
1239 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1240 case FMODE_READ|FMODE_WRITE:
1241 return state->n_rdwr != 0;
1242 case FMODE_WRITE:
1243 return state->n_wronly != 0;
1244 case FMODE_READ:
1245 return state->n_rdonly != 0;
1246 }
1247 WARN_ON_ONCE(1);
1248 return false;
1249}
1250
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001252{
1253 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001254
Trond Myklebust536e43d2012-01-17 22:04:26 -05001255 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001256 goto out;
1257 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001258 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001259 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1260 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001261 break;
1262 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001263 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1264 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001265 break;
1266 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001267 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1268 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001269 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001271 return ret;
1272}
1273
Trond Myklebust2a606182015-08-19 22:30:00 -05001274static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1275 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001276{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001277 if (delegation == NULL)
1278 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001280 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001281 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1282 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001283 switch (claim) {
1284 case NFS4_OPEN_CLAIM_NULL:
1285 case NFS4_OPEN_CLAIM_FH:
1286 break;
1287 case NFS4_OPEN_CLAIM_PREVIOUS:
1288 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1289 break;
1290 default:
1291 return 0;
1292 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001293 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001294 return 1;
1295}
1296
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001297static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001298{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001299 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001300 case FMODE_WRITE:
1301 state->n_wronly++;
1302 break;
1303 case FMODE_READ:
1304 state->n_rdonly++;
1305 break;
1306 case FMODE_READ|FMODE_WRITE:
1307 state->n_rdwr++;
1308 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001310}
1311
Trond Myklebust4f14c192014-02-12 19:15:06 -05001312static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001313{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001314 struct nfs_client *clp = state->owner->so_server->nfs_client;
1315 bool need_recover = false;
1316
1317 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1318 need_recover = true;
1319 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1320 need_recover = true;
1321 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1322 need_recover = true;
1323 if (need_recover)
1324 nfs4_state_mark_reclaim_nograce(clp, state);
1325}
1326
Trond Myklebuste999e802014-02-10 18:20:47 -05001327static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1328 nfs4_stateid *stateid)
1329{
1330 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1331 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001332 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1333 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001334 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001335 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001336 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1337 return true;
1338 return false;
1339}
1340
Trond Myklebustf95549c2015-01-23 18:06:09 -05001341static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1342{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001343 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1344 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001345 if (state->n_wronly)
1346 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1347 if (state->n_rdonly)
1348 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1349 if (state->n_rdwr)
1350 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001351 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001352}
1353
Trond Myklebust226056c2014-02-11 10:41:07 -05001354static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001355 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001356 nfs4_stateid *stateid, fmode_t fmode)
1357{
1358 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1359 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1360 case FMODE_WRITE:
1361 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1362 break;
1363 case FMODE_READ:
1364 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1365 break;
1366 case 0:
1367 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1368 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1369 clear_bit(NFS_OPEN_STATE, &state->flags);
1370 }
1371 if (stateid == NULL)
1372 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001373 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001374 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1375 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1376 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001377 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001378 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001379 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001380 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001381 nfs4_stateid_copy(&state->stateid, stateid);
1382 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001383}
1384
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001385static void nfs_clear_open_stateid(struct nfs4_state *state,
1386 nfs4_stateid *arg_stateid,
1387 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001388{
1389 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001390 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001391 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001392 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1393 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001394}
1395
Trond Myklebust003707c2007-07-05 18:07:55 -04001396static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1397{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001398 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001399 case FMODE_READ:
1400 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1401 break;
1402 case FMODE_WRITE:
1403 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1404 break;
1405 case FMODE_READ|FMODE_WRITE:
1406 set_bit(NFS_O_RDWR_STATE, &state->flags);
1407 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001408 if (!nfs_need_update_open_stateid(state, stateid))
1409 return;
1410 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1411 nfs4_stateid_copy(&state->stateid, stateid);
1412 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001413}
1414
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001415static 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 -07001416{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001417 /*
1418 * Protect the call to nfs4_state_set_mode_locked and
1419 * serialise the stateid update
1420 */
Andrew Elble361cad32015-12-02 09:20:57 -05001421 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001422 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001423 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001424 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001425 set_bit(NFS_DELEGATED_STATE, &state->flags);
1426 }
1427 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001428 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001429 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001430 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001431 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001434static 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 -05001435{
1436 struct nfs_inode *nfsi = NFS_I(state->inode);
1437 struct nfs_delegation *deleg_cur;
1438 int ret = 0;
1439
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001441
1442 rcu_read_lock();
1443 deleg_cur = rcu_dereference(nfsi->delegation);
1444 if (deleg_cur == NULL)
1445 goto no_delegation;
1446
1447 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001448 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001449 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001450 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001451 goto no_delegation_unlock;
1452
1453 if (delegation == NULL)
1454 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001455 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001456 goto no_delegation_unlock;
1457
Trond Myklebustb7391f42008-12-23 15:21:52 -05001458 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001460 ret = 1;
1461no_delegation_unlock:
1462 spin_unlock(&deleg_cur->lock);
1463no_delegation:
1464 rcu_read_unlock();
1465
1466 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001467 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001468 ret = 1;
1469 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001470 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1471 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001472
1473 return ret;
1474}
1475
Trond Myklebust39071e62015-01-24 15:07:56 -05001476static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1477 const nfs4_stateid *stateid)
1478{
1479 struct nfs4_state *state = lsp->ls_state;
1480 bool ret = false;
1481
1482 spin_lock(&state->state_lock);
1483 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1484 goto out_noupdate;
1485 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1486 goto out_noupdate;
1487 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1488 ret = true;
1489out_noupdate:
1490 spin_unlock(&state->state_lock);
1491 return ret;
1492}
Trond Myklebust34310432008-12-23 15:21:38 -05001493
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001495{
1496 struct nfs_delegation *delegation;
1497
1498 rcu_read_lock();
1499 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001501 rcu_read_unlock();
1502 return;
1503 }
1504 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001505 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001506}
1507
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001509{
1510 struct nfs4_state *state = opendata->state;
1511 struct nfs_inode *nfsi = NFS_I(state->inode);
1512 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001513 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001514 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001515 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001516 nfs4_stateid stateid;
1517 int ret = -EAGAIN;
1518
Trond Myklebustaac00a82007-07-05 19:02:21 -04001519 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001520 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001521 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001522 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001523 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001524 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001525 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001526 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001527 rcu_read_lock();
1528 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001529 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001530 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001531 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001532 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001533 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001534 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001535 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001536 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001537 if (!opendata->is_recover) {
1538 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1539 if (ret != 0)
1540 goto out;
1541 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001543
1544 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001546 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001547 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548out:
1549 return ERR_PTR(ret);
1550out_return_state:
1551 atomic_inc(&state->count);
1552 return state;
1553}
1554
Andy Adamsone23008e2012-10-02 21:07:32 -04001555static void
1556nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1557{
1558 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1559 struct nfs_delegation *delegation;
1560 int delegation_flags = 0;
1561
1562 rcu_read_lock();
1563 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1564 if (delegation)
1565 delegation_flags = delegation->flags;
1566 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001567 switch (data->o_arg.claim) {
1568 default:
1569 break;
1570 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1571 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001572 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1573 "returning a delegation for "
1574 "OPEN(CLAIM_DELEGATE_CUR)\n",
1575 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001576 return;
1577 }
1578 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001579 nfs_inode_set_delegation(state->inode,
1580 data->owner->so_cred,
1581 &data->o_res);
1582 else
1583 nfs_inode_reclaim_delegation(state->inode,
1584 data->owner->so_cred,
1585 &data->o_res);
1586}
1587
1588/*
1589 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1590 * and update the nfs4_state.
1591 */
1592static struct nfs4_state *
1593_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1594{
1595 struct inode *inode = data->state->inode;
1596 struct nfs4_state *state = data->state;
1597 int ret;
1598
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001599 if (!data->rpc_done) {
1600 if (data->rpc_status) {
1601 ret = data->rpc_status;
1602 goto err;
1603 }
1604 /* cached opens have already been processed */
1605 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001606 }
1607
Andy Adamsone23008e2012-10-02 21:07:32 -04001608 ret = nfs_refresh_inode(inode, &data->f_attr);
1609 if (ret)
1610 goto err;
1611
1612 if (data->o_res.delegation_type != 0)
1613 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001614update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001615 update_open_stateid(state, &data->o_res.stateid, NULL,
1616 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001617 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001618
1619 return state;
1620err:
1621 return ERR_PTR(ret);
1622
1623}
1624
1625static struct nfs4_state *
1626_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627{
1628 struct inode *inode;
1629 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001630 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001631
Trond Myklebustaac00a82007-07-05 19:02:21 -04001632 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001633 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001634 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 goto out;
1636 }
1637
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001638 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001640 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001641 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001642 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001643 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001644 goto err;
1645 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646 state = nfs4_get_open_state(inode, data->owner);
1647 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001648 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001649 if (data->o_res.delegation_type != 0)
1650 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001651 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001652 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001654out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001655 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001657err_put_inode:
1658 iput(inode);
1659err:
1660 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661}
1662
Andy Adamsone23008e2012-10-02 21:07:32 -04001663static struct nfs4_state *
1664nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1665{
1666 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1667 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1668 return _nfs4_opendata_to_nfs4_state(data);
1669}
1670
Trond Myklebust864472e2006-01-03 09:55:15 +01001671static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1672{
1673 struct nfs_inode *nfsi = NFS_I(state->inode);
1674 struct nfs_open_context *ctx;
1675
1676 spin_lock(&state->inode->i_lock);
1677 list_for_each_entry(ctx, &nfsi->open_files, list) {
1678 if (ctx->state != state)
1679 continue;
1680 get_nfs_open_context(ctx);
1681 spin_unlock(&state->inode->i_lock);
1682 return ctx;
1683 }
1684 spin_unlock(&state->inode->i_lock);
1685 return ERR_PTR(-ENOENT);
1686}
1687
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001688static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1689 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001690{
1691 struct nfs4_opendata *opendata;
1692
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001693 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001694 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001695 if (opendata == NULL)
1696 return ERR_PTR(-ENOMEM);
1697 opendata->state = state;
1698 atomic_inc(&state->count);
1699 return opendata;
1700}
1701
Trond Myklebust24311f82015-09-20 10:50:17 -04001702static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1703 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001704{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001705 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001706 int ret;
1707
Trond Myklebust24311f82015-09-20 10:50:17 -04001708 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001709 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001710 opendata->o_arg.open_flags = 0;
1711 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001712 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1713 NFS_SB(opendata->dentry->d_sb),
1714 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001715 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1716 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1717 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001718 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 if (ret != 0)
1720 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001721 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001722 if (IS_ERR(newstate))
1723 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001724 if (newstate != opendata->state)
1725 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001726 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001727 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001728}
1729
1730static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1731{
Trond Myklebust864472e2006-01-03 09:55:15 +01001732 int ret;
1733
Trond Myklebust4f14c192014-02-12 19:15:06 -05001734 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1735 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1736 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1737 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001738 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001739 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001740 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001741 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001742 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1743 if (ret != 0)
1744 return ret;
1745 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1746 if (ret != 0)
1747 return ret;
1748 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1749 if (ret != 0)
1750 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001751 /*
1752 * We may have performed cached opens for all three recoveries.
1753 * Check if we need to update the current stateid.
1754 */
1755 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001756 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001757 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001758 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001759 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001760 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001761 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001762 return 0;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/*
1766 * OPEN_RECLAIM:
1767 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001769static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001771 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001772 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001773 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int status;
1775
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001776 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1777 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001778 if (IS_ERR(opendata))
1779 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001780 rcu_read_lock();
1781 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001782 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001783 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001784 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001785 opendata->o_arg.u.delegation_type = delegation_type;
1786 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001787 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return status;
1789}
1790
Trond Myklebust539cd032007-06-05 11:46:42 -04001791static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 struct nfs_server *server = NFS_SERVER(state->inode);
1794 struct nfs4_exception exception = { };
1795 int err;
1796 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001797 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001798 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001799 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1800 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001801 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001802 break;
1803 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 } while (exception.retry);
1805 return err;
1806}
1807
Trond Myklebust864472e2006-01-03 09:55:15 +01001808static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1809{
1810 struct nfs_open_context *ctx;
1811 int ret;
1812
1813 ctx = nfs4_state_find_open_context(state);
1814 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001815 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001816 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001817 put_nfs_open_context(ctx);
1818 return ret;
1819}
1820
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001821static 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 -07001822{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001823 switch (err) {
1824 default:
1825 printk(KERN_ERR "NFS: %s: unhandled error "
1826 "%d.\n", __func__, err);
1827 case 0:
1828 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001829 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001830 case -ESTALE:
1831 break;
1832 case -NFS4ERR_BADSESSION:
1833 case -NFS4ERR_BADSLOT:
1834 case -NFS4ERR_BAD_HIGH_SLOT:
1835 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1836 case -NFS4ERR_DEADSESSION:
1837 set_bit(NFS_DELEGATED_STATE, &state->flags);
1838 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1839 return -EAGAIN;
1840 case -NFS4ERR_STALE_CLIENTID:
1841 case -NFS4ERR_STALE_STATEID:
1842 set_bit(NFS_DELEGATED_STATE, &state->flags);
1843 case -NFS4ERR_EXPIRED:
1844 /* Don't recall a delegation if it was lost */
1845 nfs4_schedule_lease_recovery(server->nfs_client);
1846 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001847 case -NFS4ERR_MOVED:
1848 nfs4_schedule_migration_recovery(server);
1849 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001850 case -NFS4ERR_LEASE_MOVED:
1851 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1852 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001853 case -NFS4ERR_DELEG_REVOKED:
1854 case -NFS4ERR_ADMIN_REVOKED:
1855 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001856 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001857 nfs_inode_find_state_and_recover(state->inode,
1858 stateid);
1859 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001860 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001861 case -NFS4ERR_DELAY:
1862 case -NFS4ERR_GRACE:
1863 set_bit(NFS_DELEGATED_STATE, &state->flags);
1864 ssleep(1);
1865 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001866 case -ENOMEM:
1867 case -NFS4ERR_DENIED:
1868 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1869 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return err;
1872}
1873
Trond Myklebust24311f82015-09-20 10:50:17 -04001874int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1875 struct nfs4_state *state, const nfs4_stateid *stateid,
1876 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001877{
1878 struct nfs_server *server = NFS_SERVER(state->inode);
1879 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001880 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001881
1882 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1883 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1884 if (IS_ERR(opendata))
1885 return PTR_ERR(opendata);
1886 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001887 write_seqlock(&state->seqlock);
1888 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1889 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001890 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1891 switch (type & (FMODE_READ|FMODE_WRITE)) {
1892 case FMODE_READ|FMODE_WRITE:
1893 case FMODE_WRITE:
1894 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1895 if (err)
1896 break;
1897 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1898 if (err)
1899 break;
1900 case FMODE_READ:
1901 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1902 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001903 nfs4_opendata_put(opendata);
1904 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1905}
1906
Chuck Leverbe05c862013-08-09 12:49:47 -04001907static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1908{
1909 struct nfs4_opendata *data = calldata;
1910
Peng Taocb04ad22014-06-11 05:24:15 +08001911 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1912 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001913}
1914
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001915static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1916{
1917 struct nfs4_opendata *data = calldata;
1918
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001919 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001920
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001921 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001922 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001923 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001924 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001925 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001926 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001927 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001928}
1929
1930static void nfs4_open_confirm_release(void *calldata)
1931{
1932 struct nfs4_opendata *data = calldata;
1933 struct nfs4_state *state = NULL;
1934
1935 /* If this request hasn't been cancelled, do nothing */
1936 if (data->cancelled == 0)
1937 goto out_free;
1938 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001939 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001940 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001941 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001942 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001943 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001944out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001945 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001946}
1947
1948static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001949 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001950 .rpc_call_done = nfs4_open_confirm_done,
1951 .rpc_release = nfs4_open_confirm_release,
1952};
1953
1954/*
1955 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1956 */
1957static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1958{
David Howells2b0143b2015-03-17 22:25:59 +00001959 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001960 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001961 struct rpc_message msg = {
1962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1963 .rpc_argp = &data->c_arg,
1964 .rpc_resp = &data->c_res,
1965 .rpc_cred = data->owner->so_cred,
1966 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001967 struct rpc_task_setup task_setup_data = {
1968 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001969 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001970 .callback_ops = &nfs4_open_confirm_ops,
1971 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001972 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001973 .flags = RPC_TASK_ASYNC,
1974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int status;
1976
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001977 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001978 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001979 data->rpc_done = 0;
1980 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001981 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001982 if (data->is_recover)
1983 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001984 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001985 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001986 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001987 status = nfs4_wait_for_completion_rpc_task(task);
1988 if (status != 0) {
1989 data->cancelled = 1;
1990 smp_wmb();
1991 } else
1992 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001993 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return status;
1995}
1996
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001997static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001999 struct nfs4_opendata *data = calldata;
2000 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002001 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002002 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002005 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002006 /*
2007 * Check if we still need to send an OPEN call, or if we can use
2008 * a delegation instead.
2009 */
2010 if (data->state != NULL) {
2011 struct nfs_delegation *delegation;
2012
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002013 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002014 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002015 rcu_read_lock();
2016 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002017 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002018 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002019 rcu_read_unlock();
2020 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002021 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002022 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002023 switch (claim) {
2024 default:
2025 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002026 case NFS4_OPEN_CLAIM_PREVIOUS:
2027 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2028 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002029 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002030 case NFS4_OPEN_CLAIM_FH:
2031 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002032 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2033 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002034 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002035 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002036 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002037 &data->o_res.seq_res,
2038 task) != 0)
2039 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002040
2041 /* Set the create mode (note dependency on the session type) */
2042 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2043 if (data->o_arg.open_flags & O_EXCL) {
2044 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2045 if (nfs4_has_persistent_session(clp))
2046 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2047 else if (clp->cl_mvops->minor_version > 0)
2048 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2049 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002050 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002051unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002052 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002053 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002054out_no_action:
2055 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002056out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002057 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002058}
2059
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002060static void nfs4_open_done(struct rpc_task *task, void *calldata)
2061{
2062 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002064 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002065
Trond Myklebust14516c32010-07-31 14:29:06 -04002066 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2067 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002068
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002069 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002070 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2071 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002072 case S_IFREG:
2073 break;
2074 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002075 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002076 break;
2077 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002078 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002079 break;
2080 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002081 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002082 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002083 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002084 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002085 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2086 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002087 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002088 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002089}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002090
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002091static void nfs4_open_release(void *calldata)
2092{
2093 struct nfs4_opendata *data = calldata;
2094 struct nfs4_state *state = NULL;
2095
2096 /* If this request hasn't been cancelled, do nothing */
2097 if (data->cancelled == 0)
2098 goto out_free;
2099 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002100 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002101 goto out_free;
2102 /* In case we need an open_confirm, no cleanup! */
2103 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2104 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002105 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002106 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002107 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002108out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002109 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002110}
2111
2112static const struct rpc_call_ops nfs4_open_ops = {
2113 .rpc_call_prepare = nfs4_open_prepare,
2114 .rpc_call_done = nfs4_open_done,
2115 .rpc_release = nfs4_open_release,
2116};
2117
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002118static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002119{
David Howells2b0143b2015-03-17 22:25:59 +00002120 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 struct nfs_server *server = NFS_SERVER(dir);
2122 struct nfs_openargs *o_arg = &data->o_arg;
2123 struct nfs_openres *o_res = &data->o_res;
2124 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002125 struct rpc_message msg = {
2126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2127 .rpc_argp = o_arg,
2128 .rpc_resp = o_res,
2129 .rpc_cred = data->owner->so_cred,
2130 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002131 struct rpc_task_setup task_setup_data = {
2132 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002133 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002134 .callback_ops = &nfs4_open_ops,
2135 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002136 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002137 .flags = RPC_TASK_ASYNC,
2138 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002139 int status;
2140
Chuck Levera9c92d62013-08-09 12:48:18 -04002141 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002142 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002143 data->rpc_done = 0;
2144 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002145 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002146 data->is_recover = 0;
2147 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002148 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002149 data->is_recover = 1;
2150 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002151 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002152 if (IS_ERR(task))
2153 return PTR_ERR(task);
2154 status = nfs4_wait_for_completion_rpc_task(task);
2155 if (status != 0) {
2156 data->cancelled = 1;
2157 smp_wmb();
2158 } else
2159 status = data->rpc_status;
2160 rpc_put_task(task);
2161
2162 return status;
2163}
2164
2165static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2166{
David Howells2b0143b2015-03-17 22:25:59 +00002167 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002168 struct nfs_openres *o_res = &data->o_res;
2169 int status;
2170
2171 status = nfs4_run_open_task(data, 1);
2172 if (status != 0 || !data->rpc_done)
2173 return status;
2174
Trond Myklebust6926afd2012-01-07 13:22:46 -05002175 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2176
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002177 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2178 status = _nfs4_proc_open_confirm(data);
2179 if (status != 0)
2180 return status;
2181 }
2182
2183 return status;
2184}
2185
Trond Myklebustf3792d62014-07-10 08:54:32 -04002186/*
2187 * Additional permission checks in order to distinguish between an
2188 * open for read, and an open for execute. This works around the
2189 * fact that NFSv4 OPEN treats read and execute permissions as being
2190 * the same.
2191 * Note that in the non-execute case, we want to turn off permission
2192 * checking if we just created a new file (POSIX open() semantics).
2193 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002194static int nfs4_opendata_access(struct rpc_cred *cred,
2195 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002196 struct nfs4_state *state, fmode_t fmode,
2197 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002198{
2199 struct nfs_access_entry cache;
2200 u32 mask;
2201
2202 /* access call failed or for some reason the server doesn't
2203 * support any access modes -- defer access call until later */
2204 if (opendata->o_res.access_supported == 0)
2205 return 0;
2206
2207 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002208 /*
2209 * Use openflags to check for exec, because fmode won't
2210 * always have FMODE_EXEC set when file open for exec.
2211 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002212 if (openflags & __FMODE_EXEC) {
2213 /* ONLY check for exec rights */
2214 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002215 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002216 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002217
2218 cache.cred = cred;
2219 cache.jiffies = jiffies;
2220 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2221 nfs_access_add_cache(state->inode, &cache);
2222
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002223 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002224 return 0;
2225
2226 /* even though OPEN succeeded, access is denied. Close the file */
2227 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002228 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002229}
2230
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002231/*
2232 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2233 */
2234static int _nfs4_proc_open(struct nfs4_opendata *data)
2235{
David Howells2b0143b2015-03-17 22:25:59 +00002236 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002237 struct nfs_server *server = NFS_SERVER(dir);
2238 struct nfs_openargs *o_arg = &data->o_arg;
2239 struct nfs_openres *o_res = &data->o_res;
2240 int status;
2241
2242 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002243 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002244 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002245 if (status != 0) {
2246 if (status == -NFS4ERR_BADNAME &&
2247 !(o_arg->open_flags & O_CREAT))
2248 return -ENOENT;
2249 return status;
2250 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002251
Trond Myklebust6926afd2012-01-07 13:22:46 -05002252 nfs_fattr_map_and_free_names(server, &data->f_attr);
2253
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002254 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002255 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002256 if (o_arg->open_flags & O_EXCL)
2257 data->file_created = 1;
2258 else if (o_res->cinfo.before != o_res->cinfo.after)
2259 data->file_created = 1;
2260 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002261 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2262 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002264 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002269 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Andy Adamsond83217c2011-03-01 01:34:17 +00002273static int nfs4_recover_expired_lease(struct nfs_server *server)
2274{
2275 return nfs4_client_recover_expired_lease(server->nfs_client);
2276}
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278/*
2279 * OPEN_EXPIRED:
2280 * reclaim state on the server after a network partition.
2281 * Assumes caller holds the appropriate lock
2282 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002283static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002285 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002286 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002288 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002289 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002290 if (IS_ERR(opendata))
2291 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002292 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002293 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002294 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002295 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002299static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002300{
Trond Myklebust539cd032007-06-05 11:46:42 -04002301 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002302 struct nfs4_exception exception = { };
2303 int err;
2304
2305 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002306 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002307 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002308 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2309 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002310 switch (err) {
2311 default:
2312 goto out;
2313 case -NFS4ERR_GRACE:
2314 case -NFS4ERR_DELAY:
2315 nfs4_handle_exception(server, err, &exception);
2316 err = 0;
2317 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002318 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002319out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002320 return err;
2321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002326 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Trond Myklebust864472e2006-01-03 09:55:15 +01002328 ctx = nfs4_state_find_open_context(state);
2329 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002330 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002331 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002332 put_nfs_open_context(ctx);
2333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002336static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2337{
2338 nfs_remove_bad_delegation(state->inode);
2339 write_seqlock(&state->seqlock);
2340 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2341 write_sequnlock(&state->seqlock);
2342 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2343}
2344
2345static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2346{
2347 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2348 nfs_finish_clear_delegation_stateid(state);
2349}
2350
2351static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2352{
2353 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2354 nfs40_clear_delegation_stateid(state);
2355 return nfs4_open_expired(sp, state);
2356}
2357
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002358#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002359static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002360{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002361 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002362 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002363 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002364 struct rpc_cred *cred;
2365 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002366
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002367 /* Get the delegation credential for use by test/free_stateid */
2368 rcu_read_lock();
2369 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002370 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002371 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002372 return;
2373 }
2374
2375 nfs4_stateid_copy(&stateid, &delegation->stateid);
2376 cred = get_rpccred(delegation->cred);
2377 rcu_read_unlock();
2378 status = nfs41_test_stateid(server, &stateid, cred);
2379 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002380
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002381 if (status != NFS_OK) {
2382 /* Free the stateid unless the server explicitly
2383 * informs us the stateid is unrecognized. */
2384 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002385 nfs41_free_stateid(server, &stateid, cred);
2386 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002387 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002388
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002389 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002390}
2391
2392/**
2393 * nfs41_check_open_stateid - possibly free an open stateid
2394 *
2395 * @state: NFSv4 state for an inode
2396 *
2397 * Returns NFS_OK if recovery for this stateid is now finished.
2398 * Otherwise a negative NFS4ERR value is returned.
2399 */
2400static int nfs41_check_open_stateid(struct nfs4_state *state)
2401{
2402 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002403 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002404 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002405 int status;
2406
2407 /* If a state reset has been done, test_stateid is unneeded */
2408 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2409 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2410 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2411 return -NFS4ERR_BAD_STATEID;
2412
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002413 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002414 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002415 if (status != NFS_OK) {
2416 /* Free the stateid unless the server explicitly
2417 * informs us the stateid is unrecognized. */
2418 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002419 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002420
2421 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2422 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2423 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002424 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002425 }
2426 return status;
2427}
2428
2429static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2430{
Chuck Levereb64cf92012-07-11 16:30:05 -04002431 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002432
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002433 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002434 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002435 if (status != NFS_OK)
2436 status = nfs4_open_expired(sp, state);
2437 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002438}
2439#endif
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002442 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2443 * fields corresponding to attributes that were used to store the verifier.
2444 * Make sure we clobber those fields in the later setattr call
2445 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002446static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2447 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002448{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002449 const u32 *attrset = opendata->o_res.attrset;
2450
2451 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002452 !(sattr->ia_valid & ATTR_ATIME_SET))
2453 sattr->ia_valid |= ATTR_ATIME;
2454
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002455 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002456 !(sattr->ia_valid & ATTR_MTIME_SET))
2457 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002458
2459 /* Except MODE, it seems harmless of setting twice. */
2460 if ((attrset[1] & FATTR4_WORD1_MODE))
2461 sattr->ia_valid &= ~ATTR_MODE;
2462
2463 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2464 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002465}
2466
Trond Myklebustc21443c2013-02-07 14:26:21 -05002467static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2468 fmode_t fmode,
2469 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002470 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002471{
2472 struct nfs4_state_owner *sp = opendata->owner;
2473 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002474 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002475 struct nfs4_state *state;
2476 unsigned int seq;
2477 int ret;
2478
2479 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2480
2481 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002482 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002483 goto out;
2484
2485 state = nfs4_opendata_to_nfs4_state(opendata);
2486 ret = PTR_ERR(state);
2487 if (IS_ERR(state))
2488 goto out;
2489 if (server->caps & NFS_CAP_POSIX_LOCK)
2490 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2491
Trond Myklebust275bb302013-05-29 13:11:28 -04002492 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002493 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002494 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002495 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002496 alias = d_exact_alias(dentry, state->inode);
2497 if (!alias)
2498 alias = d_splice_alias(igrab(state->inode), dentry);
2499 /* d_splice_alias() can't fail here - it's a non-directory */
2500 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002501 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002502 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002503 }
2504 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002505 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002506 }
2507
Trond Myklebustc21443c2013-02-07 14:26:21 -05002508 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2509 if (ret != 0)
2510 goto out;
2511
Trond Myklebust3efb9722013-05-29 13:17:04 -04002512 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002513 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002514 nfs_inode_attach_open_context(ctx);
2515 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2516 nfs4_schedule_stateid_recovery(server, state);
2517 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002518out:
2519 return ret;
2520}
2521
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002522/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002523 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 */
Andy Adamson82be4172012-05-23 05:02:35 -04002525static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002526 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002527 int flags,
2528 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002529 struct nfs4_label *label,
2530 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 struct nfs4_state_owner *sp;
2533 struct nfs4_state *state = NULL;
2534 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002535 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002536 struct dentry *dentry = ctx->dentry;
2537 struct rpc_cred *cred = ctx->cred;
2538 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2539 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002540 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002541 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002542 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002546 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2547 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2549 goto out_err;
2550 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002551 status = nfs4_recover_expired_lease(server);
2552 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002553 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002554 if (d_really_is_positive(dentry))
2555 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002556 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002557 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002558 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002559 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002560 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002561 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002562 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
David Quigley14c43f72013-05-22 12:50:43 -04002564 if (label) {
2565 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2566 if (IS_ERR(olabel)) {
2567 status = PTR_ERR(olabel);
2568 goto err_opendata_put;
2569 }
2570 }
2571
Trond Myklebuste911b812014-03-26 13:24:37 -07002572 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2573 if (!opendata->f_attr.mdsthreshold) {
2574 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2575 if (!opendata->f_attr.mdsthreshold)
2576 goto err_free_label;
2577 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002578 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002579 }
David Howells2b0143b2015-03-17 22:25:59 +00002580 if (d_really_is_positive(dentry))
2581 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002582
Trond Myklebust3efb9722013-05-29 13:17:04 -04002583 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002584 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002585 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002586 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002587
NeilBrownefcbc042015-07-30 13:00:56 +10002588 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002589 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002590 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002591 /*
2592 * send create attributes which was not set by open
2593 * with an extra setattr.
2594 */
2595 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2596 nfs_fattr_init(opendata->o_res.f_attr);
2597 status = nfs4_do_setattr(state->inode, cred,
2598 opendata->o_res.f_attr, sattr,
2599 state, label, olabel);
2600 if (status == 0) {
2601 nfs_setattr_update_inode(state->inode, sattr,
2602 opendata->o_res.f_attr);
2603 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2604 }
David Quigley1775fd32013-05-22 12:50:42 -04002605 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002606 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002607 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002608 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002609
Trond Myklebuste911b812014-03-26 13:24:37 -07002610 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002611 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002612 opendata->f_attr.mdsthreshold = NULL;
2613 }
Andy Adamson82be4172012-05-23 05:02:35 -04002614
David Quigley14c43f72013-05-22 12:50:43 -04002615 nfs4_label_free(olabel);
2616
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002617 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002620err_free_label:
2621 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002622err_opendata_put:
2623 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002624err_put_state_owner:
2625 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return status;
2628}
2629
2630
Andy Adamson82be4172012-05-23 05:02:35 -04002631static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002632 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002633 int flags,
2634 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002635 struct nfs4_label *label,
2636 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002638 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 struct nfs4_exception exception = { };
2640 struct nfs4_state *res;
2641 int status;
2642
2643 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002644 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002645 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002646 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (status == 0)
2648 break;
2649 /* NOTE: BAD_SEQID means the server and client disagree about the
2650 * book-keeping w.r.t. state-changing operations
2651 * (OPEN/CLOSE/LOCK/LOCKU...)
2652 * It is actually a sign of a bug on the client or on the server.
2653 *
2654 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002655 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 * have unhashed the old state_owner for us, and that we can
2657 * therefore safely retry using a new one. We should still warn
2658 * the user though...
2659 */
2660 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002661 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002662 " returned a bad sequence-id error!\n",
2663 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 exception.retry = 1;
2665 continue;
2666 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002667 /*
2668 * BAD_STATEID on OPEN means that the server cancelled our
2669 * state before it received the OPEN_CONFIRM.
2670 * Recover by retrying the request as per the discussion
2671 * on Page 181 of RFC3530.
2672 */
2673 if (status == -NFS4ERR_BAD_STATEID) {
2674 exception.retry = 1;
2675 continue;
2676 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002677 if (status == -EAGAIN) {
2678 /* We must have found a delegation */
2679 exception.retry = 1;
2680 continue;
2681 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002682 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2683 continue;
2684 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 status, &exception));
2686 } while (exception.retry);
2687 return res;
2688}
2689
Trond Myklebust8487c472016-06-26 08:44:35 -04002690static int _nfs4_do_setattr(struct inode *inode,
2691 struct nfs_setattrargs *arg,
2692 struct nfs_setattrres *res,
2693 struct rpc_cred *cred,
2694 struct nfs4_state *state)
2695{
2696 struct nfs_server *server = NFS_SERVER(inode);
2697 struct rpc_message msg = {
2698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2699 .rpc_argp = arg,
2700 .rpc_resp = res,
2701 .rpc_cred = cred,
2702 };
2703 struct rpc_cred *delegation_cred = NULL;
2704 unsigned long timestamp = jiffies;
2705 fmode_t fmode;
2706 bool truncate;
2707 int status;
2708
2709 nfs_fattr_init(res->fattr);
2710
2711 /* Servers should only apply open mode checks for file size changes */
2712 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2713 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2714
2715 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2716 /* Use that stateid */
2717 } else if (truncate && state != NULL) {
2718 struct nfs_lockowner lockowner = {
2719 .l_owner = current->files,
2720 .l_pid = current->tgid,
2721 };
2722 if (!nfs4_valid_open_stateid(state))
2723 return -EBADF;
2724 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2725 &arg->stateid, &delegation_cred) == -EIO)
2726 return -EBADF;
2727 } else
2728 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2729 if (delegation_cred)
2730 msg.rpc_cred = delegation_cred;
2731
2732 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2733
2734 put_rpccred(delegation_cred);
2735 if (status == 0 && state != NULL)
2736 renew_lease(server, timestamp);
2737 trace_nfs4_setattr(inode, &arg->stateid, status);
2738 return status;
2739}
2740
2741static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2742 struct nfs_fattr *fattr, struct iattr *sattr,
2743 struct nfs4_state *state, struct nfs4_label *ilabel,
2744 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002746 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002748 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 .iap = sattr,
2750 .server = server,
2751 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002752 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 };
2754 struct nfs_setattrres res = {
2755 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002756 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .server = server,
2758 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002759 struct nfs4_exception exception = {
2760 .state = state,
2761 .inode = inode,
2762 .stateid = &arg.stateid,
2763 };
2764 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
David Quigleyaa9c2662013-05-22 12:50:44 -04002766 arg.bitmask = nfs4_bitmask(server, ilabel);
2767 if (ilabel)
2768 arg.bitmask = nfs4_bitmask(server, olabel);
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002771 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002772 switch (err) {
2773 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002774 if (!(sattr->ia_valid & ATTR_SIZE)) {
2775 pr_warn_once("NFSv4: server %s is incorrectly "
2776 "applying open mode checks to "
2777 "a SETATTR that is not "
2778 "changing file size.\n",
2779 server->nfs_client->cl_hostname);
2780 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002781 if (state && !(state->state & FMODE_WRITE)) {
2782 err = -EBADF;
2783 if (sattr->ia_valid & ATTR_OPEN)
2784 err = -EACCES;
2785 goto out;
2786 }
2787 }
2788 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002790out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return err;
2792}
2793
Peng Tao500d7012015-09-22 11:35:22 +08002794static bool
2795nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2796{
2797 if (inode == NULL || !nfs_have_layout(inode))
2798 return false;
2799
2800 return pnfs_wait_on_layoutreturn(inode, task);
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803struct nfs4_closedata {
2804 struct inode *inode;
2805 struct nfs4_state *state;
2806 struct nfs_closeargs arg;
2807 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002808 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002809 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002810 bool roc;
2811 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812};
2813
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002814static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002815{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002816 struct nfs4_closedata *calldata = data;
2817 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002818 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002819
Fred Isamanf7e89172011-01-06 11:36:32 +00002820 if (calldata->roc)
2821 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002822 nfs4_put_open_state(calldata->state);
2823 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002824 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002825 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002826 kfree(calldata);
2827}
2828
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002829static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002831 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002834 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Chuck Levera3ca5652012-03-01 17:00:40 -05002836 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002837 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2838 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002839 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 /* hmm. we are done with the inode, and in the process of freeing
2841 * the state_owner. we keep this around to process errors
2842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 switch (task->tk_status) {
2844 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002845 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002846 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002847 pnfs_roc_set_barrier(state->inode,
2848 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002849 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002850 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002851 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002853 case -NFS4ERR_OLD_STATEID:
2854 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002856 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002857 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002858 rpc_restart_call_prepare(task);
2859 goto out_release;
2860 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002861 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002864 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002865 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002866 goto out_release;
2867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002869 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2870 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002871out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002872 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002873 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002874 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002877static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002879 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002880 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002881 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002882 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002883 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002884
Chuck Levera3ca5652012-03-01 17:00:40 -05002885 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002886 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002887 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002888
Trond Myklebust88069f72009-12-08 08:33:16 -05002889 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002890 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002891 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2892 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2893 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002894 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002895 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002896 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002897 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002898 if (state->n_rdonly == 0)
2899 call_close |= is_rdonly;
2900 else if (is_rdonly)
2901 calldata->arg.fmode |= FMODE_READ;
2902 if (state->n_wronly == 0)
2903 call_close |= is_wronly;
2904 else if (is_wronly)
2905 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002906 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2907 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002908 } else if (is_rdwr)
2909 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2910
Trond Myklebust5d422302013-03-14 16:57:48 -04002911 if (!nfs4_valid_open_stateid(state))
2912 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002913 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002914
2915 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002916 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002917 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002918 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002919
Peng Tao500d7012015-09-22 11:35:22 +08002920 if (nfs4_wait_on_layoutreturn(inode, task)) {
2921 nfs_release_seqid(calldata->arg.seqid);
2922 goto out_wait;
2923 }
2924
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002925 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002926 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002927 if (calldata->roc)
2928 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2929
Trond Myklebust6ae37332015-01-30 14:21:14 -05002930 calldata->arg.share_access =
2931 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2932 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002933
Trond Myklebust516a6af2005-10-27 22:12:41 -04002934 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002935 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002936 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002937 &calldata->arg.seq_args,
2938 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002939 task) != 0)
2940 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002941 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002942 return;
2943out_no_action:
2944 task->tk_action = NULL;
2945out_wait:
2946 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002949static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002950 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002951 .rpc_call_done = nfs4_close_done,
2952 .rpc_release = nfs4_free_closedata,
2953};
2954
Peng Taofe08c542014-07-03 13:05:01 +08002955static bool nfs4_roc(struct inode *inode)
2956{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002957 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002958 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002959 return pnfs_roc(inode);
2960}
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962/*
2963 * It is possible for data to be read/written from a mem-mapped file
2964 * after the sys_close call (which hits the vfs layer as a flush).
2965 * This means that we can't safely call nfsv4 close on a file until
2966 * the inode is cleared. This in turn means that we are not good
2967 * NFSv4 citizens - we do not indicate to the server to update the file's
2968 * share state even when we are done with one of the three share
2969 * stateid's in the inode.
2970 *
2971 * NOTE: Caller must be holding the sp->so_owner semaphore!
2972 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002973int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002975 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002976 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002978 struct nfs4_state_owner *sp = state->owner;
2979 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002980 struct rpc_message msg = {
2981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2982 .rpc_cred = state->owner->so_cred,
2983 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002984 struct rpc_task_setup task_setup_data = {
2985 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002986 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002987 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002988 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002989 .flags = RPC_TASK_ASYNC,
2990 };
Trond Myklebust95121352005-10-18 14:20:12 -07002991 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002993 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2994 &task_setup_data.rpc_client, &msg);
2995
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002996 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002998 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002999 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003000 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003002 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003004 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3005 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003006 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003007 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003008 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003009 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003010 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003011 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003012 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003013 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003014 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003015
Trond Myklebust1174dd12010-12-21 10:52:24 -05003016 msg.rpc_argp = &calldata->arg;
3017 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003018 task_setup_data.callback_data = calldata;
3019 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003020 if (IS_ERR(task))
3021 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003022 status = 0;
3023 if (wait)
3024 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003025 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003026 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003027out_free_calldata:
3028 kfree(calldata);
3029out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003030 nfs4_put_open_state(state);
3031 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003032 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Trond Myklebust2b484292010-09-17 10:56:51 -04003035static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003036nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3037 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003040 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3041
3042 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Trond Myklebust565277f2007-10-15 18:17:53 -04003044 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003045 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003046
3047 nfs4_label_release_security(label);
3048
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003049 if (IS_ERR(state))
3050 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003051 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Trond Myklebust1185a552009-12-03 15:54:02 -05003054static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003055{
3056 if (ctx->state == NULL)
3057 return;
3058 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003059 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003060 else
Al Viro643168c2011-06-22 18:20:23 -04003061 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Trond Myklebustb944dba2013-11-04 15:20:20 -05003064#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3065#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003066#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3069{
Kinglong Mee8c612822015-08-26 21:12:58 +08003070 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003071 struct nfs4_server_caps_arg args = {
3072 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003073 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct nfs4_server_caps_res res = {};
3076 struct rpc_message msg = {
3077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003078 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 .rpc_resp = &res,
3080 };
3081 int status;
3082
Kinglong Mee8c612822015-08-26 21:12:58 +08003083 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3084 FATTR4_WORD0_FH_EXPIRE_TYPE |
3085 FATTR4_WORD0_LINK_SUPPORT |
3086 FATTR4_WORD0_SYMLINK_SUPPORT |
3087 FATTR4_WORD0_ACLSUPPORT;
3088 if (minorversion)
3089 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3090
Bryan Schumaker7c513052011-03-24 17:12:24 +00003091 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003093 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003094 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003095 case 0:
3096 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3097 res.attr_bitmask[2] = 0;
3098 break;
3099 case 1:
3100 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3101 break;
3102 case 2:
3103 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003106 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3107 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3108 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3109 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003110 NFS_CAP_CTIME|NFS_CAP_MTIME|
3111 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003112 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3113 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 server->caps |= NFS_CAP_ACLS;
3115 if (res.has_links != 0)
3116 server->caps |= NFS_CAP_HARDLINKS;
3117 if (res.has_symlinks != 0)
3118 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003119 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3120 server->caps |= NFS_CAP_FILEID;
3121 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3122 server->caps |= NFS_CAP_MODE;
3123 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3124 server->caps |= NFS_CAP_NLINK;
3125 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3126 server->caps |= NFS_CAP_OWNER;
3127 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3128 server->caps |= NFS_CAP_OWNER_GROUP;
3129 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3130 server->caps |= NFS_CAP_ATIME;
3131 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3132 server->caps |= NFS_CAP_CTIME;
3133 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3134 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003135#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3136 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3137 server->caps |= NFS_CAP_SECURITY_LABEL;
3138#endif
3139 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3140 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003141 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003142
Trond Myklebusta65318b2009-03-11 14:10:28 -04003143 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3144 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3145 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003146 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003147 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3148 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003150 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return status;
3154}
3155
Trond Myklebust55a97592006-06-09 09:34:19 -04003156int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
3158 struct nfs4_exception exception = { };
3159 int err;
3160 do {
3161 err = nfs4_handle_exception(server,
3162 _nfs4_server_capabilities(server, fhandle),
3163 &exception);
3164 } while (exception.retry);
3165 return err;
3166}
3167
3168static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3169 struct nfs_fsinfo *info)
3170{
David Quigleyaa9c2662013-05-22 12:50:44 -04003171 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003173 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 };
3175 struct nfs4_lookup_res res = {
3176 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003177 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 .fh = fhandle,
3179 };
3180 struct rpc_message msg = {
3181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3182 .rpc_argp = &args,
3183 .rpc_resp = &res,
3184 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003185
David Quigleyaa9c2662013-05-22 12:50:44 -04003186 bitmask[0] = nfs4_fattr_bitmap[0];
3187 bitmask[1] = nfs4_fattr_bitmap[1];
3188 /*
3189 * Process the label in the upcoming getfattr
3190 */
3191 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3192
Trond Myklebust0e574af2005-10-27 22:12:38 -04003193 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003194 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
3197static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3198 struct nfs_fsinfo *info)
3199{
3200 struct nfs4_exception exception = { };
3201 int err;
3202 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003203 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003204 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003205 switch (err) {
3206 case 0:
3207 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003208 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003209 default:
3210 err = nfs4_handle_exception(server, err, &exception);
3211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return err;
3215}
3216
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003217static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3218 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3219{
Trond Myklebustc2190662013-08-26 19:23:04 -04003220 struct rpc_auth_create_args auth_args = {
3221 .pseudoflavor = flavor,
3222 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003223 struct rpc_auth *auth;
3224 int ret;
3225
Trond Myklebustc2190662013-08-26 19:23:04 -04003226 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003227 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003228 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003229 goto out;
3230 }
3231 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003232out:
3233 return ret;
3234}
3235
Chuck Lever9a744ba2013-03-16 15:56:02 -04003236/*
3237 * Retry pseudoroot lookup with various security flavors. We do this when:
3238 *
3239 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3240 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3241 *
3242 * Returns zero on success, or a negative NFS4ERR value, or a
3243 * negative errno value.
3244 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003245static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003246 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003248 /* Per 3530bis 15.33.5 */
3249 static const rpc_authflavor_t flav_array[] = {
3250 RPC_AUTH_GSS_KRB5P,
3251 RPC_AUTH_GSS_KRB5I,
3252 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003253 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003254 RPC_AUTH_NULL,
3255 };
3256 int status = -EPERM;
3257 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003259 if (server->auth_info.flavor_len > 0) {
3260 /* try each flavor specified by user */
3261 for (i = 0; i < server->auth_info.flavor_len; i++) {
3262 status = nfs4_lookup_root_sec(server, fhandle, info,
3263 server->auth_info.flavors[i]);
3264 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3265 continue;
3266 break;
3267 }
3268 } else {
3269 /* no flavors specified by user, try default list */
3270 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3271 status = nfs4_lookup_root_sec(server, fhandle, info,
3272 flav_array[i]);
3273 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3274 continue;
3275 break;
3276 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003277 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003278
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003279 /*
3280 * -EACCESS could mean that the user doesn't have correct permissions
3281 * to access the mount. It could also mean that we tried to mount
3282 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3283 * existing mount programs don't handle -EACCES very well so it should
3284 * be mapped to -EPERM instead.
3285 */
3286 if (status == -EACCES)
3287 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003288 return status;
3289}
3290
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003291/**
3292 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3293 * @server: initialized nfs_server handle
3294 * @fhandle: we fill in the pseudo-fs root file handle
3295 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003296 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003297 *
3298 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003299 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003300int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003301 struct nfs_fsinfo *info,
3302 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003303{
Andre Przywarac7757072015-04-23 17:17:40 +01003304 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003305
Andre Przywarac7757072015-04-23 17:17:40 +01003306 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003307 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003308
3309 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003310 status = server->nfs_client->cl_mvops->find_root_sec(server,
3311 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (status == 0)
3314 status = nfs4_server_capabilities(server, fhandle);
3315 if (status == 0)
3316 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003317
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003318 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
Bryan Schumakerbae36242012-05-10 15:07:31 -04003321static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3322 struct nfs_fsinfo *info)
3323{
3324 int error;
3325 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003326 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003327
3328 error = nfs4_server_capabilities(server, mntfh);
3329 if (error < 0) {
3330 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3331 return error;
3332 }
3333
David Quigley14c43f72013-05-22 12:50:43 -04003334 label = nfs4_label_alloc(server, GFP_KERNEL);
3335 if (IS_ERR(label))
3336 return PTR_ERR(label);
3337
David Quigley1775fd32013-05-22 12:50:42 -04003338 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003339 if (error < 0) {
3340 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003341 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003342 }
3343
3344 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3345 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3346 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3347
David Quigley14c43f72013-05-22 12:50:43 -04003348err_free_label:
3349 nfs4_label_free(label);
3350
Bryan Schumakerbae36242012-05-10 15:07:31 -04003351 return error;
3352}
3353
Manoj Naik6b97fd32006-06-09 09:34:29 -04003354/*
3355 * Get locations and (maybe) other attributes of a referral.
3356 * Note that we'll actually follow the referral later when
3357 * we detect fsid mismatch in inode revalidation
3358 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003359static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3360 const struct qstr *name, struct nfs_fattr *fattr,
3361 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003362{
3363 int status = -ENOMEM;
3364 struct page *page = NULL;
3365 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003366
3367 page = alloc_page(GFP_KERNEL);
3368 if (page == NULL)
3369 goto out;
3370 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3371 if (locations == NULL)
3372 goto out;
3373
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003374 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003375 if (status != 0)
3376 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003377
3378 /*
3379 * If the fsid didn't change, this is a migration event, not a
3380 * referral. Cause us to drop into the exception handler, which
3381 * will kick off migration recovery.
3382 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003383 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003384 dprintk("%s: server did not return a different fsid for"
3385 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003386 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003387 goto out;
3388 }
Andy Adamson533eb462011-06-13 18:25:56 -04003389 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3390 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003391
Andy Adamson533eb462011-06-13 18:25:56 -04003392 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003393 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003394 memset(fhandle, 0, sizeof(struct nfs_fh));
3395out:
3396 if (page)
3397 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003398 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003399 return status;
3400}
3401
David Quigley1775fd32013-05-22 12:50:42 -04003402static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3403 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
3405 struct nfs4_getattr_arg args = {
3406 .fh = fhandle,
3407 .bitmask = server->attr_bitmask,
3408 };
3409 struct nfs4_getattr_res res = {
3410 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003411 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 .server = server,
3413 };
3414 struct rpc_message msg = {
3415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3416 .rpc_argp = &args,
3417 .rpc_resp = &res,
3418 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003419
3420 args.bitmask = nfs4_bitmask(server, label);
3421
Trond Myklebust0e574af2005-10-27 22:12:38 -04003422 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003423 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
David Quigley1775fd32013-05-22 12:50:42 -04003426static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3427 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
3429 struct nfs4_exception exception = { };
3430 int err;
3431 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003432 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3433 trace_nfs4_getattr(server, fhandle, fattr, err);
3434 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 &exception);
3436 } while (exception.retry);
3437 return err;
3438}
3439
3440/*
3441 * The file is not closed if it is opened due to the a request to change
3442 * the size of the file. The open call will not be needed once the
3443 * VFS layer lookup-intents are implemented.
3444 *
3445 * Close is called when the inode is destroyed.
3446 * If we haven't opened the file for O_WRONLY, we
3447 * need to in the size_change case to obtain a stateid.
3448 *
3449 * Got race?
3450 * Because OPEN is always done by name in nfsv4, it is
3451 * possible that we opened a different file by the same
3452 * name. We can recognize this race condition, but we
3453 * can't do anything about it besides returning an error.
3454 *
3455 * This will be fixed with VFS changes (lookup-intent).
3456 */
3457static int
3458nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3459 struct iattr *sattr)
3460{
David Howells2b0143b2015-03-17 22:25:59 +00003461 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003462 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003463 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003464 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 int status;
3466
Peng Tao88ac8152014-09-12 11:04:10 +08003467 if (pnfs_ld_layoutret_on_setattr(inode) &&
3468 sattr->ia_valid & ATTR_SIZE &&
3469 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003470 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003471
Trond Myklebust0e574af2005-10-27 22:12:38 -04003472 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Andy Adamson26699402012-05-30 16:12:24 -04003474 /* Deal with open(O_TRUNC) */
3475 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003476 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003477
3478 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003479 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003480 return 0;
3481
Trond Myklebustd5308382005-11-04 15:33:38 -05003482 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003483 if (sattr->ia_valid & ATTR_FILE) {
3484 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003485
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003486 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003487 if (ctx) {
3488 cred = ctx->cred;
3489 state = ctx->state;
3490 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003491 }
3492
David Quigley14c43f72013-05-22 12:50:43 -04003493 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3494 if (IS_ERR(label))
3495 return PTR_ERR(label);
3496
3497 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003498 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003499 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003500 nfs_setsecurity(inode, fattr, label);
3501 }
David Quigley14c43f72013-05-22 12:50:43 -04003502 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return status;
3504}
3505
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003506static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3507 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003508 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003509{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003510 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003511 int status;
3512 struct nfs4_lookup_arg args = {
3513 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003514 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003515 .name = name,
3516 };
3517 struct nfs4_lookup_res res = {
3518 .server = server,
3519 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003520 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003521 .fh = fhandle,
3522 };
3523 struct rpc_message msg = {
3524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3525 .rpc_argp = &args,
3526 .rpc_resp = &res,
3527 };
3528
David Quigleyaa9c2662013-05-22 12:50:44 -04003529 args.bitmask = nfs4_bitmask(server, label);
3530
David Howells2b3de442006-08-22 20:06:09 -04003531 nfs_fattr_init(fattr);
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003534 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 dprintk("NFS reply lookup: %d\n", status);
3536 return status;
3537}
3538
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003539static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003540{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003541 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003542 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003543 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3544 fattr->nlink = 2;
3545}
3546
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003547static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003548 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003549 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003550{
3551 struct nfs4_exception exception = { };
3552 struct rpc_clnt *client = *clnt;
3553 int err;
3554 do {
David Quigley1775fd32013-05-22 12:50:42 -04003555 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003556 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003557 switch (err) {
3558 case -NFS4ERR_BADNAME:
3559 err = -ENOENT;
3560 goto out;
3561 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003562 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003563 if (err == -NFS4ERR_MOVED)
3564 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003565 goto out;
3566 case -NFS4ERR_WRONGSEC:
3567 err = -EPERM;
3568 if (client != *clnt)
3569 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003570 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003571 if (IS_ERR(client))
3572 return PTR_ERR(client);
3573
3574 exception.retry = 1;
3575 break;
3576 default:
3577 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3578 }
3579 } while (exception.retry);
3580
3581out:
3582 if (err == 0)
3583 *clnt = client;
3584 else if (client != *clnt)
3585 rpc_shutdown_client(client);
3586
3587 return err;
3588}
3589
Al Virobeffb8f2016-07-20 16:34:42 -04003590static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003591 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3592 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003594 int status;
3595 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003596
David Quigley1775fd32013-05-22 12:50:42 -04003597 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003598 if (client != NFS_CLIENT(dir)) {
3599 rpc_shutdown_client(client);
3600 nfs_fixup_secinfo_attributes(fattr);
3601 }
3602 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003605struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003606nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003607 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3608{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003609 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003610 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003611
David Quigley1775fd32013-05-22 12:50:42 -04003612 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003613 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003614 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003615 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003616}
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3619{
Trond Myklebust76b32992007-08-10 17:45:11 -04003620 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 struct nfs4_accessargs args = {
3622 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003623 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003625 struct nfs4_accessres res = {
3626 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003627 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 struct rpc_message msg = {
3629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3630 .rpc_argp = &args,
3631 .rpc_resp = &res,
3632 .rpc_cred = entry->cred,
3633 };
3634 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003635 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 /*
3638 * Determine which access bits we want to ask for...
3639 */
3640 if (mode & MAY_READ)
3641 args.access |= NFS4_ACCESS_READ;
3642 if (S_ISDIR(inode->i_mode)) {
3643 if (mode & MAY_WRITE)
3644 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3645 if (mode & MAY_EXEC)
3646 args.access |= NFS4_ACCESS_LOOKUP;
3647 } else {
3648 if (mode & MAY_WRITE)
3649 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3650 if (mode & MAY_EXEC)
3651 args.access |= NFS4_ACCESS_EXECUTE;
3652 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003653
3654 res.fattr = nfs_alloc_fattr();
3655 if (res.fattr == NULL)
3656 return -ENOMEM;
3657
Bryan Schumaker7c513052011-03-24 17:12:24 +00003658 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003660 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003661 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003663 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return status;
3665}
3666
3667static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3668{
3669 struct nfs4_exception exception = { };
3670 int err;
3671 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003672 err = _nfs4_proc_access(inode, entry);
3673 trace_nfs4_access(inode, err);
3674 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 &exception);
3676 } while (exception.retry);
3677 return err;
3678}
3679
3680/*
3681 * TODO: For the time being, we don't try to get any attributes
3682 * along with any of the zero-copy operations READ, READDIR,
3683 * READLINK, WRITE.
3684 *
3685 * In the case of the first three, we want to put the GETATTR
3686 * after the read-type operation -- this is because it is hard
3687 * to predict the length of a GETATTR response in v4, and thus
3688 * align the READ data correctly. This means that the GETATTR
3689 * may end up partially falling into the page cache, and we should
3690 * shift it into the 'tail' of the xdr_buf before processing.
3691 * To do this efficiently, we need to know the total length
3692 * of data received, which doesn't seem to be available outside
3693 * of the RPC layer.
3694 *
3695 * In the case of WRITE, we also want to put the GETATTR after
3696 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003697 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 *
3699 * Both of these changes to the XDR layer would in fact be quite
3700 * minor, but I decided to leave them for a subsequent patch.
3701 */
3702static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3703 unsigned int pgbase, unsigned int pglen)
3704{
3705 struct nfs4_readlink args = {
3706 .fh = NFS_FH(inode),
3707 .pgbase = pgbase,
3708 .pglen = pglen,
3709 .pages = &page,
3710 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003711 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 struct rpc_message msg = {
3713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3714 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003715 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 };
3717
Bryan Schumaker7c513052011-03-24 17:12:24 +00003718 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 -07003719}
3720
3721static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3722 unsigned int pgbase, unsigned int pglen)
3723{
3724 struct nfs4_exception exception = { };
3725 int err;
3726 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003727 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3728 trace_nfs4_readlink(inode, err);
3729 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 &exception);
3731 } while (exception.retry);
3732 return err;
3733}
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003736 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738static int
3739nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003740 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
David Quigleyaa9c2662013-05-22 12:50:44 -04003742 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003743 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 int status = 0;
3746
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003747 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3748 if (IS_ERR(ctx))
3749 return PTR_ERR(ctx);
3750
David Quigleyaa9c2662013-05-22 12:50:44 -04003751 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3752
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003753 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003754 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (IS_ERR(state)) {
3756 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003757 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003760 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003761 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return status;
3763}
3764
Al Virobeffb8f2016-07-20 16:34:42 -04003765static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Trond Myklebust16e42952005-10-27 22:12:44 -04003767 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003768 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003770 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003772 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003773 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003774 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3777 .rpc_argp = &args,
3778 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003780 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003781
Bryan Schumaker7c513052011-03-24 17:12:24 +00003782 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003783 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003784 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return status;
3786}
3787
Al Virobeffb8f2016-07-20 16:34:42 -04003788static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 struct nfs4_exception exception = { };
3791 int err;
3792 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003793 err = _nfs4_proc_remove(dir, name);
3794 trace_nfs4_remove(dir, name, err);
3795 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 &exception);
3797 } while (exception.retry);
3798 return err;
3799}
3800
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003801static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003803 struct nfs_server *server = NFS_SERVER(dir);
3804 struct nfs_removeargs *args = msg->rpc_argp;
3805 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003807 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003809 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003810
3811 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003814static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3815{
Al Viro884be172016-04-28 23:56:31 -04003816 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003817 &data->args.seq_args,
3818 &data->res.seq_res,
3819 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003822static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003824 struct nfs_unlinkdata *data = task->tk_calldata;
3825 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003826
Trond Myklebust14516c32010-07-31 14:29:06 -04003827 if (!nfs4_sequence_done(task, &res->seq_res))
3828 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003829 if (nfs4_async_handle_error(task, res->server, NULL,
3830 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003831 return 0;
3832 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003833 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835
Jeff Laytond3d41522010-09-17 17:31:57 -04003836static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3837{
3838 struct nfs_server *server = NFS_SERVER(dir);
3839 struct nfs_renameargs *arg = msg->rpc_argp;
3840 struct nfs_renameres *res = msg->rpc_resp;
3841
3842 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003843 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003844 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003845}
3846
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003847static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3848{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003849 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3850 &data->args.seq_args,
3851 &data->res.seq_res,
3852 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003853}
3854
3855static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3856 struct inode *new_dir)
3857{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003858 struct nfs_renamedata *data = task->tk_calldata;
3859 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003860
3861 if (!nfs4_sequence_done(task, &res->seq_res))
3862 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003863 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003864 return 0;
3865
3866 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003867 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003868 return 1;
3869}
3870
Al Virobeffb8f2016-07-20 16:34:42 -04003871static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003873 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 struct nfs4_link_arg arg = {
3875 .fh = NFS_FH(inode),
3876 .dir_fh = NFS_FH(dir),
3877 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003878 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003880 struct nfs4_link_res res = {
3881 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003882 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003883 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 struct rpc_message msg = {
3885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3886 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003887 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003889 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Trond Myklebust136f2622010-04-16 16:22:49 -04003891 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003892 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003893 goto out;
3894
David Quigley14c43f72013-05-22 12:50:43 -04003895 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3896 if (IS_ERR(res.label)) {
3897 status = PTR_ERR(res.label);
3898 goto out;
3899 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003900 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003901
Bryan Schumaker7c513052011-03-24 17:12:24 +00003902 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003903 if (!status) {
3904 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003905 status = nfs_post_op_update_inode(inode, res.fattr);
3906 if (!status)
3907 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003908 }
David Quigley14c43f72013-05-22 12:50:43 -04003909
3910
3911 nfs4_label_free(res.label);
3912
Trond Myklebust136f2622010-04-16 16:22:49 -04003913out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003914 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 return status;
3916}
3917
Al Virobeffb8f2016-07-20 16:34:42 -04003918static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919{
3920 struct nfs4_exception exception = { };
3921 int err;
3922 do {
3923 err = nfs4_handle_exception(NFS_SERVER(inode),
3924 _nfs4_proc_link(inode, dir, name),
3925 &exception);
3926 } while (exception.retry);
3927 return err;
3928}
3929
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003930struct nfs4_createdata {
3931 struct rpc_message msg;
3932 struct nfs4_create_arg arg;
3933 struct nfs4_create_res res;
3934 struct nfs_fh fh;
3935 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003936 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003937};
3938
3939static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003940 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003941{
3942 struct nfs4_createdata *data;
3943
3944 data = kzalloc(sizeof(*data), GFP_KERNEL);
3945 if (data != NULL) {
3946 struct nfs_server *server = NFS_SERVER(dir);
3947
David Quigley14c43f72013-05-22 12:50:43 -04003948 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3949 if (IS_ERR(data->label))
3950 goto out_free;
3951
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003952 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3953 data->msg.rpc_argp = &data->arg;
3954 data->msg.rpc_resp = &data->res;
3955 data->arg.dir_fh = NFS_FH(dir);
3956 data->arg.server = server;
3957 data->arg.name = name;
3958 data->arg.attrs = sattr;
3959 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003960 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003961 data->res.server = server;
3962 data->res.fh = &data->fh;
3963 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003964 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003965 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003966 }
3967 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003968out_free:
3969 kfree(data);
3970 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003971}
3972
3973static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3974{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003975 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003976 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003977 if (status == 0) {
3978 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003979 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003980 }
3981 return status;
3982}
3983
3984static void nfs4_free_createdata(struct nfs4_createdata *data)
3985{
David Quigley14c43f72013-05-22 12:50:43 -04003986 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003987 kfree(data);
3988}
3989
Chuck Lever4f390c12006-08-22 20:06:22 -04003990static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003991 struct page *page, unsigned int len, struct iattr *sattr,
3992 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003994 struct nfs4_createdata *data;
3995 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Chuck Lever94a6d752006-08-22 20:06:23 -04003997 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003998 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003999
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004000 status = -ENOMEM;
4001 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4002 if (data == NULL)
4003 goto out;
4004
4005 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4006 data->arg.u.symlink.pages = &page;
4007 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004008 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004010 status = nfs4_do_create(dir, dentry, data);
4011
4012 nfs4_free_createdata(data);
4013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 return status;
4015}
4016
Chuck Lever4f390c12006-08-22 20:06:22 -04004017static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004018 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004021 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004023
4024 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004027 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4028 trace_nfs4_symlink(dir, &dentry->d_name, err);
4029 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 &exception);
4031 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004032
4033 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return err;
4035}
4036
4037static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004038 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004040 struct nfs4_createdata *data;
4041 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004043 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4044 if (data == NULL)
4045 goto out;
4046
David Quigley1775fd32013-05-22 12:50:42 -04004047 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004048 status = nfs4_do_create(dir, dentry, data);
4049
4050 nfs4_free_createdata(data);
4051out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 return status;
4053}
4054
4055static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4056 struct iattr *sattr)
4057{
4058 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004059 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004061
David Quigleyaa9c2662013-05-22 12:50:44 -04004062 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4063
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004064 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004066 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4067 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4068 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 &exception);
4070 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004071 nfs4_label_release_security(label);
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return err;
4074}
4075
4076static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004077 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
David Howells2b0143b2015-03-17 22:25:59 +00004079 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 struct nfs4_readdir_arg args = {
4081 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004082 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 .pgbase = 0,
4084 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004085 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004086 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 };
4088 struct nfs4_readdir_res res;
4089 struct rpc_message msg = {
4090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4091 .rpc_argp = &args,
4092 .rpc_resp = &res,
4093 .rpc_cred = cred,
4094 };
4095 int status;
4096
Al Viro6de14722013-09-16 10:53:17 -04004097 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4098 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004099 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004100 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004102 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 -05004103 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004104 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004105 status += args.pgbase;
4106 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004107
4108 nfs_invalidate_atime(dir);
4109
Harvey Harrison3110ff82008-05-02 13:42:44 -07004110 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 return status;
4112}
4113
4114static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004115 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
4117 struct nfs4_exception exception = { };
4118 int err;
4119 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004120 err = _nfs4_proc_readdir(dentry, cred, cookie,
4121 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004122 trace_nfs4_readdir(d_inode(dentry), err);
4123 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 &exception);
4125 } while (exception.retry);
4126 return err;
4127}
4128
4129static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004130 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004132 struct nfs4_createdata *data;
4133 int mode = sattr->ia_mode;
4134 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004136 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4137 if (data == NULL)
4138 goto out;
4139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004141 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004143 data->arg.ftype = NF4BLK;
4144 data->arg.u.device.specdata1 = MAJOR(rdev);
4145 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
4147 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004148 data->arg.ftype = NF4CHR;
4149 data->arg.u.device.specdata1 = MAJOR(rdev);
4150 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004151 } else if (!S_ISSOCK(mode)) {
4152 status = -EINVAL;
4153 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 }
David Quigley1775fd32013-05-22 12:50:42 -04004155
David Quigleyaa9c2662013-05-22 12:50:44 -04004156 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004158out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 nfs4_free_createdata(data);
4160out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return status;
4162}
4163
4164static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4165 struct iattr *sattr, dev_t rdev)
4166{
4167 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004168 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004170
David Quigleyaa9c2662013-05-22 12:50:44 -04004171 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4172
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004173 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004175 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4176 trace_nfs4_mknod(dir, &dentry->d_name, err);
4177 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 &exception);
4179 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004180
4181 nfs4_label_release_security(label);
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return err;
4184}
4185
4186static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4187 struct nfs_fsstat *fsstat)
4188{
4189 struct nfs4_statfs_arg args = {
4190 .fh = fhandle,
4191 .bitmask = server->attr_bitmask,
4192 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004193 struct nfs4_statfs_res res = {
4194 .fsstat = fsstat,
4195 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 struct rpc_message msg = {
4197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4198 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004199 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 };
4201
Trond Myklebust0e574af2005-10-27 22:12:38 -04004202 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004203 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
4206static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4207{
4208 struct nfs4_exception exception = { };
4209 int err;
4210 do {
4211 err = nfs4_handle_exception(server,
4212 _nfs4_proc_statfs(server, fhandle, fsstat),
4213 &exception);
4214 } while (exception.retry);
4215 return err;
4216}
4217
4218static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4219 struct nfs_fsinfo *fsinfo)
4220{
4221 struct nfs4_fsinfo_arg args = {
4222 .fh = fhandle,
4223 .bitmask = server->attr_bitmask,
4224 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004225 struct nfs4_fsinfo_res res = {
4226 .fsinfo = fsinfo,
4227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 struct rpc_message msg = {
4229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4230 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004231 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 };
4233
Bryan Schumaker7c513052011-03-24 17:12:24 +00004234 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
4237static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4238{
4239 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004240 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 int err;
4242
4243 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004244 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004245 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004246 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004247 nfs4_set_lease_period(server->nfs_client,
4248 fsinfo->lease_time * HZ,
4249 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004250 break;
4251 }
4252 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 } while (exception.retry);
4254 return err;
4255}
4256
4257static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4258{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004259 int error;
4260
Trond Myklebust0e574af2005-10-27 22:12:38 -04004261 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004262 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004263 if (error == 0) {
4264 /* block layout checks this! */
4265 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004266 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004267 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004268
4269 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270}
4271
4272static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4273 struct nfs_pathconf *pathconf)
4274{
4275 struct nfs4_pathconf_arg args = {
4276 .fh = fhandle,
4277 .bitmask = server->attr_bitmask,
4278 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004279 struct nfs4_pathconf_res res = {
4280 .pathconf = pathconf,
4281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 struct rpc_message msg = {
4283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4284 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004285 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
4287
4288 /* None of the pathconf attributes are mandatory to implement */
4289 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4290 memset(pathconf, 0, sizeof(*pathconf));
4291 return 0;
4292 }
4293
Trond Myklebust0e574af2005-10-27 22:12:38 -04004294 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004295 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4299 struct nfs_pathconf *pathconf)
4300{
4301 struct nfs4_exception exception = { };
4302 int err;
4303
4304 do {
4305 err = nfs4_handle_exception(server,
4306 _nfs4_proc_pathconf(server, fhandle, pathconf),
4307 &exception);
4308 } while (exception.retry);
4309 return err;
4310}
4311
Trond Myklebust5521abf2013-03-16 20:54:34 -04004312int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004313 const struct nfs_open_context *ctx,
4314 const struct nfs_lock_context *l_ctx,
4315 fmode_t fmode)
4316{
4317 const struct nfs_lockowner *lockowner = NULL;
4318
4319 if (l_ctx != NULL)
4320 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004321 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004322}
4323EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4324
Trond Myklebust5521abf2013-03-16 20:54:34 -04004325static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4326 const struct nfs_open_context *ctx,
4327 const struct nfs_lock_context *l_ctx,
4328 fmode_t fmode)
4329{
4330 nfs4_stateid current_stateid;
4331
Trond Myklebuste1253be2014-03-05 08:44:23 -05004332 /* If the current stateid represents a lost lock, then exit */
4333 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4334 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004335 return nfs4_stateid_match(stateid, &current_stateid);
4336}
4337
4338static bool nfs4_error_stateid_expired(int err)
4339{
4340 switch (err) {
4341 case -NFS4ERR_DELEG_REVOKED:
4342 case -NFS4ERR_ADMIN_REVOKED:
4343 case -NFS4ERR_BAD_STATEID:
4344 case -NFS4ERR_STALE_STATEID:
4345 case -NFS4ERR_OLD_STATEID:
4346 case -NFS4ERR_OPENMODE:
4347 case -NFS4ERR_EXPIRED:
4348 return true;
4349 }
4350 return false;
4351}
4352
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004353void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004354{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004355 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004356}
4357
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004358static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004360 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004362 trace_nfs4_read(hdr, task->tk_status);
4363 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004364 hdr->args.context->state,
4365 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004366 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004367 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004369
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004370 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004372 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374}
4375
Trond Myklebust5521abf2013-03-16 20:54:34 -04004376static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004377 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004378{
4379
4380 if (!nfs4_error_stateid_expired(task->tk_status) ||
4381 nfs4_stateid_is_current(&args->stateid,
4382 args->context,
4383 args->lock_context,
4384 FMODE_READ))
4385 return false;
4386 rpc_restart_call_prepare(task);
4387 return true;
4388}
4389
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004390static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004391{
4392
4393 dprintk("--> %s\n", __func__);
4394
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004395 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004396 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004397 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004398 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004399 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4400 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004401}
4402
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004403static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4404 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004406 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004407 if (!hdr->pgio_done_cb)
4408 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004409 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004410 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004413static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4414 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004415{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004416 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4417 &hdr->args.seq_args,
4418 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004419 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004420 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004421 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4422 hdr->args.lock_context,
4423 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004424 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004425 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004426 return -EIO;
4427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004430static int nfs4_write_done_cb(struct rpc_task *task,
4431 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004433 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004434
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004435 trace_nfs4_write(hdr, task->tk_status);
4436 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004437 hdr->args.context->state,
4438 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004439 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004440 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004442 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004443 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004444 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004445 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Trond Myklebust5521abf2013-03-16 20:54:34 -04004449static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004450 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004451{
4452
4453 if (!nfs4_error_stateid_expired(task->tk_status) ||
4454 nfs4_stateid_is_current(&args->stateid,
4455 args->context,
4456 args->lock_context,
4457 FMODE_WRITE))
4458 return false;
4459 rpc_restart_call_prepare(task);
4460 return true;
4461}
4462
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004463static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004464{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004466 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004467 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004468 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004469 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4470 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004471}
4472
Trond Myklebust5a37f852012-04-28 14:55:16 -04004473static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004474bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004475{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004476 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004477 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004478 return false;
4479 /* Otherwise, request attributes if and only if we don't hold
4480 * a delegation
4481 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004482 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004483}
Fred Isamana69aef12011-03-03 15:13:47 +00004484
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004485static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4486 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004488 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004489
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004490 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4491 hdr->args.bitmask = NULL;
4492 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004493 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004494 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004495
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004496 if (!hdr->pgio_done_cb)
4497 hdr->pgio_done_cb = nfs4_write_done_cb;
4498 hdr->res.server = server;
4499 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004501 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004502 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
Fred Isaman0b7c0152012-04-20 14:47:39 -04004505static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4506{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004507 nfs4_setup_sequence(NFS_SERVER(data->inode),
4508 &data->args.seq_args,
4509 &data->res.seq_res,
4510 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
Fred Isaman0b7c0152012-04-20 14:47:39 -04004513static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004516
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004517 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004518 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4519 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004520 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004521 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
Fred Isaman0b7c0152012-04-20 14:47:39 -04004526static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004527{
4528 if (!nfs4_sequence_done(task, &data->res.seq_res))
4529 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004530 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004531}
4532
Fred Isaman0b7c0152012-04-20 14:47:39 -04004533static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004535 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004536
Fred Isaman0b7c0152012-04-20 14:47:39 -04004537 if (data->commit_done_cb == NULL)
4538 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004539 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004540 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004541 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542}
4543
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004544struct nfs4_renewdata {
4545 struct nfs_client *client;
4546 unsigned long timestamp;
4547};
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549/*
4550 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4551 * standalone procedure for queueing an asynchronous RENEW.
4552 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004553static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004554{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004555 struct nfs4_renewdata *data = calldata;
4556 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004557
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004558 if (atomic_read(&clp->cl_count) > 1)
4559 nfs4_schedule_state_renewal(clp);
4560 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004561 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004562}
4563
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004564static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004566 struct nfs4_renewdata *data = calldata;
4567 struct nfs_client *clp = data->client;
4568 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004570 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004571 switch (task->tk_status) {
4572 case 0:
4573 break;
4574 case -NFS4ERR_LEASE_MOVED:
4575 nfs4_schedule_lease_moved_recovery(clp);
4576 break;
4577 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004578 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004579 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4580 return;
4581 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004582 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004583 return;
4584 }
4585 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004587 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004590static const struct rpc_call_ops nfs4_renew_ops = {
4591 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004592 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004593};
4594
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004595static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 struct rpc_message msg = {
4598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4599 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004600 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004602 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004604 if (renew_flags == 0)
4605 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004606 if (!atomic_inc_not_zero(&clp->cl_count))
4607 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004608 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004609 if (data == NULL)
4610 return -ENOMEM;
4611 data->client = clp;
4612 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004613 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004614 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004617static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
4619 struct rpc_message msg = {
4620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4621 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004622 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 };
4624 unsigned long now = jiffies;
4625 int status;
4626
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004627 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (status < 0)
4629 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004630 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return 0;
4632}
4633
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004634static inline int nfs4_server_supports_acls(struct nfs_server *server)
4635{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004636 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004637}
4638
Trond Myklebust21f498c2012-08-24 10:59:25 -04004639/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4640 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004641 * the stack.
4642 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004643#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004644
Neil Hormane9e3d722011-03-04 19:26:03 -05004645static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004646 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004647{
4648 struct page *newpage, **spages;
4649 int rc = 0;
4650 size_t len;
4651 spages = pages;
4652
4653 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004654 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004655 newpage = alloc_page(GFP_KERNEL);
4656
4657 if (newpage == NULL)
4658 goto unwind;
4659 memcpy(page_address(newpage), buf, len);
4660 buf += len;
4661 buflen -= len;
4662 *pages++ = newpage;
4663 rc++;
4664 } while (buflen != 0);
4665
4666 return rc;
4667
4668unwind:
4669 for(; rc > 0; rc--)
4670 __free_page(spages[rc-1]);
4671 return -ENOMEM;
4672}
4673
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004674struct nfs4_cached_acl {
4675 int cached;
4676 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004677 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004678};
4679
4680static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004681{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004682 struct nfs_inode *nfsi = NFS_I(inode);
4683
4684 spin_lock(&inode->i_lock);
4685 kfree(nfsi->nfs4_acl);
4686 nfsi->nfs4_acl = acl;
4687 spin_unlock(&inode->i_lock);
4688}
4689
4690static void nfs4_zap_acl_attr(struct inode *inode)
4691{
4692 nfs4_set_cached_acl(inode, NULL);
4693}
4694
4695static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4696{
4697 struct nfs_inode *nfsi = NFS_I(inode);
4698 struct nfs4_cached_acl *acl;
4699 int ret = -ENOENT;
4700
4701 spin_lock(&inode->i_lock);
4702 acl = nfsi->nfs4_acl;
4703 if (acl == NULL)
4704 goto out;
4705 if (buf == NULL) /* user is just asking for length */
4706 goto out_len;
4707 if (acl->cached == 0)
4708 goto out;
4709 ret = -ERANGE; /* see getxattr(2) man page */
4710 if (acl->len > buflen)
4711 goto out;
4712 memcpy(buf, acl->data, acl->len);
4713out_len:
4714 ret = acl->len;
4715out:
4716 spin_unlock(&inode->i_lock);
4717 return ret;
4718}
4719
Sachin Prabhu5794d212012-04-17 14:36:40 +01004720static 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 +00004721{
4722 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004723 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004724
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004725 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004726 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004727 if (acl == NULL)
4728 goto out;
4729 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004730 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004731 } else {
4732 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4733 if (acl == NULL)
4734 goto out;
4735 acl->cached = 0;
4736 }
4737 acl->len = acl_len;
4738out:
4739 nfs4_set_cached_acl(inode, acl);
4740}
4741
Andy Adamsonbf118a32011-12-07 11:55:27 -05004742/*
4743 * The getxattr API returns the required buffer length when called with a
4744 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4745 * the required buf. On a NULL buf, we send a page of data to the server
4746 * guessing that the ACL request can be serviced by a page. If so, we cache
4747 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4748 * the cache. If not so, we throw away the page, and cache the required
4749 * length. The next getxattr call will then produce another round trip to
4750 * the server, this time with the input buf of the required size.
4751 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004752static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004753{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004754 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004755 struct nfs_getaclargs args = {
4756 .fh = NFS_FH(inode),
4757 .acl_pages = pages,
4758 .acl_len = buflen,
4759 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004760 struct nfs_getaclres res = {
4761 .acl_len = buflen,
4762 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004763 struct rpc_message msg = {
4764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4765 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004766 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004767 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004768 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4769 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004770
Andy Adamsonbf118a32011-12-07 11:55:27 -05004771 /* As long as we're doing a round trip to the server anyway,
4772 * let's be prepared for a page of acl data. */
4773 if (npages == 0)
4774 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004775 if (npages > ARRAY_SIZE(pages))
4776 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004777
Andy Adamsonbf118a32011-12-07 11:55:27 -05004778 for (i = 0; i < npages; i++) {
4779 pages[i] = alloc_page(GFP_KERNEL);
4780 if (!pages[i])
4781 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004782 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004783
4784 /* for decoding across pages */
4785 res.acl_scratch = alloc_page(GFP_KERNEL);
4786 if (!res.acl_scratch)
4787 goto out_free;
4788
Andy Adamsonbf118a32011-12-07 11:55:27 -05004789 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004790
Peng Taode040be2012-01-10 22:42:47 +08004791 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004792 __func__, buf, buflen, npages, args.acl_len);
4793 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4794 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004795 if (ret)
4796 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004797
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004798 /* Handle the case where the passed-in buffer is too short */
4799 if (res.acl_flags & NFS4_ACL_TRUNC) {
4800 /* Did the user only issue a request for the acl length? */
4801 if (buf == NULL)
4802 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004803 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004804 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004805 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004806 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004807 if (buf) {
4808 if (res.acl_len > buflen) {
4809 ret = -ERANGE;
4810 goto out_free;
4811 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004812 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004813 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004814out_ok:
4815 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004816out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004817 for (i = 0; i < npages; i++)
4818 if (pages[i])
4819 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004820 if (res.acl_scratch)
4821 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004822 return ret;
4823}
4824
Trond Myklebust16b42892006-08-24 12:27:15 -04004825static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4826{
4827 struct nfs4_exception exception = { };
4828 ssize_t ret;
4829 do {
4830 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004831 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004832 if (ret >= 0)
4833 break;
4834 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4835 } while (exception.retry);
4836 return ret;
4837}
4838
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004839static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4840{
4841 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004842 int ret;
4843
4844 if (!nfs4_server_supports_acls(server))
4845 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004846 ret = nfs_revalidate_inode(server, inode);
4847 if (ret < 0)
4848 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004849 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4850 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004851 ret = nfs4_read_cached_acl(inode, buf, buflen);
4852 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004853 /* -ENOENT is returned if there is no ACL or if there is an ACL
4854 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004855 return ret;
4856 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004857}
4858
Trond Myklebust16b42892006-08-24 12:27:15 -04004859static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004860{
4861 struct nfs_server *server = NFS_SERVER(inode);
4862 struct page *pages[NFS4ACL_MAXPAGES];
4863 struct nfs_setaclargs arg = {
4864 .fh = NFS_FH(inode),
4865 .acl_pages = pages,
4866 .acl_len = buflen,
4867 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004868 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004869 struct rpc_message msg = {
4870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4871 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004872 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004873 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004874 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004875 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004876
4877 if (!nfs4_server_supports_acls(server))
4878 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004879 if (npages > ARRAY_SIZE(pages))
4880 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004881 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004882 if (i < 0)
4883 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004884 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004885 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004886
4887 /*
4888 * Free each page after tx, so the only ref left is
4889 * held by the network stack
4890 */
4891 for (; i > 0; i--)
4892 put_page(pages[i-1]);
4893
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004894 /*
4895 * Acl update can result in inode attribute update.
4896 * so mark the attribute cache invalid.
4897 */
4898 spin_lock(&inode->i_lock);
4899 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4900 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004901 nfs_access_zap_cache(inode);
4902 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004903 return ret;
4904}
4905
Trond Myklebust16b42892006-08-24 12:27:15 -04004906static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4907{
4908 struct nfs4_exception exception = { };
4909 int err;
4910 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004911 err = __nfs4_proc_set_acl(inode, buf, buflen);
4912 trace_nfs4_set_acl(inode, err);
4913 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004914 &exception);
4915 } while (exception.retry);
4916 return err;
4917}
4918
David Quigleyaa9c2662013-05-22 12:50:44 -04004919#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4920static int _nfs4_get_security_label(struct inode *inode, void *buf,
4921 size_t buflen)
4922{
4923 struct nfs_server *server = NFS_SERVER(inode);
4924 struct nfs_fattr fattr;
4925 struct nfs4_label label = {0, 0, buflen, buf};
4926
4927 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004928 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004929 .fh = NFS_FH(inode),
4930 .bitmask = bitmask,
4931 };
4932 struct nfs4_getattr_res res = {
4933 .fattr = &fattr,
4934 .label = &label,
4935 .server = server,
4936 };
4937 struct rpc_message msg = {
4938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004939 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004940 .rpc_resp = &res,
4941 };
4942 int ret;
4943
4944 nfs_fattr_init(&fattr);
4945
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004946 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004947 if (ret)
4948 return ret;
4949 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4950 return -ENOENT;
4951 if (buflen < label.len)
4952 return -ERANGE;
4953 return 0;
4954}
4955
4956static int nfs4_get_security_label(struct inode *inode, void *buf,
4957 size_t buflen)
4958{
4959 struct nfs4_exception exception = { };
4960 int err;
4961
4962 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4963 return -EOPNOTSUPP;
4964
4965 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004966 err = _nfs4_get_security_label(inode, buf, buflen);
4967 trace_nfs4_get_security_label(inode, err);
4968 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004969 &exception);
4970 } while (exception.retry);
4971 return err;
4972}
4973
4974static int _nfs4_do_set_security_label(struct inode *inode,
4975 struct nfs4_label *ilabel,
4976 struct nfs_fattr *fattr,
4977 struct nfs4_label *olabel)
4978{
4979
4980 struct iattr sattr = {0};
4981 struct nfs_server *server = NFS_SERVER(inode);
4982 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004983 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004984 .fh = NFS_FH(inode),
4985 .iap = &sattr,
4986 .server = server,
4987 .bitmask = bitmask,
4988 .label = ilabel,
4989 };
4990 struct nfs_setattrres res = {
4991 .fattr = fattr,
4992 .label = olabel,
4993 .server = server,
4994 };
4995 struct rpc_message msg = {
4996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004997 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004998 .rpc_resp = &res,
4999 };
5000 int status;
5001
Jeff Layton12207f62013-11-01 10:49:32 -04005002 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005003
Jeff Layton12207f62013-11-01 10:49:32 -04005004 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005005 if (status)
5006 dprintk("%s failed: %d\n", __func__, status);
5007
5008 return status;
5009}
5010
5011static int nfs4_do_set_security_label(struct inode *inode,
5012 struct nfs4_label *ilabel,
5013 struct nfs_fattr *fattr,
5014 struct nfs4_label *olabel)
5015{
5016 struct nfs4_exception exception = { };
5017 int err;
5018
5019 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005020 err = _nfs4_do_set_security_label(inode, ilabel,
5021 fattr, olabel);
5022 trace_nfs4_set_security_label(inode, err);
5023 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005024 &exception);
5025 } while (exception.retry);
5026 return err;
5027}
5028
5029static int
Al Viro59301222016-05-27 10:19:30 -04005030nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005031{
5032 struct nfs4_label ilabel, *olabel = NULL;
5033 struct nfs_fattr fattr;
5034 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005035 int status;
5036
5037 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5038 return -EOPNOTSUPP;
5039
5040 nfs_fattr_init(&fattr);
5041
5042 ilabel.pi = 0;
5043 ilabel.lfs = 0;
5044 ilabel.label = (char *)buf;
5045 ilabel.len = buflen;
5046
5047 cred = rpc_lookup_cred();
5048 if (IS_ERR(cred))
5049 return PTR_ERR(cred);
5050
5051 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5052 if (IS_ERR(olabel)) {
5053 status = -PTR_ERR(olabel);
5054 goto out;
5055 }
5056
5057 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5058 if (status == 0)
5059 nfs_setsecurity(inode, &fattr, olabel);
5060
5061 nfs4_label_free(olabel);
5062out:
5063 put_rpccred(cred);
5064 return status;
5065}
5066#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5067
5068
Chuck Leverf0920752012-05-21 22:45:41 -04005069static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5070 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005071{
5072 __be32 verf[2];
5073
Chuck Lever2c820d92012-05-21 22:45:33 -04005074 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5075 /* An impossible timestamp guarantees this value
5076 * will never match a generated boot time. */
5077 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005078 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005079 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005080 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005081 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5082 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005083 }
Chuck Levercd937102012-03-02 17:14:31 -05005084 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5085}
5086
Jeff Laytona3192682015-06-09 19:43:59 -04005087static int
5088nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005089{
Jeff Laytona3192682015-06-09 19:43:59 -04005090 size_t len;
5091 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005092
Trond Myklebustceb3a162015-01-03 15:16:04 -05005093 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005094 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005095
Jeff Laytona3192682015-06-09 19:43:59 -04005096 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005097 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005098 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5099 1 +
5100 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5101 1;
5102 rcu_read_unlock();
5103
5104 if (len > NFS4_OPAQUE_LIMIT + 1)
5105 return -EINVAL;
5106
5107 /*
5108 * Since this string is allocated at mount time, and held until the
5109 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5110 * about a memory-reclaim deadlock.
5111 */
5112 str = kmalloc(len, GFP_KERNEL);
5113 if (!str)
5114 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005115
Chuck Levere984a552012-09-14 17:24:21 -04005116 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005117 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005118 clp->cl_ipaddr,
5119 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5120 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005121 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005122
Jeff Laytona3192682015-06-09 19:43:59 -04005123 clp->cl_owner_id = str;
5124 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005125}
5126
Jeff Layton873e3852015-06-09 19:44:00 -04005127static int
5128nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005129{
Jeff Layton873e3852015-06-09 19:44:00 -04005130 size_t len;
5131 char *str;
5132
5133 len = 10 + 10 + 1 + 10 + 1 +
5134 strlen(nfs4_client_id_uniquifier) + 1 +
5135 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5136
5137 if (len > NFS4_OPAQUE_LIMIT + 1)
5138 return -EINVAL;
5139
5140 /*
5141 * Since this string is allocated at mount time, and held until the
5142 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5143 * about a memory-reclaim deadlock.
5144 */
5145 str = kmalloc(len, GFP_KERNEL);
5146 if (!str)
5147 return -ENOMEM;
5148
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005149 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005150 clp->rpc_ops->version, clp->cl_minorversion,
5151 nfs4_client_id_uniquifier,
5152 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005153 clp->cl_owner_id = str;
5154 return 0;
5155}
5156
5157static int
5158nfs4_init_uniform_client_string(struct nfs_client *clp)
5159{
Jeff Layton873e3852015-06-09 19:44:00 -04005160 size_t len;
5161 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005162
5163 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005164 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005165
5166 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005167 return nfs4_init_uniquifier_client_string(clp);
5168
5169 len = 10 + 10 + 1 + 10 + 1 +
5170 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5171
5172 if (len > NFS4_OPAQUE_LIMIT + 1)
5173 return -EINVAL;
5174
5175 /*
5176 * Since this string is allocated at mount time, and held until the
5177 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5178 * about a memory-reclaim deadlock.
5179 */
5180 str = kmalloc(len, GFP_KERNEL);
5181 if (!str)
5182 return -ENOMEM;
5183
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005184 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005185 clp->rpc_ops->version, clp->cl_minorversion,
5186 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005187 clp->cl_owner_id = str;
5188 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005189}
5190
Chuck Lever706cb8d2014-03-12 12:51:47 -04005191/*
5192 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5193 * services. Advertise one based on the address family of the
5194 * clientaddr.
5195 */
5196static unsigned int
5197nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5198{
5199 if (strchr(clp->cl_ipaddr, ':') != NULL)
5200 return scnprintf(buf, len, "tcp6");
5201 else
5202 return scnprintf(buf, len, "tcp");
5203}
5204
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005205static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5206{
5207 struct nfs4_setclientid *sc = calldata;
5208
5209 if (task->tk_status == 0)
5210 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5211}
5212
5213static const struct rpc_call_ops nfs4_setclientid_ops = {
5214 .rpc_call_done = nfs4_setclientid_done,
5215};
5216
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005217/**
5218 * nfs4_proc_setclientid - Negotiate client ID
5219 * @clp: state data structure
5220 * @program: RPC program for NFSv4 callback service
5221 * @port: IP port number for NFS4 callback service
5222 * @cred: RPC credential to use for this call
5223 * @res: where to place the result
5224 *
5225 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5226 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005227int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5228 unsigned short port, struct rpc_cred *cred,
5229 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230{
5231 nfs4_verifier sc_verifier;
5232 struct nfs4_setclientid setclientid = {
5233 .sc_verifier = &sc_verifier,
5234 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005235 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 };
5237 struct rpc_message msg = {
5238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5239 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005240 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005241 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005243 struct rpc_task *task;
5244 struct rpc_task_setup task_setup_data = {
5245 .rpc_client = clp->cl_rpcclient,
5246 .rpc_message = &msg,
5247 .callback_ops = &nfs4_setclientid_ops,
5248 .callback_data = &setclientid,
5249 .flags = RPC_TASK_TIMEOUT,
5250 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005251 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Chuck Leverde734832012-07-11 16:30:50 -04005253 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005254 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005255
5256 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5257 status = nfs4_init_uniform_client_string(clp);
5258 else
Jeff Laytona3192682015-06-09 19:43:59 -04005259 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005260
5261 if (status)
5262 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005263
Chuck Leverde734832012-07-11 16:30:50 -04005264 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005265 setclientid.sc_netid_len =
5266 nfs4_init_callback_netid(clp,
5267 setclientid.sc_netid,
5268 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005269 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005270 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 clp->cl_ipaddr, port >> 8, port & 255);
5272
Jeff Layton3a6bb732015-06-09 19:43:57 -04005273 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005274 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005275 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005276 task = rpc_run_task(&task_setup_data);
5277 if (IS_ERR(task)) {
5278 status = PTR_ERR(task);
5279 goto out;
5280 }
5281 status = task->tk_status;
5282 if (setclientid.sc_cred) {
5283 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5284 put_rpccred(setclientid.sc_cred);
5285 }
5286 rpc_put_task(task);
5287out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005288 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005289 dprintk("NFS reply setclientid: %d\n", status);
5290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005293/**
5294 * nfs4_proc_setclientid_confirm - Confirm client ID
5295 * @clp: state data structure
5296 * @res: result of a previous SETCLIENTID
5297 * @cred: RPC credential to use for this call
5298 *
5299 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5300 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005301int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005302 struct nfs4_setclientid_res *arg,
5303 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 struct rpc_message msg = {
5306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005307 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005308 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 int status;
5311
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005312 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5313 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5314 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005315 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005316 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005317 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 return status;
5319}
5320
Trond Myklebustfe650402006-01-03 09:55:18 +01005321struct nfs4_delegreturndata {
5322 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005323 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005324 struct nfs_fh fh;
5325 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005326 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005327 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005328 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005329 struct inode *inode;
5330 bool roc;
5331 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005332};
5333
Trond Myklebustfe650402006-01-03 09:55:18 +01005334static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5335{
5336 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005337
Trond Myklebust14516c32010-07-31 14:29:06 -04005338 if (!nfs4_sequence_done(task, &data->res.seq_res))
5339 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005340
Trond Myklebustca8acf82013-08-13 10:36:56 -04005341 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005342 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005343 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005344 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005345 case -NFS4ERR_ADMIN_REVOKED:
5346 case -NFS4ERR_DELEG_REVOKED:
5347 case -NFS4ERR_BAD_STATEID:
5348 case -NFS4ERR_OLD_STATEID:
5349 case -NFS4ERR_STALE_STATEID:
5350 case -NFS4ERR_EXPIRED:
5351 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005352 if (data->roc)
5353 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005354 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005355 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005356 if (nfs4_async_handle_error(task, data->res.server,
5357 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005358 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005359 return;
5360 }
5361 }
5362 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005363}
5364
5365static void nfs4_delegreturn_release(void *calldata)
5366{
Peng Tao039b7562014-07-03 13:05:02 +08005367 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005368 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005369
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005370 if (inode) {
5371 if (data->roc)
5372 pnfs_roc_release(inode);
5373 nfs_iput_and_deactive(inode);
5374 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005375 kfree(calldata);
5376}
5377
Andy Adamson938e1012009-04-01 09:22:28 -04005378static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5379{
5380 struct nfs4_delegreturndata *d_data;
5381
5382 d_data = (struct nfs4_delegreturndata *)data;
5383
Peng Tao500d7012015-09-22 11:35:22 +08005384 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5385 return;
5386
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005387 if (d_data->roc)
5388 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005389
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005390 nfs4_setup_sequence(d_data->res.server,
5391 &d_data->args.seq_args,
5392 &d_data->res.seq_res,
5393 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005394}
Andy Adamson938e1012009-04-01 09:22:28 -04005395
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005396static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005397 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005398 .rpc_call_done = nfs4_delegreturn_done,
5399 .rpc_release = nfs4_delegreturn_release,
5400};
5401
Trond Myklebuste6f81072008-01-24 18:14:34 -05005402static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005403{
5404 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005405 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005406 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005407 struct rpc_message msg = {
5408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5409 .rpc_cred = cred,
5410 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005411 struct rpc_task_setup task_setup_data = {
5412 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005413 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005414 .callback_ops = &nfs4_delegreturn_ops,
5415 .flags = RPC_TASK_ASYNC,
5416 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005417 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005418
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005419 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005420 if (data == NULL)
5421 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005422 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005423
5424 nfs4_state_protect(server->nfs_client,
5425 NFS_SP4_MACH_CRED_CLEANUP,
5426 &task_setup_data.rpc_client, &msg);
5427
Trond Myklebustfe650402006-01-03 09:55:18 +01005428 data->args.fhandle = &data->fh;
5429 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005430 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005431 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005432 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005433 data->res.fattr = &data->fattr;
5434 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005435 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005436 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005437 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005438 data->inode = nfs_igrab_and_active(inode);
5439 if (data->inode)
5440 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005441
Trond Myklebustc970aa82007-07-14 15:39:59 -04005442 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005443 msg.rpc_argp = &data->args;
5444 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005445 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005446 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005447 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005448 if (!issync)
5449 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005450 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005451 if (status != 0)
5452 goto out;
5453 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005454 if (status == 0)
5455 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5456 else
5457 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005458out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005459 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005460 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
Trond Myklebuste6f81072008-01-24 18:14:34 -05005463int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
5465 struct nfs_server *server = NFS_SERVER(inode);
5466 struct nfs4_exception exception = { };
5467 int err;
5468 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005469 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005470 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 switch (err) {
5472 case -NFS4ERR_STALE_STATEID:
5473 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 case 0:
5475 return 0;
5476 }
5477 err = nfs4_handle_exception(server, err, &exception);
5478 } while (exception.retry);
5479 return err;
5480}
5481
5482#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5483#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5484
5485/*
5486 * sleep, with exponential backoff, and retry the LOCK operation.
5487 */
5488static unsigned long
5489nfs4_set_lock_task_retry(unsigned long timeout)
5490{
Colin Cross416ad3c2013-05-06 23:50:06 +00005491 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 timeout <<= 1;
5493 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5494 return NFS4_LOCK_MAXTIMEOUT;
5495 return timeout;
5496}
5497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5499{
5500 struct inode *inode = state->inode;
5501 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005502 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005503 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005505 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005507 struct nfs_lockt_res res = {
5508 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 };
5510 struct rpc_message msg = {
5511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5512 .rpc_argp = &arg,
5513 .rpc_resp = &res,
5514 .rpc_cred = state->owner->so_cred,
5515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 struct nfs4_lock_state *lsp;
5517 int status;
5518
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005519 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005520 status = nfs4_set_lock_state(state, request);
5521 if (status != 0)
5522 goto out;
5523 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005524 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005525 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005526 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005527 switch (status) {
5528 case 0:
5529 request->fl_type = F_UNLCK;
5530 break;
5531 case -NFS4ERR_DENIED:
5532 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005534 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005535 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 return status;
5538}
5539
5540static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5541{
5542 struct nfs4_exception exception = { };
5543 int err;
5544
5545 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005546 err = _nfs4_proc_getlk(state, cmd, request);
5547 trace_nfs4_get_lock(request, state, cmd, err);
5548 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 &exception);
5550 } while (exception.retry);
5551 return err;
5552}
5553
Jeff Layton83bfff22015-07-11 06:43:03 -04005554static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
Benjamin Coddington4f656362015-10-22 13:38:14 -04005556 return locks_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557}
5558
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005559struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005560 struct nfs_locku_args arg;
5561 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005562 struct nfs4_lock_state *lsp;
5563 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005564 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005565 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005566 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005567};
5568
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005569static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5570 struct nfs_open_context *ctx,
5571 struct nfs4_lock_state *lsp,
5572 struct nfs_seqid *seqid)
5573{
5574 struct nfs4_unlockdata *p;
5575 struct inode *inode = lsp->ls_state->inode;
5576
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005577 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005578 if (p == NULL)
5579 return NULL;
5580 p->arg.fh = NFS_FH(inode);
5581 p->arg.fl = &p->fl;
5582 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005583 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005584 p->lsp = lsp;
5585 atomic_inc(&lsp->ls_count);
5586 /* Ensure we don't close file until we're done freeing locks! */
5587 p->ctx = get_nfs_open_context(ctx);
5588 memcpy(&p->fl, fl, sizeof(p->fl));
5589 p->server = NFS_SERVER(inode);
5590 return p;
5591}
5592
Trond Myklebust06f814a2006-01-03 09:55:07 +01005593static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005594{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005595 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005596 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005597 nfs4_put_lock_state(calldata->lsp);
5598 put_nfs_open_context(calldata->ctx);
5599 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005600}
5601
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005602static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005603{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005604 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605
Trond Myklebust14516c32010-07-31 14:29:06 -04005606 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5607 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005608 switch (task->tk_status) {
5609 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005610 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005611 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005612 if (nfs4_update_lock_stateid(calldata->lsp,
5613 &calldata->res.stateid))
5614 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005615 case -NFS4ERR_BAD_STATEID:
5616 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005617 case -NFS4ERR_STALE_STATEID:
5618 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005619 if (!nfs4_stateid_match(&calldata->arg.stateid,
5620 &calldata->lsp->ls_stateid))
5621 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005622 break;
5623 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005624 if (nfs4_async_handle_error(task, calldata->server,
5625 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005626 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005627 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005628 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005629}
5630
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005631static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005632{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005633 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005635 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005636 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005637 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005638 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005639 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005640 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005641 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005642 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005643 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005644 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005645 &calldata->res.seq_res,
5646 task) != 0)
5647 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005648 return;
5649out_no_action:
5650 task->tk_action = NULL;
5651out_wait:
5652 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653}
5654
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005655static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005656 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005657 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005658 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005659};
5660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005661static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5662 struct nfs_open_context *ctx,
5663 struct nfs4_lock_state *lsp,
5664 struct nfs_seqid *seqid)
5665{
5666 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005667 struct rpc_message msg = {
5668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5669 .rpc_cred = ctx->cred,
5670 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005671 struct rpc_task_setup task_setup_data = {
5672 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005673 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005674 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005675 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005676 .flags = RPC_TASK_ASYNC,
5677 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005678
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005679 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5680 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5681
Frank Filz137d6ac2007-07-09 15:32:29 -07005682 /* Ensure this is an unlock - when canceling a lock, the
5683 * canceled lock is passed in, and it won't be an unlock.
5684 */
5685 fl->fl_type = F_UNLCK;
5686
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005687 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5688 if (data == NULL) {
5689 nfs_free_seqid(seqid);
5690 return ERR_PTR(-ENOMEM);
5691 }
5692
Chuck Levera9c92d62013-08-09 12:48:18 -04005693 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005694 msg.rpc_argp = &data->arg;
5695 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005696 task_setup_data.callback_data = data;
5697 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005698}
5699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5701{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005702 struct inode *inode = state->inode;
5703 struct nfs4_state_owner *sp = state->owner;
5704 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005705 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005706 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005707 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005708 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005709 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005710 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
Trond Myklebust9b073572006-06-29 16:38:34 -04005712 status = nfs4_set_lock_state(state, request);
5713 /* Unlock _before_ we do the RPC call */
5714 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005715 /* Exclude nfs_delegation_claim_locks() */
5716 mutex_lock(&sp->so_delegreturn_mutex);
5717 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005718 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005719 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005720 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005721 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005722 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005723 }
5724 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005725 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005726 if (status != 0)
5727 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005728 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005729 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005730 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5731 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005732 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5733 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005734 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005735 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005736 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005737 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005738 status = PTR_ERR(task);
5739 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005740 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005741 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005742 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005743out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005744 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005745 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005746 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747}
5748
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005749struct nfs4_lockdata {
5750 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005751 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005752 struct nfs4_lock_state *lsp;
5753 struct nfs_open_context *ctx;
5754 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005755 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005756 int rpc_status;
5757 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005758 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759};
5760
5761static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005762 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5763 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005764{
5765 struct nfs4_lockdata *p;
5766 struct inode *inode = lsp->ls_state->inode;
5767 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005768 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005769
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005770 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005771 if (p == NULL)
5772 return NULL;
5773
5774 p->arg.fh = NFS_FH(inode);
5775 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005776 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005777 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005778 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005779 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5780 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005781 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005782 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005783 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005784 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005785 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005786 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005787 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005788 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005789 atomic_inc(&lsp->ls_count);
5790 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005791 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005792 memcpy(&p->fl, fl, sizeof(p->fl));
5793 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005794out_free_seqid:
5795 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005796out_free:
5797 kfree(p);
5798 return NULL;
5799}
5800
5801static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5802{
5803 struct nfs4_lockdata *data = calldata;
5804 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Harvey Harrison3110ff82008-05-02 13:42:44 -07005806 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005807 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005808 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005809 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005810 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005811 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005812 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005813 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005814 nfs4_stateid_copy(&data->arg.open_stateid,
5815 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005816 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005817 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005818 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005819 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005820 nfs4_stateid_copy(&data->arg.lock_stateid,
5821 &data->lsp->ls_stateid);
5822 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005823 if (!nfs4_valid_open_stateid(state)) {
5824 data->rpc_status = -EBADF;
5825 task->tk_action = NULL;
5826 goto out_release_open_seqid;
5827 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005828 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005829 if (nfs4_setup_sequence(data->server,
5830 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005831 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005832 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005833 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005834out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005835 nfs_release_seqid(data->arg.open_seqid);
5836out_release_lock_seqid:
5837 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005838out_wait:
5839 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005840 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005841}
5842
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005843static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5844{
5845 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005846 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005847
Harvey Harrison3110ff82008-05-02 13:42:44 -07005848 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005849
Trond Myklebust14516c32010-07-31 14:29:06 -04005850 if (!nfs4_sequence_done(task, &data->res.seq_res))
5851 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005852
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005853 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005854 switch (task->tk_status) {
5855 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005856 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005857 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005858 if (data->arg.new_lock) {
5859 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005860 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005861 rpc_restart_call_prepare(task);
5862 break;
5863 }
5864 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005865 if (data->arg.new_lock_owner != 0) {
5866 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5867 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5868 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5869 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5870 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005871 break;
5872 case -NFS4ERR_BAD_STATEID:
5873 case -NFS4ERR_OLD_STATEID:
5874 case -NFS4ERR_STALE_STATEID:
5875 case -NFS4ERR_EXPIRED:
5876 if (data->arg.new_lock_owner != 0) {
5877 if (!nfs4_stateid_match(&data->arg.open_stateid,
5878 &lsp->ls_state->open_stateid))
5879 rpc_restart_call_prepare(task);
5880 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5881 &lsp->ls_stateid))
5882 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005883 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005884 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005885}
5886
5887static void nfs4_lock_release(void *calldata)
5888{
5889 struct nfs4_lockdata *data = calldata;
5890
Harvey Harrison3110ff82008-05-02 13:42:44 -07005891 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005892 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005893 if (data->cancelled != 0) {
5894 struct rpc_task *task;
5895 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5896 data->arg.lock_seqid);
5897 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005898 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005899 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005900 } else
5901 nfs_free_seqid(data->arg.lock_seqid);
5902 nfs4_put_lock_state(data->lsp);
5903 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005904 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005905 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005906 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005907}
5908
5909static const struct rpc_call_ops nfs4_lock_ops = {
5910 .rpc_call_prepare = nfs4_lock_prepare,
5911 .rpc_call_done = nfs4_lock_done,
5912 .rpc_release = nfs4_lock_release,
5913};
5914
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005915static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5916{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005917 switch (error) {
5918 case -NFS4ERR_ADMIN_REVOKED:
5919 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005920 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005921 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005922 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005923 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005924 break;
5925 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005926 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005927 case -NFS4ERR_EXPIRED:
5928 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005929 };
5930}
5931
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005932static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005933{
5934 struct nfs4_lockdata *data;
5935 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005936 struct rpc_message msg = {
5937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5938 .rpc_cred = state->owner->so_cred,
5939 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 struct rpc_task_setup task_setup_data = {
5941 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005942 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005943 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005944 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005945 .flags = RPC_TASK_ASYNC,
5946 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005947 int ret;
5948
Harvey Harrison3110ff82008-05-02 13:42:44 -07005949 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005950 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005951 fl->fl_u.nfs4_fl.owner,
5952 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005953 if (data == NULL)
5954 return -ENOMEM;
5955 if (IS_SETLKW(cmd))
5956 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005957 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005958 msg.rpc_argp = &data->arg;
5959 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005960 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005961 if (recovery_type > NFS_LOCK_NEW) {
5962 if (recovery_type == NFS_LOCK_RECLAIM)
5963 data->arg.reclaim = NFS_LOCK_RECLAIM;
5964 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005965 } else
5966 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005967 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005968 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005969 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005970 ret = nfs4_wait_for_completion_rpc_task(task);
5971 if (ret == 0) {
5972 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005973 if (ret)
5974 nfs4_handle_setlk_error(data->server, data->lsp,
5975 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976 } else
5977 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005978 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005979 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005980 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982}
5983
5984static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5985{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005986 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005987 struct nfs4_exception exception = {
5988 .inode = state->inode,
5989 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005990 int err;
5991
5992 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005993 /* Cache the lock if possible... */
5994 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5995 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005996 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005997 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005998 break;
5999 nfs4_handle_exception(server, err, &exception);
6000 } while (exception.retry);
6001 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002}
6003
6004static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6005{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006006 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006007 struct nfs4_exception exception = {
6008 .inode = state->inode,
6009 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006010 int err;
6011
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006012 err = nfs4_set_lock_state(state, request);
6013 if (err != 0)
6014 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006015 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006016 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6017 return 0;
6018 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006019 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006020 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6021 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006022 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006023 switch (err) {
6024 default:
6025 goto out;
6026 case -NFS4ERR_GRACE:
6027 case -NFS4ERR_DELAY:
6028 nfs4_handle_exception(server, err, &exception);
6029 err = 0;
6030 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006031 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006032out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006033 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034}
6035
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006036#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006037/**
6038 * nfs41_check_expired_locks - possibly free a lock stateid
6039 *
6040 * @state: NFSv4 state for an inode
6041 *
6042 * Returns NFS_OK if recovery for this stateid is now finished.
6043 * Otherwise a negative NFS4ERR value is returned.
6044 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006045static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006046{
Chuck Levereb64cf92012-07-11 16:30:05 -04006047 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006048 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006049 struct nfs_server *server = NFS_SERVER(state->inode);
6050
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006051 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006052 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006053 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6054
6055 status = nfs41_test_stateid(server,
6056 &lsp->ls_stateid,
6057 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006058 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006059 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006060 /* Free the stateid unless the server
6061 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006062 if (status != -NFS4ERR_BAD_STATEID)
6063 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006064 &lsp->ls_stateid,
6065 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006066 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006067 ret = status;
6068 }
6069 }
6070 };
6071
6072 return ret;
6073}
6074
6075static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6076{
6077 int status = NFS_OK;
6078
6079 if (test_bit(LK_STATE_IN_USE, &state->flags))
6080 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006081 if (status != NFS_OK)
6082 status = nfs4_lock_expired(state, request);
6083 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006084}
6085#endif
6086
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6088{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006089 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006090 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006091 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006092 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006094 if ((fl_flags & FL_POSIX) &&
6095 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6096 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006097 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006098 status = nfs4_set_lock_state(state, request);
6099 if (status != 0)
6100 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006101 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006102 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 if (status < 0)
6104 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006105 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006106 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006107 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006108 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006109 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006110 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006111 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006112 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006113 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006114 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006115 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006116 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006117 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006118 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006119out:
6120 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 return status;
6122}
6123
6124static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6125{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006126 struct nfs4_exception exception = {
6127 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006128 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 int err;
6131
6132 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006133 err = _nfs4_proc_setlk(state, cmd, request);
6134 if (err == -NFS4ERR_DENIED)
6135 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006137 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 } while (exception.retry);
6139 return err;
6140}
6141
6142static int
6143nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6144{
6145 struct nfs_open_context *ctx;
6146 struct nfs4_state *state;
6147 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6148 int status;
6149
6150 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006151 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 state = ctx->state;
6153
6154 if (request->fl_start < 0 || request->fl_end < 0)
6155 return -EINVAL;
6156
Trond Myklebustd9531262009-07-21 19:22:38 -04006157 if (IS_GETLK(cmd)) {
6158 if (state != NULL)
6159 return nfs4_proc_getlk(state, F_GETLK, request);
6160 return 0;
6161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
6163 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6164 return -EINVAL;
6165
Trond Myklebustd9531262009-07-21 19:22:38 -04006166 if (request->fl_type == F_UNLCK) {
6167 if (state != NULL)
6168 return nfs4_proc_unlck(state, cmd, request);
6169 return 0;
6170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Trond Myklebustd9531262009-07-21 19:22:38 -04006172 if (state == NULL)
6173 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006174 /*
6175 * Don't rely on the VFS having checked the file open mode,
6176 * since it won't do this for flock() locks.
6177 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006178 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006179 case F_RDLCK:
6180 if (!(filp->f_mode & FMODE_READ))
6181 return -EBADF;
6182 break;
6183 case F_WRLCK:
6184 if (!(filp->f_mode & FMODE_WRITE))
6185 return -EBADF;
6186 }
6187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 do {
6189 status = nfs4_proc_setlk(state, cmd, request);
6190 if ((status != -EAGAIN) || IS_SETLK(cmd))
6191 break;
6192 timeout = nfs4_set_lock_task_retry(timeout);
6193 status = -ERESTARTSYS;
6194 if (signalled())
6195 break;
6196 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 return status;
6198}
6199
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006200int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006201{
6202 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006203 int err;
6204
6205 err = nfs4_set_lock_state(state, fl);
6206 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006207 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006208 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006209 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006210}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006211
Trond Myklebustcf470c32012-03-07 13:49:12 -05006212struct nfs_release_lockowner_data {
6213 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006214 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006215 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006216 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006217 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006218};
6219
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006220static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6221{
6222 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006223 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006224 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6225 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006226 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006227 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006228}
6229
6230static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6231{
6232 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006233 struct nfs_server *server = data->server;
6234
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006235 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006236
6237 switch (task->tk_status) {
6238 case 0:
6239 renew_lease(server, data->timestamp);
6240 break;
6241 case -NFS4ERR_STALE_CLIENTID:
6242 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006243 nfs4_schedule_lease_recovery(server->nfs_client);
6244 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006245 case -NFS4ERR_LEASE_MOVED:
6246 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006247 if (nfs4_async_handle_error(task, server,
6248 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006249 rpc_restart_call_prepare(task);
6250 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006251}
6252
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006253static void nfs4_release_lockowner_release(void *calldata)
6254{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006255 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006256 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006257 kfree(calldata);
6258}
6259
Trond Myklebust17280172012-03-11 13:11:00 -04006260static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006261 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6262 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006263 .rpc_release = nfs4_release_lockowner_release,
6264};
6265
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006266static void
6267nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006268{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006269 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006270 struct rpc_message msg = {
6271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6272 };
6273
6274 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006275 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006276
Trond Myklebustcf470c32012-03-07 13:49:12 -05006277 data = kmalloc(sizeof(*data), GFP_NOFS);
6278 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006279 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006280 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006281 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006282 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6283 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6284 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006285
Trond Myklebustcf470c32012-03-07 13:49:12 -05006286 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006287 msg.rpc_resp = &data->res;
6288 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006289 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006290}
6291
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006292#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6293
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006294static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006295 struct dentry *unused, struct inode *inode,
6296 const char *key, const void *buf,
6297 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006298{
Al Viro59301222016-05-27 10:19:30 -04006299 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006300}
6301
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006302static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006303 struct dentry *unused, struct inode *inode,
6304 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006305{
Al Virob2968212016-04-10 20:48:24 -04006306 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006307}
6308
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006309static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006310{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006311 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006312}
6313
David Quigleyc9bccef2013-05-22 12:50:45 -04006314#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006315
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006316static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006317 struct dentry *unused, struct inode *inode,
6318 const char *key, const void *buf,
6319 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006320{
6321 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006322 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006323
6324 return -EOPNOTSUPP;
6325}
6326
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006327static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006328 struct dentry *unused, struct inode *inode,
6329 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006330{
6331 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006332 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006333 return -EOPNOTSUPP;
6334}
6335
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006336static ssize_t
6337nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006338{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006339 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006340
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006341 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6342 len = security_inode_listsecurity(inode, list, list_len);
6343 if (list_len && len > list_len)
6344 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006345 }
6346 return len;
6347}
6348
6349static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6350 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006351 .get = nfs4_xattr_get_nfs4_label,
6352 .set = nfs4_xattr_set_nfs4_label,
6353};
David Quigleyc9bccef2013-05-22 12:50:45 -04006354
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006355#else
6356
6357static ssize_t
6358nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6359{
6360 return 0;
6361}
6362
6363#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006364
Andy Adamson533eb462011-06-13 18:25:56 -04006365/*
6366 * nfs_fhget will use either the mounted_on_fileid or the fileid
6367 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006368static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6369{
Andy Adamson533eb462011-06-13 18:25:56 -04006370 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6371 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6372 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006373 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006374 return;
6375
6376 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006377 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006378 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6379 fattr->nlink = 2;
6380}
6381
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006382static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6383 const struct qstr *name,
6384 struct nfs4_fs_locations *fs_locations,
6385 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006386{
6387 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006388 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006389 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006390 };
6391 struct nfs4_fs_locations_arg args = {
6392 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006393 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006394 .page = page,
6395 .bitmask = bitmask,
6396 };
Benny Halevy22958462009-04-01 09:22:02 -04006397 struct nfs4_fs_locations_res res = {
6398 .fs_locations = fs_locations,
6399 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006400 struct rpc_message msg = {
6401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6402 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006403 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006404 };
6405 int status;
6406
Harvey Harrison3110ff82008-05-02 13:42:44 -07006407 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006408
6409 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6410 * is not supported */
6411 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6412 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6413 else
6414 bitmask[0] |= FATTR4_WORD0_FILEID;
6415
Trond Myklebustc228fd32007-01-13 02:28:11 -05006416 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006417 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006418 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006419 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006420 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006421 return status;
6422}
6423
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006424int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6425 const struct qstr *name,
6426 struct nfs4_fs_locations *fs_locations,
6427 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006428{
6429 struct nfs4_exception exception = { };
6430 int err;
6431 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006432 err = _nfs4_proc_fs_locations(client, dir, name,
6433 fs_locations, page);
6434 trace_nfs4_get_fs_locations(dir, name, err);
6435 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006436 &exception);
6437 } while (exception.retry);
6438 return err;
6439}
6440
Chuck Leverb03d7352013-10-17 14:12:50 -04006441/*
6442 * This operation also signals the server that this client is
6443 * performing migration recovery. The server can stop returning
6444 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6445 * appended to this compound to identify the client ID which is
6446 * performing recovery.
6447 */
6448static int _nfs40_proc_get_locations(struct inode *inode,
6449 struct nfs4_fs_locations *locations,
6450 struct page *page, struct rpc_cred *cred)
6451{
6452 struct nfs_server *server = NFS_SERVER(inode);
6453 struct rpc_clnt *clnt = server->client;
6454 u32 bitmask[2] = {
6455 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6456 };
6457 struct nfs4_fs_locations_arg args = {
6458 .clientid = server->nfs_client->cl_clientid,
6459 .fh = NFS_FH(inode),
6460 .page = page,
6461 .bitmask = bitmask,
6462 .migration = 1, /* skip LOOKUP */
6463 .renew = 1, /* append RENEW */
6464 };
6465 struct nfs4_fs_locations_res res = {
6466 .fs_locations = locations,
6467 .migration = 1,
6468 .renew = 1,
6469 };
6470 struct rpc_message msg = {
6471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6472 .rpc_argp = &args,
6473 .rpc_resp = &res,
6474 .rpc_cred = cred,
6475 };
6476 unsigned long now = jiffies;
6477 int status;
6478
6479 nfs_fattr_init(&locations->fattr);
6480 locations->server = server;
6481 locations->nlocations = 0;
6482
6483 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6484 nfs4_set_sequence_privileged(&args.seq_args);
6485 status = nfs4_call_sync_sequence(clnt, server, &msg,
6486 &args.seq_args, &res.seq_res);
6487 if (status)
6488 return status;
6489
6490 renew_lease(server, now);
6491 return 0;
6492}
6493
6494#ifdef CONFIG_NFS_V4_1
6495
6496/*
6497 * This operation also signals the server that this client is
6498 * performing migration recovery. The server can stop asserting
6499 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6500 * performing this operation is identified in the SEQUENCE
6501 * operation in this compound.
6502 *
6503 * When the client supports GETATTR(fs_locations_info), it can
6504 * be plumbed in here.
6505 */
6506static int _nfs41_proc_get_locations(struct inode *inode,
6507 struct nfs4_fs_locations *locations,
6508 struct page *page, struct rpc_cred *cred)
6509{
6510 struct nfs_server *server = NFS_SERVER(inode);
6511 struct rpc_clnt *clnt = server->client;
6512 u32 bitmask[2] = {
6513 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6514 };
6515 struct nfs4_fs_locations_arg args = {
6516 .fh = NFS_FH(inode),
6517 .page = page,
6518 .bitmask = bitmask,
6519 .migration = 1, /* skip LOOKUP */
6520 };
6521 struct nfs4_fs_locations_res res = {
6522 .fs_locations = locations,
6523 .migration = 1,
6524 };
6525 struct rpc_message msg = {
6526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6527 .rpc_argp = &args,
6528 .rpc_resp = &res,
6529 .rpc_cred = cred,
6530 };
6531 int status;
6532
6533 nfs_fattr_init(&locations->fattr);
6534 locations->server = server;
6535 locations->nlocations = 0;
6536
6537 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6538 nfs4_set_sequence_privileged(&args.seq_args);
6539 status = nfs4_call_sync_sequence(clnt, server, &msg,
6540 &args.seq_args, &res.seq_res);
6541 if (status == NFS4_OK &&
6542 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6543 status = -NFS4ERR_LEASE_MOVED;
6544 return status;
6545}
6546
6547#endif /* CONFIG_NFS_V4_1 */
6548
6549/**
6550 * nfs4_proc_get_locations - discover locations for a migrated FSID
6551 * @inode: inode on FSID that is migrating
6552 * @locations: result of query
6553 * @page: buffer
6554 * @cred: credential to use for this operation
6555 *
6556 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6557 * operation failed, or a negative errno if a local error occurred.
6558 *
6559 * On success, "locations" is filled in, but if the server has
6560 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6561 * asserted.
6562 *
6563 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6564 * from this client that require migration recovery.
6565 */
6566int nfs4_proc_get_locations(struct inode *inode,
6567 struct nfs4_fs_locations *locations,
6568 struct page *page, struct rpc_cred *cred)
6569{
6570 struct nfs_server *server = NFS_SERVER(inode);
6571 struct nfs_client *clp = server->nfs_client;
6572 const struct nfs4_mig_recovery_ops *ops =
6573 clp->cl_mvops->mig_recovery_ops;
6574 struct nfs4_exception exception = { };
6575 int status;
6576
6577 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6578 (unsigned long long)server->fsid.major,
6579 (unsigned long long)server->fsid.minor,
6580 clp->cl_hostname);
6581 nfs_display_fhandle(NFS_FH(inode), __func__);
6582
6583 do {
6584 status = ops->get_locations(inode, locations, page, cred);
6585 if (status != -NFS4ERR_DELAY)
6586 break;
6587 nfs4_handle_exception(server, status, &exception);
6588 } while (exception.retry);
6589 return status;
6590}
6591
Chuck Lever44c99932013-10-17 14:13:30 -04006592/*
6593 * This operation also signals the server that this client is
6594 * performing "lease moved" recovery. The server can stop
6595 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6596 * is appended to this compound to identify the client ID which is
6597 * performing recovery.
6598 */
6599static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6600{
6601 struct nfs_server *server = NFS_SERVER(inode);
6602 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6603 struct rpc_clnt *clnt = server->client;
6604 struct nfs4_fsid_present_arg args = {
6605 .fh = NFS_FH(inode),
6606 .clientid = clp->cl_clientid,
6607 .renew = 1, /* append RENEW */
6608 };
6609 struct nfs4_fsid_present_res res = {
6610 .renew = 1,
6611 };
6612 struct rpc_message msg = {
6613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6614 .rpc_argp = &args,
6615 .rpc_resp = &res,
6616 .rpc_cred = cred,
6617 };
6618 unsigned long now = jiffies;
6619 int status;
6620
6621 res.fh = nfs_alloc_fhandle();
6622 if (res.fh == NULL)
6623 return -ENOMEM;
6624
6625 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6626 nfs4_set_sequence_privileged(&args.seq_args);
6627 status = nfs4_call_sync_sequence(clnt, server, &msg,
6628 &args.seq_args, &res.seq_res);
6629 nfs_free_fhandle(res.fh);
6630 if (status)
6631 return status;
6632
6633 do_renew_lease(clp, 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 "lease moved" recovery. The server can stop asserting
6642 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6643 * this operation is identified in the SEQUENCE operation in this
6644 * compound.
6645 */
6646static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6647{
6648 struct nfs_server *server = NFS_SERVER(inode);
6649 struct rpc_clnt *clnt = server->client;
6650 struct nfs4_fsid_present_arg args = {
6651 .fh = NFS_FH(inode),
6652 };
6653 struct nfs4_fsid_present_res res = {
6654 };
6655 struct rpc_message msg = {
6656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6657 .rpc_argp = &args,
6658 .rpc_resp = &res,
6659 .rpc_cred = cred,
6660 };
6661 int status;
6662
6663 res.fh = nfs_alloc_fhandle();
6664 if (res.fh == NULL)
6665 return -ENOMEM;
6666
6667 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6668 nfs4_set_sequence_privileged(&args.seq_args);
6669 status = nfs4_call_sync_sequence(clnt, server, &msg,
6670 &args.seq_args, &res.seq_res);
6671 nfs_free_fhandle(res.fh);
6672 if (status == NFS4_OK &&
6673 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6674 status = -NFS4ERR_LEASE_MOVED;
6675 return status;
6676}
6677
6678#endif /* CONFIG_NFS_V4_1 */
6679
6680/**
6681 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6682 * @inode: inode on FSID to check
6683 * @cred: credential to use for this operation
6684 *
6685 * Server indicates whether the FSID is present, moved, or not
6686 * recognized. This operation is necessary to clear a LEASE_MOVED
6687 * condition for this client ID.
6688 *
6689 * Returns NFS4_OK if the FSID is present on this server,
6690 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6691 * NFS4ERR code if some error occurred on the server, or a
6692 * negative errno if a local failure occurred.
6693 */
6694int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6695{
6696 struct nfs_server *server = NFS_SERVER(inode);
6697 struct nfs_client *clp = server->nfs_client;
6698 const struct nfs4_mig_recovery_ops *ops =
6699 clp->cl_mvops->mig_recovery_ops;
6700 struct nfs4_exception exception = { };
6701 int status;
6702
6703 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6704 (unsigned long long)server->fsid.major,
6705 (unsigned long long)server->fsid.minor,
6706 clp->cl_hostname);
6707 nfs_display_fhandle(NFS_FH(inode), __func__);
6708
6709 do {
6710 status = ops->fsid_present(inode, cred);
6711 if (status != -NFS4ERR_DELAY)
6712 break;
6713 nfs4_handle_exception(server, status, &exception);
6714 } while (exception.retry);
6715 return status;
6716}
6717
Andy Adamson5ec16a82013-08-08 10:57:55 -04006718/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006719 * If 'use_integrity' is true and the state managment nfs_client
6720 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6721 * and the machine credential as per RFC3530bis and RFC5661 Security
6722 * Considerations sections. Otherwise, just use the user cred with the
6723 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006724 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006725static 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 +00006726{
6727 int status;
6728 struct nfs4_secinfo_arg args = {
6729 .dir_fh = NFS_FH(dir),
6730 .name = name,
6731 };
6732 struct nfs4_secinfo_res res = {
6733 .flavors = flavors,
6734 };
6735 struct rpc_message msg = {
6736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6737 .rpc_argp = &args,
6738 .rpc_resp = &res,
6739 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006740 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006741 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006742
6743 if (use_integrity) {
6744 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006745 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6746 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006747 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006748
6749 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006750
6751 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6752 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6753
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006754 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6755 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006756 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006757
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006758 if (cred)
6759 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006760
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006761 return status;
6762}
6763
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006764int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6765 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006766{
6767 struct nfs4_exception exception = { };
6768 int err;
6769 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006770 err = -NFS4ERR_WRONGSEC;
6771
6772 /* try to use integrity protection with machine cred */
6773 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6774 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6775
6776 /*
6777 * if unable to use integrity protection, or SECINFO with
6778 * integrity protection returns NFS4ERR_WRONGSEC (which is
6779 * disallowed by spec, but exists in deployed servers) use
6780 * the current filesystem's rpc_client and the user cred.
6781 */
6782 if (err == -NFS4ERR_WRONGSEC)
6783 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6784
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006785 trace_nfs4_secinfo(dir, name, err);
6786 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006787 &exception);
6788 } while (exception.retry);
6789 return err;
6790}
6791
Andy Adamson557134a2009-04-01 09:21:53 -04006792#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006793/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006794 * Check the exchange flags returned by the server for invalid flags, having
6795 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6796 * DS flags set.
6797 */
6798static int nfs4_check_cl_exchange_flags(u32 flags)
6799{
6800 if (flags & ~EXCHGID4_FLAG_MASK_R)
6801 goto out_inval;
6802 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6803 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6804 goto out_inval;
6805 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6806 goto out_inval;
6807 return NFS_OK;
6808out_inval:
6809 return -NFS4ERR_INVAL;
6810}
6811
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006812static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006813nfs41_same_server_scope(struct nfs41_server_scope *a,
6814 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006815{
6816 if (a->server_scope_sz == b->server_scope_sz &&
6817 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6818 return true;
6819
6820 return false;
6821}
6822
Andy Adamson02a95de2016-02-05 16:08:37 -05006823static void
6824nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6825{
6826}
6827
6828static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6829 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6830};
6831
Andy Adamson357f54d2010-12-14 10:11:57 -05006832/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006833 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006834 *
6835 * The 4.1 client currently uses the same TCP connection for the
6836 * fore and backchannel.
6837 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006838static
6839int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6840 struct rpc_xprt *xprt,
6841 struct nfs_client *clp,
6842 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006843{
6844 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006845 struct nfs41_bind_conn_to_session_args args = {
6846 .client = clp,
6847 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6848 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006849 struct nfs41_bind_conn_to_session_res res;
6850 struct rpc_message msg = {
6851 .rpc_proc =
6852 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006853 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006854 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006855 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006856 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006857 struct rpc_task_setup task_setup_data = {
6858 .rpc_client = clnt,
6859 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006860 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006861 .rpc_message = &msg,
6862 .flags = RPC_TASK_TIMEOUT,
6863 };
6864 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006865
6866 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006867
Trond Myklebust71a097c2015-02-18 09:27:18 -08006868 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6869 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6870 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006871
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006872 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6873 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6874 args.dir = NFS4_CDFC4_FORE;
6875
6876 task = rpc_run_task(&task_setup_data);
6877 if (!IS_ERR(task)) {
6878 status = task->tk_status;
6879 rpc_put_task(task);
6880 } else
6881 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006882 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006883 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006884 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006885 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6886 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6887 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006888 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006889 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006890 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006891 dprintk("NFS: %s: Unexpected direction from server\n",
6892 __func__);
6893 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006894 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006895 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006896 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006897 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6898 __func__);
6899 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006900 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006901 }
6902 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006903out:
6904 dprintk("<-- %s status= %d\n", __func__, status);
6905 return status;
6906}
6907
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006908struct rpc_bind_conn_calldata {
6909 struct nfs_client *clp;
6910 struct rpc_cred *cred;
6911};
6912
6913static int
6914nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6915 struct rpc_xprt *xprt,
6916 void *calldata)
6917{
6918 struct rpc_bind_conn_calldata *p = calldata;
6919
6920 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6921}
6922
6923int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6924{
6925 struct rpc_bind_conn_calldata data = {
6926 .clp = clp,
6927 .cred = cred,
6928 };
6929 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6930 nfs4_proc_bind_conn_to_session_callback, &data);
6931}
6932
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006933/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006934 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6935 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006936 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006937static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6938 .how = SP4_MACH_CRED,
6939 .enforce.u.words = {
6940 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6941 1 << (OP_EXCHANGE_ID - 32) |
6942 1 << (OP_CREATE_SESSION - 32) |
6943 1 << (OP_DESTROY_SESSION - 32) |
6944 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006945 },
6946 .allow.u.words = {
6947 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006948 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006949 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006950 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006951 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006952 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006953 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006954 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006955 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006956 1 << (OP_FREE_STATEID - 32) |
6957 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006958 }
6959};
6960
6961/*
6962 * Select the state protection mode for client `clp' given the server results
6963 * from exchange_id in `sp'.
6964 *
6965 * Returns 0 on success, negative errno otherwise.
6966 */
6967static int nfs4_sp4_select_mode(struct nfs_client *clp,
6968 struct nfs41_state_protection *sp)
6969{
6970 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6971 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6972 1 << (OP_EXCHANGE_ID - 32) |
6973 1 << (OP_CREATE_SESSION - 32) |
6974 1 << (OP_DESTROY_SESSION - 32) |
6975 1 << (OP_DESTROY_CLIENTID - 32)
6976 };
6977 unsigned int i;
6978
6979 if (sp->how == SP4_MACH_CRED) {
6980 /* Print state protect result */
6981 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6982 for (i = 0; i <= LAST_NFS4_OP; i++) {
6983 if (test_bit(i, sp->enforce.u.longs))
6984 dfprintk(MOUNT, " enforce op %d\n", i);
6985 if (test_bit(i, sp->allow.u.longs))
6986 dfprintk(MOUNT, " allow op %d\n", i);
6987 }
6988
6989 /* make sure nothing is on enforce list that isn't supported */
6990 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6991 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6992 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6993 return -EINVAL;
6994 }
6995 }
6996
6997 /*
6998 * Minimal mode - state operations are allowed to use machine
6999 * credential. Note this already happens by default, so the
7000 * client doesn't have to do anything more than the negotiation.
7001 *
7002 * NOTE: we don't care if EXCHANGE_ID is in the list -
7003 * we're already using the machine cred for exchange_id
7004 * and will never use a different cred.
7005 */
7006 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7007 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7008 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7009 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7010 dfprintk(MOUNT, "sp4_mach_cred:\n");
7011 dfprintk(MOUNT, " minimal mode enabled\n");
7012 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7013 } else {
7014 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7015 return -EINVAL;
7016 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007017
7018 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007019 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7020 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007021 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7022 dfprintk(MOUNT, " cleanup mode enabled\n");
7023 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7024 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007025
Andrew Elble99ade3c2015-12-02 09:39:51 -05007026 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7027 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7028 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7029 &clp->cl_sp4_flags);
7030 }
7031
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007032 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7033 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7034 dfprintk(MOUNT, " secinfo mode enabled\n");
7035 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7036 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007037
7038 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7039 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7040 dfprintk(MOUNT, " stateid mode enabled\n");
7041 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7042 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007043
7044 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7045 dfprintk(MOUNT, " write mode enabled\n");
7046 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7047 }
7048
7049 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7050 dfprintk(MOUNT, " commit mode enabled\n");
7051 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7052 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007053 }
7054
7055 return 0;
7056}
7057
7058/*
7059 * _nfs4_proc_exchange_id()
7060 *
7061 * Wrapper for EXCHANGE_ID operation.
7062 */
7063static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7064 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007065{
7066 nfs4_verifier verifier;
7067 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007068 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007069 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007070#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007071 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007072 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7073 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7074#else
7075 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7076 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7077#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007078 };
7079 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007080 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007081 };
7082 int status;
7083 struct rpc_message msg = {
7084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7085 .rpc_argp = &args,
7086 .rpc_resp = &res,
7087 .rpc_cred = cred,
7088 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007089
Chuck Leverf0920752012-05-21 22:45:41 -04007090 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007091
7092 status = nfs4_init_uniform_client_string(clp);
7093 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007094 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007095
7096 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007097 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007098 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007099
Chuck Leveracdeb692012-05-21 22:46:16 -04007100 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007101 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007102 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007103 status = -ENOMEM;
7104 goto out;
7105 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007106
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007107 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007108 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007109 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007110 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007111 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007112 }
7113
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007114 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007115 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007116 status = -ENOMEM;
7117 goto out_server_scope;
7118 }
7119
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007120 switch (sp4_how) {
7121 case SP4_NONE:
7122 args.state_protect.how = SP4_NONE;
7123 break;
7124
7125 case SP4_MACH_CRED:
7126 args.state_protect = nfs4_sp4_mach_cred_request;
7127 break;
7128
7129 default:
7130 /* unsupported! */
7131 WARN_ON_ONCE(1);
7132 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007133 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007134 }
7135
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007136 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007137 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007138 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007139 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007140
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007141 if (status == 0)
7142 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7143
Chuck Lever177313f2012-05-21 22:44:58 -04007144 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007145 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007146 clp->cl_exchange_flags = res.flags;
7147 /* Client ID is not confirmed */
7148 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7149 clear_bit(NFS4_SESSION_ESTABLISHED,
7150 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007151 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007152 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007153
Chuck Leveracdeb692012-05-21 22:46:16 -04007154 kfree(clp->cl_serverowner);
7155 clp->cl_serverowner = res.server_owner;
7156 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007157
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007158 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007159 kfree(clp->cl_implid);
7160 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007161 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007162
Chuck Lever177313f2012-05-21 22:44:58 -04007163 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007164 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007165 res.server_scope)) {
7166 dprintk("%s: server_scope mismatch detected\n",
7167 __func__);
7168 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007169 kfree(clp->cl_serverscope);
7170 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007171 }
7172
Chuck Lever177313f2012-05-21 22:44:58 -04007173 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007174 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007175 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007176 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007177 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007178
Kinglong Mee6b559702015-07-01 11:54:53 +08007179out_impl_id:
7180 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007181out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007182 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007183out_server_owner:
7184 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007185out:
Chuck Lever177313f2012-05-21 22:44:58 -04007186 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007187 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007188 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007189 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007190 clp->cl_implid->date.seconds,
7191 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007192 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007193 return status;
7194}
7195
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007196/*
7197 * nfs4_proc_exchange_id()
7198 *
7199 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7200 *
7201 * Since the clientid has expired, all compounds using sessions
7202 * associated with the stale clientid will be returning
7203 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7204 * be in some phase of session reset.
7205 *
7206 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7207 */
7208int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7209{
7210 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7211 int status;
7212
7213 /* try SP4_MACH_CRED if krb5i/p */
7214 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7215 authflavor == RPC_AUTH_GSS_KRB5P) {
7216 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7217 if (!status)
7218 return 0;
7219 }
7220
7221 /* try SP4_NONE */
7222 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7223}
7224
Trond Myklebust66245532012-05-25 17:18:09 -04007225static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7226 struct rpc_cred *cred)
7227{
7228 struct rpc_message msg = {
7229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7230 .rpc_argp = clp,
7231 .rpc_cred = cred,
7232 };
7233 int status;
7234
7235 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007236 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007237 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007238 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007239 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7240 return status;
7241}
7242
7243static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7244 struct rpc_cred *cred)
7245{
7246 unsigned int loop;
7247 int ret;
7248
7249 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7250 ret = _nfs4_proc_destroy_clientid(clp, cred);
7251 switch (ret) {
7252 case -NFS4ERR_DELAY:
7253 case -NFS4ERR_CLIENTID_BUSY:
7254 ssleep(1);
7255 break;
7256 default:
7257 return ret;
7258 }
7259 }
7260 return 0;
7261}
7262
7263int nfs4_destroy_clientid(struct nfs_client *clp)
7264{
7265 struct rpc_cred *cred;
7266 int ret = 0;
7267
7268 if (clp->cl_mvops->minor_version < 1)
7269 goto out;
7270 if (clp->cl_exchange_flags == 0)
7271 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007272 if (clp->cl_preserve_clid)
7273 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007274 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007275 ret = nfs4_proc_destroy_clientid(clp, cred);
7276 if (cred)
7277 put_rpccred(cred);
7278 switch (ret) {
7279 case 0:
7280 case -NFS4ERR_STALE_CLIENTID:
7281 clp->cl_exchange_flags = 0;
7282 }
7283out:
7284 return ret;
7285}
7286
Andy Adamson2050f0c2009-04-01 09:22:30 -04007287struct nfs4_get_lease_time_data {
7288 struct nfs4_get_lease_time_args *args;
7289 struct nfs4_get_lease_time_res *res;
7290 struct nfs_client *clp;
7291};
7292
7293static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7294 void *calldata)
7295{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007296 struct nfs4_get_lease_time_data *data =
7297 (struct nfs4_get_lease_time_data *)calldata;
7298
7299 dprintk("--> %s\n", __func__);
7300 /* just setup sequence, do not trigger session recovery
7301 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007302 nfs41_setup_sequence(data->clp->cl_session,
7303 &data->args->la_seq_args,
7304 &data->res->lr_seq_res,
7305 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007306 dprintk("<-- %s\n", __func__);
7307}
7308
7309/*
7310 * Called from nfs4_state_manager thread for session setup, so don't recover
7311 * from sequence operation or clientid errors.
7312 */
7313static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7314{
7315 struct nfs4_get_lease_time_data *data =
7316 (struct nfs4_get_lease_time_data *)calldata;
7317
7318 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007319 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7320 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007321 switch (task->tk_status) {
7322 case -NFS4ERR_DELAY:
7323 case -NFS4ERR_GRACE:
7324 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7325 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7326 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007327 /* fall through */
7328 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007329 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007330 return;
7331 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007332 dprintk("<-- %s\n", __func__);
7333}
7334
Trond Myklebust17280172012-03-11 13:11:00 -04007335static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007336 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7337 .rpc_call_done = nfs4_get_lease_time_done,
7338};
7339
7340int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7341{
7342 struct rpc_task *task;
7343 struct nfs4_get_lease_time_args args;
7344 struct nfs4_get_lease_time_res res = {
7345 .lr_fsinfo = fsinfo,
7346 };
7347 struct nfs4_get_lease_time_data data = {
7348 .args = &args,
7349 .res = &res,
7350 .clp = clp,
7351 };
7352 struct rpc_message msg = {
7353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7354 .rpc_argp = &args,
7355 .rpc_resp = &res,
7356 };
7357 struct rpc_task_setup task_setup = {
7358 .rpc_client = clp->cl_rpcclient,
7359 .rpc_message = &msg,
7360 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007361 .callback_data = &data,
7362 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007363 };
7364 int status;
7365
Chuck Levera9c92d62013-08-09 12:48:18 -04007366 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007367 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007368 dprintk("--> %s\n", __func__);
7369 task = rpc_run_task(&task_setup);
7370
7371 if (IS_ERR(task))
7372 status = PTR_ERR(task);
7373 else {
7374 status = task->tk_status;
7375 rpc_put_task(task);
7376 }
7377 dprintk("<-- %s return %d\n", __func__, status);
7378
7379 return status;
7380}
7381
Andy Adamsonfc931582009-04-01 09:22:31 -04007382/*
7383 * Initialize the values to be used by the client in CREATE_SESSION
7384 * If nfs4_init_session set the fore channel request and response sizes,
7385 * use them.
7386 *
7387 * Set the back channel max_resp_sz_cached to zero to force the client to
7388 * always set csa_cachethis to FALSE because the current implementation
7389 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7390 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007391static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7392 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007393{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007394 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007395 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007396
Andy Adamson18aad3d2013-06-26 12:21:49 -04007397 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7398 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7399
Andy Adamsonfc931582009-04-01 09:22:31 -04007400 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007401 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7402 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007403 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007404 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007405
7406 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007407 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007408 __func__,
7409 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007410 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007411
7412 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007413 args->bc_attrs.max_rqst_sz = max_bc_payload;
7414 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007415 args->bc_attrs.max_resp_sz_cached = 0;
7416 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebustf4f58ed2016-01-23 14:12:52 -05007417 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
Andy Adamsonfc931582009-04-01 09:22:31 -04007418
7419 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7420 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7421 __func__,
7422 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7423 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7424 args->bc_attrs.max_reqs);
7425}
7426
Trond Myklebust79969dd2015-02-18 11:30:18 -08007427static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7428 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007429{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007430 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007431 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007432
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007433 if (rcvd->max_resp_sz > sent->max_resp_sz)
7434 return -EINVAL;
7435 /*
7436 * Our requested max_ops is the minimum we need; we're not
7437 * prepared to break up compounds into smaller pieces than that.
7438 * So, no point even trying to continue if the server won't
7439 * cooperate:
7440 */
7441 if (rcvd->max_ops < sent->max_ops)
7442 return -EINVAL;
7443 if (rcvd->max_reqs == 0)
7444 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007445 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7446 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007447 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007448}
7449
Trond Myklebust79969dd2015-02-18 11:30:18 -08007450static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7451 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007452{
7453 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007454 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007455
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007456 if (!(res->flags & SESSION4_BACK_CHAN))
7457 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007458 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7459 return -EINVAL;
7460 if (rcvd->max_resp_sz < sent->max_resp_sz)
7461 return -EINVAL;
7462 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7463 return -EINVAL;
7464 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007465 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007466 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007467 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007468 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007469out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007470 return 0;
7471}
Andy Adamson8d353012009-04-01 09:22:32 -04007472
Andy Adamson8d353012009-04-01 09:22:32 -04007473static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007474 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007475{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007476 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007477
Trond Myklebust79969dd2015-02-18 11:30:18 -08007478 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007479 if (ret)
7480 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007481 return nfs4_verify_back_channel_attrs(args, res);
7482}
7483
7484static void nfs4_update_session(struct nfs4_session *session,
7485 struct nfs41_create_session_res *res)
7486{
7487 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007488 /* Mark client id and session as being confirmed */
7489 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7490 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007491 session->flags = res->flags;
7492 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007493 if (res->flags & SESSION4_BACK_CHAN)
7494 memcpy(&session->bc_attrs, &res->bc_attrs,
7495 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007496}
7497
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007498static int _nfs4_proc_create_session(struct nfs_client *clp,
7499 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007500{
7501 struct nfs4_session *session = clp->cl_session;
7502 struct nfs41_create_session_args args = {
7503 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007504 .clientid = clp->cl_clientid,
7505 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007506 .cb_program = NFS4_CALLBACK,
7507 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007508 struct nfs41_create_session_res res;
7509
Andy Adamsonfc931582009-04-01 09:22:31 -04007510 struct rpc_message msg = {
7511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7512 .rpc_argp = &args,
7513 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007514 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007515 };
7516 int status;
7517
Chuck Lever6b26cc82016-05-02 14:40:40 -04007518 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007519 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007520
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007521 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007522 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007523
Trond Myklebust43095d32012-11-20 11:13:12 -05007524 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007525 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007526 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007527 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007528 if (clp->cl_seqid == res.seqid)
7529 clp->cl_seqid++;
7530 if (status)
7531 goto out;
7532 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007533 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007534out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007535 return status;
7536}
7537
7538/*
7539 * Issues a CREATE_SESSION operation to the server.
7540 * It is the responsibility of the caller to verify the session is
7541 * expired before calling this routine.
7542 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007543int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007544{
7545 int status;
7546 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007547 struct nfs4_session *session = clp->cl_session;
7548
7549 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7550
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007551 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007552 if (status)
7553 goto out;
7554
Andy Adamsonaacd5532011-11-09 13:58:21 -05007555 /* Init or reset the session slot tables */
7556 status = nfs4_setup_session_slot_tables(session);
7557 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007558 if (status)
7559 goto out;
7560
7561 ptr = (unsigned *)&session->sess_id.data[0];
7562 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7563 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007564out:
7565 dprintk("<-- %s\n", __func__);
7566 return status;
7567}
7568
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007569/*
7570 * Issue the over-the-wire RPC DESTROY_SESSION.
7571 * The caller must serialize access to this routine.
7572 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007573int nfs4_proc_destroy_session(struct nfs4_session *session,
7574 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007575{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007576 struct rpc_message msg = {
7577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7578 .rpc_argp = session,
7579 .rpc_cred = cred,
7580 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007581 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007582
7583 dprintk("--> nfs4_proc_destroy_session\n");
7584
7585 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007586 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7587 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007588
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007589 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007590 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007591
7592 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007593 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007594 "Session has been destroyed regardless...\n", status);
7595
7596 dprintk("<-- nfs4_proc_destroy_session\n");
7597 return status;
7598}
7599
Trond Myklebust7b38c362012-05-23 13:23:31 -04007600/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007601 * Renew the cl_session lease.
7602 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007603struct nfs4_sequence_data {
7604 struct nfs_client *clp;
7605 struct nfs4_sequence_args args;
7606 struct nfs4_sequence_res res;
7607};
7608
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007609static void nfs41_sequence_release(void *data)
7610{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007611 struct nfs4_sequence_data *calldata = data;
7612 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007613
Alexandros Batsakis71358402010-02-05 03:45:05 -08007614 if (atomic_read(&clp->cl_count) > 1)
7615 nfs4_schedule_state_renewal(clp);
7616 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007617 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007618}
7619
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007620static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7621{
7622 switch(task->tk_status) {
7623 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007624 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7625 return -EAGAIN;
7626 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007627 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007628 }
7629 return 0;
7630}
7631
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007632static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007633{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007634 struct nfs4_sequence_data *calldata = data;
7635 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007636
Trond Myklebust14516c32010-07-31 14:29:06 -04007637 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7638 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007639
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007640 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007641 if (task->tk_status < 0) {
7642 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007643 if (atomic_read(&clp->cl_count) == 1)
7644 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007645
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007646 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7647 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007648 return;
7649 }
7650 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007651 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007652out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007653 dprintk("<-- %s\n", __func__);
7654}
7655
7656static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7657{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007658 struct nfs4_sequence_data *calldata = data;
7659 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007660 struct nfs4_sequence_args *args;
7661 struct nfs4_sequence_res *res;
7662
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007663 args = task->tk_msg.rpc_argp;
7664 res = task->tk_msg.rpc_resp;
7665
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007666 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007667}
7668
7669static const struct rpc_call_ops nfs41_sequence_ops = {
7670 .rpc_call_done = nfs41_sequence_call_done,
7671 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007672 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007673};
7674
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007675static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7676 struct rpc_cred *cred,
7677 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007678{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007679 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007680 struct rpc_message msg = {
7681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7682 .rpc_cred = cred,
7683 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007684 struct rpc_task_setup task_setup_data = {
7685 .rpc_client = clp->cl_rpcclient,
7686 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007687 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007688 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007689 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007690
Alexandros Batsakis71358402010-02-05 03:45:05 -08007691 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007692 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007693 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007694 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007695 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007696 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007697 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007698 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007699 if (is_privileged)
7700 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007701 msg.rpc_argp = &calldata->args;
7702 msg.rpc_resp = &calldata->res;
7703 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007704 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007705
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007706 return rpc_run_task(&task_setup_data);
7707}
7708
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007709static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007710{
7711 struct rpc_task *task;
7712 int ret = 0;
7713
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007714 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007715 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007716 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007717 if (IS_ERR(task))
7718 ret = PTR_ERR(task);
7719 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007720 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007721 dprintk("<-- %s status=%d\n", __func__, ret);
7722 return ret;
7723}
7724
7725static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7726{
7727 struct rpc_task *task;
7728 int ret;
7729
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007730 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007731 if (IS_ERR(task)) {
7732 ret = PTR_ERR(task);
7733 goto out;
7734 }
7735 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007736 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007737 ret = task->tk_status;
7738 rpc_put_task(task);
7739out:
7740 dprintk("<-- %s status=%d\n", __func__, ret);
7741 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007742}
7743
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007744struct nfs4_reclaim_complete_data {
7745 struct nfs_client *clp;
7746 struct nfs41_reclaim_complete_args arg;
7747 struct nfs41_reclaim_complete_res res;
7748};
7749
7750static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7751{
7752 struct nfs4_reclaim_complete_data *calldata = data;
7753
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007754 nfs41_setup_sequence(calldata->clp->cl_session,
7755 &calldata->arg.seq_args,
7756 &calldata->res.seq_res,
7757 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007758}
7759
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007760static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7761{
7762 switch(task->tk_status) {
7763 case 0:
7764 case -NFS4ERR_COMPLETE_ALREADY:
7765 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7766 break;
7767 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007768 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007769 /* fall through */
7770 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007771 return -EAGAIN;
7772 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007773 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007774 }
7775 return 0;
7776}
7777
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007778static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7779{
7780 struct nfs4_reclaim_complete_data *calldata = data;
7781 struct nfs_client *clp = calldata->clp;
7782 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7783
7784 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007785 if (!nfs41_sequence_done(task, res))
7786 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007787
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007788 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007789 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7790 rpc_restart_call_prepare(task);
7791 return;
7792 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007793 dprintk("<-- %s\n", __func__);
7794}
7795
7796static void nfs4_free_reclaim_complete_data(void *data)
7797{
7798 struct nfs4_reclaim_complete_data *calldata = data;
7799
7800 kfree(calldata);
7801}
7802
7803static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7804 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7805 .rpc_call_done = nfs4_reclaim_complete_done,
7806 .rpc_release = nfs4_free_reclaim_complete_data,
7807};
7808
7809/*
7810 * Issue a global reclaim complete.
7811 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007812static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7813 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007814{
7815 struct nfs4_reclaim_complete_data *calldata;
7816 struct rpc_task *task;
7817 struct rpc_message msg = {
7818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007819 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007820 };
7821 struct rpc_task_setup task_setup_data = {
7822 .rpc_client = clp->cl_rpcclient,
7823 .rpc_message = &msg,
7824 .callback_ops = &nfs4_reclaim_complete_call_ops,
7825 .flags = RPC_TASK_ASYNC,
7826 };
7827 int status = -ENOMEM;
7828
7829 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007830 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007831 if (calldata == NULL)
7832 goto out;
7833 calldata->clp = clp;
7834 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007835
Chuck Levera9c92d62013-08-09 12:48:18 -04007836 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007837 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007838 msg.rpc_argp = &calldata->arg;
7839 msg.rpc_resp = &calldata->res;
7840 task_setup_data.callback_data = calldata;
7841 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007842 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007843 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007844 goto out;
7845 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007846 status = nfs4_wait_for_completion_rpc_task(task);
7847 if (status == 0)
7848 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007849 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007850 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007851out:
7852 dprintk("<-- %s status=%d\n", __func__, status);
7853 return status;
7854}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007855
7856static void
7857nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7858{
7859 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007860 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007861 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007862
7863 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007864 nfs41_setup_sequence(session, &lgp->args.seq_args,
7865 &lgp->res.seq_res, task);
7866 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007867}
7868
7869static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7870{
7871 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04007872
7873 dprintk("--> %s\n", __func__);
7874 nfs41_sequence_done(task, &lgp->res.seq_res);
7875 dprintk("<-- %s\n", __func__);
7876}
7877
7878static int
7879nfs4_layoutget_handle_exception(struct rpc_task *task,
7880 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
7881{
Trond Myklebustee314c22012-10-01 17:25:48 -07007882 struct inode *inode = lgp->args.inode;
7883 struct nfs_server *server = NFS_SERVER(inode);
7884 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007885 int nfs4err = task->tk_status;
7886 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007887 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007888
Boaz Harroshed7e5422014-01-22 20:34:54 +02007889 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007890
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007891 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007892 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007893 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007894
7895 /*
7896 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7897 * on the file. set tk_status to -ENODATA to tell upper layer to
7898 * retry go inband.
7899 */
7900 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04007901 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007902 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007903 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007904 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7905 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7906 */
7907 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007908 status = -EOVERFLOW;
7909 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007910 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007911 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007912 * (or clients) writing to the same RAID stripe except when
7913 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04007914 *
7915 * Treat it like we would RECALLCONFLICT -- we retry for a little
7916 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02007917 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007918 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04007919 if (lgp->args.minlength == 0) {
7920 status = -EOVERFLOW;
7921 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007922 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007923 status = -EBUSY;
7924 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04007925 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007926 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007927 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07007928 case -NFS4ERR_EXPIRED:
7929 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04007930 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07007931 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007932 lo = NFS_I(inode)->layout;
7933 /* If the open stateid was bad, then recover it. */
7934 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
7935 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04007936 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007937 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04007938 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007939 break;
7940 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007941
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007942 /*
7943 * Mark the bad layout state as invalid, then retry
7944 */
Trond Myklebust668f4552016-07-24 17:08:59 -04007945 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007946 spin_unlock(&inode->i_lock);
7947 pnfs_free_lseg_list(&head);
7948 status = -EAGAIN;
7949 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007950 }
Jeff Layton183d9e72016-05-17 12:28:47 -04007951
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007952 err = nfs4_handle_exception(server, nfs4err, exception);
7953 if (!status) {
7954 if (exception->retry)
7955 status = -EAGAIN;
7956 else
7957 status = err;
7958 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007959out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007960 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007961 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007962}
7963
Idan Kedar85541162012-08-02 11:47:10 +03007964static size_t max_response_pages(struct nfs_server *server)
7965{
7966 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7967 return nfs_page_array_len(0, max_resp_sz);
7968}
7969
7970static void nfs4_free_pages(struct page **pages, size_t size)
7971{
7972 int i;
7973
7974 if (!pages)
7975 return;
7976
7977 for (i = 0; i < size; i++) {
7978 if (!pages[i])
7979 break;
7980 __free_page(pages[i]);
7981 }
7982 kfree(pages);
7983}
7984
7985static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7986{
7987 struct page **pages;
7988 int i;
7989
7990 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7991 if (!pages) {
7992 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7993 return NULL;
7994 }
7995
7996 for (i = 0; i < size; i++) {
7997 pages[i] = alloc_page(gfp_flags);
7998 if (!pages[i]) {
7999 dprintk("%s: failed to allocate page\n", __func__);
8000 nfs4_free_pages(pages, size);
8001 return NULL;
8002 }
8003 }
8004
8005 return pages;
8006}
8007
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008008static void nfs4_layoutget_release(void *calldata)
8009{
8010 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008011 struct inode *inode = lgp->args.inode;
8012 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008013 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008014
8015 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008016 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008017 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008018 put_nfs_open_context(lgp->args.ctx);
8019 kfree(calldata);
8020 dprintk("<-- %s\n", __func__);
8021}
8022
8023static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8024 .rpc_call_prepare = nfs4_layoutget_prepare,
8025 .rpc_call_done = nfs4_layoutget_done,
8026 .rpc_release = nfs4_layoutget_release,
8027};
8028
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008029struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008030nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008031{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008032 struct inode *inode = lgp->args.inode;
8033 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008034 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008035 struct rpc_task *task;
8036 struct rpc_message msg = {
8037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8038 .rpc_argp = &lgp->args,
8039 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008040 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008041 };
8042 struct rpc_task_setup task_setup_data = {
8043 .rpc_client = server->client,
8044 .rpc_message = &msg,
8045 .callback_ops = &nfs4_layoutget_call_ops,
8046 .callback_data = lgp,
8047 .flags = RPC_TASK_ASYNC,
8048 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008049 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008050 struct nfs4_exception exception = {
8051 .inode = inode,
8052 .timeout = *timeout,
8053 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008054 int status = 0;
8055
8056 dprintk("--> %s\n", __func__);
8057
Peng Tao4bd5a9802014-11-17 11:05:17 +08008058 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8059 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8060
Idan Kedar85541162012-08-02 11:47:10 +03008061 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8062 if (!lgp->args.layout.pages) {
8063 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008064 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008065 }
8066 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8067
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008068 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008069 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008070 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008071
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008072 task = rpc_run_task(&task_setup_data);
8073 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008074 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008075 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008076 if (status == 0) {
8077 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8078 *timeout = exception.timeout;
8079 }
8080
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008081 trace_nfs4_layoutget(lgp->args.ctx,
8082 &lgp->args.range,
8083 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008084 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008085 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008086
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008087 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8088 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008089 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008090 rpc_put_task(task);
8091 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008092 if (status)
8093 return ERR_PTR(status);
8094 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008095}
8096
Benny Halevycbe82602011-05-22 19:52:37 +03008097static void
8098nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8099{
8100 struct nfs4_layoutreturn *lrp = calldata;
8101
8102 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008103 nfs41_setup_sequence(lrp->clp->cl_session,
8104 &lrp->args.seq_args,
8105 &lrp->res.seq_res,
8106 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008107}
8108
8109static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8110{
8111 struct nfs4_layoutreturn *lrp = calldata;
8112 struct nfs_server *server;
8113
8114 dprintk("--> %s\n", __func__);
8115
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008116 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008117 return;
8118
8119 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008120 switch (task->tk_status) {
8121 default:
8122 task->tk_status = 0;
8123 case 0:
8124 break;
8125 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008126 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008127 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008128 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008129 return;
8130 }
Benny Halevycbe82602011-05-22 19:52:37 +03008131 dprintk("<-- %s\n", __func__);
8132}
8133
8134static void nfs4_layoutreturn_release(void *calldata)
8135{
8136 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008137 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008138 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008139
8140 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008141 spin_lock(&lo->plh_inode->i_lock);
Jeff Layton6d597e12016-05-17 12:28:42 -04008142 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range,
8143 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust45fcc7b2016-07-24 12:26:34 -04008144 if (lrp->res.lrs_present && pnfs_layout_is_valid(lo))
Trond Myklebust849b2862012-09-24 14:18:39 -04008145 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05008146 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008147 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008148 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008149 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008150 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008151 kfree(calldata);
8152 dprintk("<-- %s\n", __func__);
8153}
8154
8155static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8156 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8157 .rpc_call_done = nfs4_layoutreturn_done,
8158 .rpc_release = nfs4_layoutreturn_release,
8159};
8160
Peng Tao6c166052014-11-17 09:30:40 +08008161int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008162{
8163 struct rpc_task *task;
8164 struct rpc_message msg = {
8165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8166 .rpc_argp = &lrp->args,
8167 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008168 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008169 };
8170 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008171 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008172 .rpc_message = &msg,
8173 .callback_ops = &nfs4_layoutreturn_call_ops,
8174 .callback_data = lrp,
8175 };
Peng Tao6c166052014-11-17 09:30:40 +08008176 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008177
Andrew Elble99ade3c2015-12-02 09:39:51 -05008178 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8179 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8180 &task_setup_data.rpc_client, &msg);
8181
Benny Halevycbe82602011-05-22 19:52:37 +03008182 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008183 if (!sync) {
8184 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8185 if (!lrp->inode) {
8186 nfs4_layoutreturn_release(lrp);
8187 return -EAGAIN;
8188 }
8189 task_setup_data.flags |= RPC_TASK_ASYNC;
8190 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008191 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008192 task = rpc_run_task(&task_setup_data);
8193 if (IS_ERR(task))
8194 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008195 if (sync)
8196 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008197 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008198 dprintk("<-- %s status=%d\n", __func__, status);
8199 rpc_put_task(task);
8200 return status;
8201}
8202
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008203static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008204_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8205 struct pnfs_device *pdev,
8206 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008207{
8208 struct nfs4_getdeviceinfo_args args = {
8209 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008210 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8211 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008212 };
8213 struct nfs4_getdeviceinfo_res res = {
8214 .pdev = pdev,
8215 };
8216 struct rpc_message msg = {
8217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8218 .rpc_argp = &args,
8219 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008220 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008221 };
8222 int status;
8223
8224 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008225 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008226 if (res.notification & ~args.notify_types)
8227 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008228 if (res.notification != args.notify_types)
8229 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008230
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008231 dprintk("<-- %s status=%d\n", __func__, status);
8232
8233 return status;
8234}
8235
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008236int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8237 struct pnfs_device *pdev,
8238 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008239{
8240 struct nfs4_exception exception = { };
8241 int err;
8242
8243 do {
8244 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008245 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008246 &exception);
8247 } while (exception.retry);
8248 return err;
8249}
8250EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8251
Andy Adamson863a3c62011-03-23 13:27:54 +00008252static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8253{
8254 struct nfs4_layoutcommit_data *data = calldata;
8255 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008256 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008257
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008258 nfs41_setup_sequence(session,
8259 &data->args.seq_args,
8260 &data->res.seq_res,
8261 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008262}
8263
8264static void
8265nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8266{
8267 struct nfs4_layoutcommit_data *data = calldata;
8268 struct nfs_server *server = NFS_SERVER(data->args.inode);
8269
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008270 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008271 return;
8272
8273 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008274 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8275 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8276 case -NFS4ERR_BADLAYOUT: /* no layout */
8277 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008278 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008279 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008280 break;
8281 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008282 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008283 rpc_restart_call_prepare(task);
8284 return;
8285 }
8286 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008287}
8288
8289static void nfs4_layoutcommit_release(void *calldata)
8290{
8291 struct nfs4_layoutcommit_data *data = calldata;
8292
Andy Adamsondb29c082011-07-30 20:52:38 -04008293 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008294 nfs_post_op_update_inode_force_wcc(data->args.inode,
8295 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008296 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008297 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008298 kfree(data);
8299}
8300
8301static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8302 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8303 .rpc_call_done = nfs4_layoutcommit_done,
8304 .rpc_release = nfs4_layoutcommit_release,
8305};
8306
8307int
Andy Adamsonef311532011-03-12 02:58:10 -05008308nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008309{
8310 struct rpc_message msg = {
8311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8312 .rpc_argp = &data->args,
8313 .rpc_resp = &data->res,
8314 .rpc_cred = data->cred,
8315 };
8316 struct rpc_task_setup task_setup_data = {
8317 .task = &data->task,
8318 .rpc_client = NFS_CLIENT(data->args.inode),
8319 .rpc_message = &msg,
8320 .callback_ops = &nfs4_layoutcommit_ops,
8321 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008322 };
8323 struct rpc_task *task;
8324 int status = 0;
8325
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008326 dprintk("NFS: initiating layoutcommit call. sync %d "
8327 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008328 data->args.lastbytewritten,
8329 data->args.inode->i_ino);
8330
Trond Myklebust472e2592015-02-05 16:50:30 -05008331 if (!sync) {
8332 data->inode = nfs_igrab_and_active(data->args.inode);
8333 if (data->inode == NULL) {
8334 nfs4_layoutcommit_release(data);
8335 return -EAGAIN;
8336 }
8337 task_setup_data.flags = RPC_TASK_ASYNC;
8338 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008339 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008340 task = rpc_run_task(&task_setup_data);
8341 if (IS_ERR(task))
8342 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008343 if (sync)
8344 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008345 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008346 dprintk("%s: status %d\n", __func__, status);
8347 rpc_put_task(task);
8348 return status;
8349}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008350
Andy Adamson97431202013-08-08 10:57:56 -04008351/**
8352 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8353 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8354 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008355static int
8356_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008357 struct nfs_fsinfo *info,
8358 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008359{
8360 struct nfs41_secinfo_no_name_args args = {
8361 .style = SECINFO_STYLE_CURRENT_FH,
8362 };
8363 struct nfs4_secinfo_res res = {
8364 .flavors = flavors,
8365 };
8366 struct rpc_message msg = {
8367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8368 .rpc_argp = &args,
8369 .rpc_resp = &res,
8370 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008371 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008372 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008373 int status;
8374
8375 if (use_integrity) {
8376 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008377 cred = nfs4_get_clid_cred(server->nfs_client);
8378 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008379 }
8380
8381 dprintk("--> %s\n", __func__);
8382 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8383 &res.seq_res, 0);
8384 dprintk("<-- %s status=%d\n", __func__, status);
8385
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008386 if (cred)
8387 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008388
8389 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008390}
8391
8392static int
8393nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8394 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8395{
8396 struct nfs4_exception exception = { };
8397 int err;
8398 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008399 /* first try using integrity protection */
8400 err = -NFS4ERR_WRONGSEC;
8401
8402 /* try to use integrity protection with machine cred */
8403 if (_nfs4_is_integrity_protected(server->nfs_client))
8404 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8405 flavors, true);
8406
8407 /*
8408 * if unable to use integrity protection, or SECINFO with
8409 * integrity protection returns NFS4ERR_WRONGSEC (which is
8410 * disallowed by spec, but exists in deployed servers) use
8411 * the current filesystem's rpc_client and the user cred.
8412 */
8413 if (err == -NFS4ERR_WRONGSEC)
8414 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8415 flavors, false);
8416
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008417 switch (err) {
8418 case 0:
8419 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008420 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008421 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008422 default:
8423 err = nfs4_handle_exception(server, err, &exception);
8424 }
8425 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008426out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008427 return err;
8428}
8429
8430static int
8431nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8432 struct nfs_fsinfo *info)
8433{
8434 int err;
8435 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008436 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008437 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008438 struct nfs4_secinfo4 *secinfo;
8439 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008440
8441 page = alloc_page(GFP_KERNEL);
8442 if (!page) {
8443 err = -ENOMEM;
8444 goto out;
8445 }
8446
8447 flavors = page_address(page);
8448 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8449
8450 /*
8451 * Fall back on "guess and check" method if
8452 * the server doesn't support SECINFO_NO_NAME
8453 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008454 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008455 err = nfs4_find_root_sec(server, fhandle, info);
8456 goto out_freepage;
8457 }
8458 if (err)
8459 goto out_freepage;
8460
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008461 for (i = 0; i < flavors->num_flavors; i++) {
8462 secinfo = &flavors->flavors[i];
8463
8464 switch (secinfo->flavor) {
8465 case RPC_AUTH_NULL:
8466 case RPC_AUTH_UNIX:
8467 case RPC_AUTH_GSS:
8468 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8469 &secinfo->flavor_info);
8470 break;
8471 default:
8472 flavor = RPC_AUTH_MAXFLAVOR;
8473 break;
8474 }
8475
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008476 if (!nfs_auth_info_match(&server->auth_info, flavor))
8477 flavor = RPC_AUTH_MAXFLAVOR;
8478
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008479 if (flavor != RPC_AUTH_MAXFLAVOR) {
8480 err = nfs4_lookup_root_sec(server, fhandle,
8481 info, flavor);
8482 if (!err)
8483 break;
8484 }
8485 }
8486
8487 if (flavor == RPC_AUTH_MAXFLAVOR)
8488 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008489
8490out_freepage:
8491 put_page(page);
8492 if (err == -EACCES)
8493 return -EPERM;
8494out:
8495 return err;
8496}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008497
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008498static int _nfs41_test_stateid(struct nfs_server *server,
8499 nfs4_stateid *stateid,
8500 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008501{
8502 int status;
8503 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008504 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008505 };
8506 struct nfs41_test_stateid_res res;
8507 struct rpc_message msg = {
8508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8509 .rpc_argp = &args,
8510 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008511 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008512 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008513 struct rpc_clnt *rpc_client = server->client;
8514
8515 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8516 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008517
Chuck Lever38527b12012-07-11 16:30:23 -04008518 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008519 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008520 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008521 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008522 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008523 if (status != NFS_OK) {
8524 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008525 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008526 }
8527 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008528 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008529}
8530
Chuck Lever38527b12012-07-11 16:30:23 -04008531/**
8532 * nfs41_test_stateid - perform a TEST_STATEID operation
8533 *
8534 * @server: server / transport on which to perform the operation
8535 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008536 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008537 *
8538 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8539 * Otherwise a negative NFS4ERR value is returned if the operation
8540 * failed or the state ID is not currently valid.
8541 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008542static int nfs41_test_stateid(struct nfs_server *server,
8543 nfs4_stateid *stateid,
8544 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008545{
8546 struct nfs4_exception exception = { };
8547 int err;
8548 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008549 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008550 if (err != -NFS4ERR_DELAY)
8551 break;
8552 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008553 } while (exception.retry);
8554 return err;
8555}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008556
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008557struct nfs_free_stateid_data {
8558 struct nfs_server *server;
8559 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008560 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008561};
8562
8563static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8564{
8565 struct nfs_free_stateid_data *data = calldata;
8566 nfs41_setup_sequence(nfs4_get_session(data->server),
8567 &data->args.seq_args,
8568 &data->res.seq_res,
8569 task);
8570}
8571
8572static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8573{
8574 struct nfs_free_stateid_data *data = calldata;
8575
8576 nfs41_sequence_done(task, &data->res.seq_res);
8577
8578 switch (task->tk_status) {
8579 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008580 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008581 rpc_restart_call_prepare(task);
8582 }
8583}
8584
8585static void nfs41_free_stateid_release(void *calldata)
8586{
8587 kfree(calldata);
8588}
8589
Trond Myklebust17f26b12013-08-21 15:48:42 -04008590static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008591 .rpc_call_prepare = nfs41_free_stateid_prepare,
8592 .rpc_call_done = nfs41_free_stateid_done,
8593 .rpc_release = nfs41_free_stateid_release,
8594};
8595
8596static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8597 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008598 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008599 bool privileged)
8600{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008601 struct rpc_message msg = {
8602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008603 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008604 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008605 struct rpc_task_setup task_setup = {
8606 .rpc_client = server->client,
8607 .rpc_message = &msg,
8608 .callback_ops = &nfs41_free_stateid_ops,
8609 .flags = RPC_TASK_ASYNC,
8610 };
8611 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008612
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008613 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8614 &task_setup.rpc_client, &msg);
8615
Chuck Lever38527b12012-07-11 16:30:23 -04008616 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008617 data = kmalloc(sizeof(*data), GFP_NOFS);
8618 if (!data)
8619 return ERR_PTR(-ENOMEM);
8620 data->server = server;
8621 nfs4_stateid_copy(&data->args.stateid, stateid);
8622
8623 task_setup.callback_data = data;
8624
8625 msg.rpc_argp = &data->args;
8626 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008627 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008628 if (privileged)
8629 nfs4_set_sequence_privileged(&data->args.seq_args);
8630
8631 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008632}
8633
Chuck Lever38527b12012-07-11 16:30:23 -04008634/**
8635 * nfs41_free_stateid - perform a FREE_STATEID operation
8636 *
8637 * @server: server / transport on which to perform the operation
8638 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008639 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008640 *
8641 * Returns NFS_OK if the server freed "stateid". Otherwise a
8642 * negative NFS4ERR value is returned.
8643 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008644static int nfs41_free_stateid(struct nfs_server *server,
8645 nfs4_stateid *stateid,
8646 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008647{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008648 struct rpc_task *task;
8649 int ret;
8650
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008651 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008652 if (IS_ERR(task))
8653 return PTR_ERR(task);
8654 ret = rpc_wait_for_completion_task(task);
8655 if (!ret)
8656 ret = task->tk_status;
8657 rpc_put_task(task);
8658 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008659}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008660
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008661static void
8662nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008663{
8664 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008665 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008666
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008667 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008668 nfs4_free_lock_state(server, lsp);
8669 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008670 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008671 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008672}
8673
Trond Myklebust36281ca2012-03-04 18:13:56 -05008674static bool nfs41_match_stateid(const nfs4_stateid *s1,
8675 const nfs4_stateid *s2)
8676{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008677 if (s1->type != s2->type)
8678 return false;
8679
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008680 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008681 return false;
8682
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008683 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008684 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008685 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008686 return true;
8687
8688 return false;
8689}
8690
Andy Adamson557134a2009-04-01 09:21:53 -04008691#endif /* CONFIG_NFS_V4_1 */
8692
Trond Myklebust36281ca2012-03-04 18:13:56 -05008693static bool nfs4_match_stateid(const nfs4_stateid *s1,
8694 const nfs4_stateid *s2)
8695{
Trond Myklebustf597c532012-03-04 18:13:56 -05008696 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008697}
8698
8699
Trond Myklebust17280172012-03-11 13:11:00 -04008700static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008701 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008702 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 .recover_open = nfs4_open_reclaim,
8704 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008705 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008706 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707};
8708
Andy Adamson591d71c2009-04-01 09:22:47 -04008709#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008710static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008711 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8712 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8713 .recover_open = nfs4_open_reclaim,
8714 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008715 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008716 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008717 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008718};
8719#endif /* CONFIG_NFS_V4_1 */
8720
Trond Myklebust17280172012-03-11 13:11:00 -04008721static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008722 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008723 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008724 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008726 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727};
8728
Andy Adamson591d71c2009-04-01 09:22:47 -04008729#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008730static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008731 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8732 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008733 .recover_open = nfs41_open_expired,
8734 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008735 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008736};
8737#endif /* CONFIG_NFS_V4_1 */
8738
Trond Myklebust17280172012-03-11 13:11:00 -04008739static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008740 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008741 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008742 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008743};
8744
8745#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008746static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008747 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008748 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008749 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008750};
8751#endif
8752
Chuck Leverec011fe2013-10-17 14:12:39 -04008753static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008754 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008755 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008756};
8757
8758#if defined(CONFIG_NFS_V4_1)
8759static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008760 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008761 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008762};
8763#endif /* CONFIG_NFS_V4_1 */
8764
Trond Myklebust97dc1352010-06-16 09:52:26 -04008765static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8766 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008767 .init_caps = NFS_CAP_READDIRPLUS
8768 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008769 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008770 .init_client = nfs40_init_client,
8771 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008772 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008773 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008774 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008775 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008776 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008777 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8778 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8779 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008780 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008781};
8782
8783#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008784static struct nfs_seqid *
8785nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8786{
8787 return NULL;
8788}
8789
Trond Myklebust97dc1352010-06-16 09:52:26 -04008790static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8791 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008792 .init_caps = NFS_CAP_READDIRPLUS
8793 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008794 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008795 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008796 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008797 .init_client = nfs41_init_client,
8798 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008799 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008800 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008801 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008802 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008803 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008804 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8805 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8806 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008807 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008808};
8809#endif
8810
Steve Dickson42c2c422013-05-22 12:50:38 -04008811#if defined(CONFIG_NFS_V4_2)
8812static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8813 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008814 .init_caps = NFS_CAP_READDIRPLUS
8815 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008816 | NFS_CAP_POSIX_LOCK
8817 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008818 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008819 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008820 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008821 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008822 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008823 | NFS_CAP_LAYOUTSTATS
8824 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008825 .init_client = nfs41_init_client,
8826 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008827 .match_stateid = nfs41_match_stateid,
8828 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008829 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008830 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008831 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008832 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8833 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8834 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008835 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008836};
8837#endif
8838
Trond Myklebust97dc1352010-06-16 09:52:26 -04008839const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8840 [0] = &nfs_v4_0_minor_ops,
8841#if defined(CONFIG_NFS_V4_1)
8842 [1] = &nfs_v4_1_minor_ops,
8843#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008844#if defined(CONFIG_NFS_V4_2)
8845 [2] = &nfs_v4_2_minor_ops,
8846#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008847};
8848
Trond Myklebust13997822016-07-24 17:10:52 -04008849static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008850{
8851 ssize_t error, error2;
8852
8853 error = generic_listxattr(dentry, list, size);
8854 if (error < 0)
8855 return error;
8856 if (list) {
8857 list += error;
8858 size -= error;
8859 }
8860
8861 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8862 if (error2 < 0)
8863 return error2;
8864 return error + error2;
8865}
8866
Trond Myklebust17f26b12013-08-21 15:48:42 -04008867static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008868 .create = nfs_create,
8869 .lookup = nfs_lookup,
8870 .atomic_open = nfs_atomic_open,
8871 .link = nfs_link,
8872 .unlink = nfs_unlink,
8873 .symlink = nfs_symlink,
8874 .mkdir = nfs_mkdir,
8875 .rmdir = nfs_rmdir,
8876 .mknod = nfs_mknod,
8877 .rename = nfs_rename,
8878 .permission = nfs_permission,
8879 .getattr = nfs_getattr,
8880 .setattr = nfs_setattr,
8881 .getxattr = generic_getxattr,
8882 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008883 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04008884 .removexattr = generic_removexattr,
8885};
8886
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008887static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008888 .permission = nfs_permission,
8889 .getattr = nfs_getattr,
8890 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008891 .getxattr = generic_getxattr,
8892 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008893 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008894 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008895};
8896
David Howells509de812006-08-22 20:06:11 -04008897const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898 .version = 4, /* protocol version */
8899 .dentry_ops = &nfs4_dentry_operations,
8900 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008901 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008902 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008904 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008905 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906 .getattr = nfs4_proc_getattr,
8907 .setattr = nfs4_proc_setattr,
8908 .lookup = nfs4_proc_lookup,
8909 .access = nfs4_proc_access,
8910 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 .create = nfs4_proc_create,
8912 .remove = nfs4_proc_remove,
8913 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008914 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008916 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008917 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008918 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919 .link = nfs4_proc_link,
8920 .symlink = nfs4_proc_symlink,
8921 .mkdir = nfs4_proc_mkdir,
8922 .rmdir = nfs4_proc_remove,
8923 .readdir = nfs4_proc_readdir,
8924 .mknod = nfs4_proc_mknod,
8925 .statfs = nfs4_proc_statfs,
8926 .fsinfo = nfs4_proc_fsinfo,
8927 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008928 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008930 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008932 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008934 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008936 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008937 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008939 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008940 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008941 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008942 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008943 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008944 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008945 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008946 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008947 .create_server = nfs4_create_server,
8948 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949};
8950
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008951static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01008952 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008953 .list = nfs4_xattr_list_nfs4_acl,
8954 .get = nfs4_xattr_get_nfs4_acl,
8955 .set = nfs4_xattr_set_nfs4_acl,
8956};
8957
8958const struct xattr_handler *nfs4_xattr_handlers[] = {
8959 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008960#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8961 &nfs4_xattr_nfs4_label_handler,
8962#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008963 NULL
8964};
8965
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966/*
8967 * Local variables:
8968 * c-basic-offset: 8
8969 * End:
8970 */