blob: 0cc0c319cfdd74bcd60016e78b308ef6f30f6744 [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 Myklebust35dc1d72009-12-05 19:32:19 -0500669 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500670 /* Be nice to the server: try to ensure that the last transmitted
671 * value for highest_user_slotid <= target_highest_slotid
672 */
673 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
674 send_new_highest_used_slotid = true;
675
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500677 send_new_highest_used_slotid = false;
678 goto out_unlock;
679 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500680 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500681
682 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
683 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500684out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500685 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400686 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500687 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400688 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400689 if (waitqueue_active(&tbl->slot_waitq))
690 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400691}
692
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500693int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400697 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500698 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500699 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400700
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500701 if (slot == NULL)
702 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400703 /* don't increment the sequence number if the task wasn't sent */
704 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400705 goto out;
706
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500707 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500708
Trond Myklebustac20d162012-12-15 15:36:07 -0500709 if (slot->interrupted) {
710 slot->interrupted = 0;
711 interrupted = true;
712 }
713
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400714 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500715 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 switch (res->sr_status) {
717 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400718 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500719 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500720 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500721 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500722 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400723 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500724 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500726 case 1:
727 /*
728 * sr_status remains 1 if an RPC level error occurred.
729 * The server may or may not have processed the sequence
730 * operation..
731 * Mark the slot as having hosted an interrupted RPC call.
732 */
733 slot->interrupted = 1;
734 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 case -NFS4ERR_DELAY:
736 /* The server detected a resend of the RPC call and
737 * returned NFS4ERR_DELAY as per Section 2.10.6.2
738 * of RFC5661.
739 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500740 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400741 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500742 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500743 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400744 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500745 case -NFS4ERR_BADSLOT:
746 /*
747 * The slot id we used was probably retired. Try again
748 * using a different slot id.
749 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500750 goto retry_nowait;
751 case -NFS4ERR_SEQ_MISORDERED:
752 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 * Was the last operation on this sequence interrupted?
754 * If so, retry after bumping the sequence number.
755 */
756 if (interrupted) {
757 ++slot->seq_nr;
758 goto retry_nowait;
759 }
760 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500761 * Could this slot have been previously retired?
762 * If so, then the server may be expecting seq_nr = 1!
763 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500764 if (slot->seq_nr != 1) {
765 slot->seq_nr = 1;
766 goto retry_nowait;
767 }
768 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500769 case -NFS4ERR_SEQ_FALSE_RETRY:
770 ++slot->seq_nr;
771 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 default:
773 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500774 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400775 }
776out:
777 /* The session may be reset by one of the error handlers. */
778 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400779 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500780out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500781 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500782retry_nowait:
783 if (rpc_restart_call_prepare(task)) {
784 task->tk_status = 0;
785 ret = 0;
786 }
787 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400788out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400789 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400790 goto out;
791 rpc_delay(task, NFS4_POLL_RETRY_MAX);
792 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400793}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500794EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400795
Peng Tao2c4b1312014-06-11 05:24:16 +0800796int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400797{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500798 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400799 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400800 if (!res->sr_slot->table->session)
801 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400803}
Peng Tao2c4b1312014-06-11 05:24:16 +0800804EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400805
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000806int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 struct nfs4_sequence_args *args,
808 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809 struct rpc_task *task)
810{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400811 struct nfs4_slot *slot;
812 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400813
814 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400815 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400816 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400817 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400818
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400819 tbl = &session->fc_slot_table;
820
Trond Myklebust69d206b2012-11-22 13:21:02 -0500821 task->tk_timeout = 0;
822
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400823 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400824 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400825 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500826 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500827 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400828 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400829 }
830
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500831 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500832 if (IS_ERR(slot)) {
833 /* If out of memory, try again in 1/4 second */
834 if (slot == ERR_PTR(-ENOMEM))
835 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400836 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400837 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400838 }
839 spin_unlock(&tbl->slot_tbl_lock);
840
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500841 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400842
Chuck Levere8d92382013-08-09 12:47:51 -0400843 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500844 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400845
Benny Halevydfb4f3092010-09-24 09:17:01 -0400846 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500847 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400848 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400849 /*
850 * sr_status is only set in decode_sequence, and so will remain
851 * set to 1 if an rpc level failure occurs.
852 */
853 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400854 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400855out_success:
856 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400857 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400858out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400859 /* Privileged tasks are queued with top priority */
860 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400861 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
862 NULL, RPC_PRIORITY_PRIVILEGED);
863 else
864 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400865 spin_unlock(&tbl->slot_tbl_lock);
866 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400867}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000868EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400869
Chuck Lever5a580e02013-08-09 12:48:09 -0400870static int nfs4_setup_sequence(const struct nfs_server *server,
871 struct nfs4_sequence_args *args,
872 struct nfs4_sequence_res *res,
873 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400874{
Trond Myklebust035168a2010-06-16 09:52:26 -0400875 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400876 int ret = 0;
877
Chuck Lever3bd23842013-08-09 12:49:19 -0400878 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800879 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
880 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400881
Chuck Levere8d92382013-08-09 12:47:51 -0400882 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400883 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400884 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400885
Trond Myklebust9d12b212012-01-17 22:04:25 -0500886 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400887
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888 dprintk("<-- %s status=%d\n", __func__, ret);
889 return ret;
890}
891
Andy Adamsonce5039c2009-04-01 09:22:13 -0400892static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
893{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400894 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400895 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400896
Trond Myklebust035168a2010-06-16 09:52:26 -0400897 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
898
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400899 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400900}
901
Andy Adamson69ab40c2009-04-01 09:22:19 -0400902static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
903{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400904 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400905
Trond Myklebust14516c32010-07-31 14:29:06 -0400906 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400907}
908
Trond Myklebust17280172012-03-11 13:11:00 -0400909static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400910 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400911 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400912};
913
Chuck Lever3bd23842013-08-09 12:49:19 -0400914#else /* !CONFIG_NFS_V4_1 */
915
Chuck Lever5a580e02013-08-09 12:48:09 -0400916static int nfs4_setup_sequence(const struct nfs_server *server,
917 struct nfs4_sequence_args *args,
918 struct nfs4_sequence_res *res,
919 struct rpc_task *task)
920{
Peng Taocb04ad22014-06-11 05:24:15 +0800921 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
922 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400923}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400924
Peng Tao2c4b1312014-06-11 05:24:16 +0800925int nfs4_sequence_done(struct rpc_task *task,
926 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400927{
Chuck Lever3bd23842013-08-09 12:49:19 -0400928 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400929}
Peng Tao2c4b1312014-06-11 05:24:16 +0800930EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400931
932#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400933
Chuck Lever9915ea72013-08-09 12:48:27 -0400934static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
935{
936 struct nfs4_call_sync_data *data = calldata;
937 nfs4_setup_sequence(data->seq_server,
938 data->seq_args, data->seq_res, task);
939}
940
941static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
942{
943 struct nfs4_call_sync_data *data = calldata;
944 nfs4_sequence_done(task, data->seq_res);
945}
946
947static const struct rpc_call_ops nfs40_call_sync_ops = {
948 .rpc_call_prepare = nfs40_call_sync_prepare,
949 .rpc_call_done = nfs40_call_sync_done,
950};
951
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400952static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400953 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 struct nfs4_sequence_args *args,
956 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400957{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100958 int ret;
959 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400960 struct nfs_client *clp = server->nfs_client;
961 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100962 .seq_server = server,
963 .seq_args = args,
964 .seq_res = res,
965 };
966 struct rpc_task_setup task_setup = {
967 .rpc_client = clnt,
968 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400969 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100970 .callback_data = &data
971 };
972
973 task = rpc_run_task(&task_setup);
974 if (IS_ERR(task))
975 ret = PTR_ERR(task);
976 else {
977 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400978 rpc_put_task(task);
979 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100980 return ret;
981}
982
Bryan Schumaker7c513052011-03-24 17:12:24 +0000983int nfs4_call_sync(struct rpc_clnt *clnt,
984 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000985 struct rpc_message *msg,
986 struct nfs4_sequence_args *args,
987 struct nfs4_sequence_res *res,
988 int cache_reply)
989{
Chuck Levera9c92d62013-08-09 12:48:18 -0400990 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400991 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000992}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
995{
996 struct nfs_inode *nfsi = NFS_I(dir);
997
998 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400999 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001000 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001002 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001003 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001004 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock(&dir->i_lock);
1006}
1007
1008struct nfs4_opendata {
1009 struct kref kref;
1010 struct nfs_openargs o_arg;
1011 struct nfs_openres o_res;
1012 struct nfs_open_confirmargs c_arg;
1013 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001014 struct nfs4_string owner_name;
1015 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001016 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001018 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001020 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct nfs4_state_owner *owner;
1022 struct nfs4_state *state;
1023 struct iattr attrs;
1024 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001025 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001026 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001027 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001028 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int cancelled;
1030};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001031
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001032static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1033 int err, struct nfs4_exception *exception)
1034{
1035 if (err != -EINVAL)
1036 return false;
1037 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1038 return false;
1039 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1040 exception->retry = 1;
1041 return true;
1042}
1043
Trond Myklebust6ae37332015-01-30 14:21:14 -05001044static u32
1045nfs4_map_atomic_open_share(struct nfs_server *server,
1046 fmode_t fmode, int openflags)
1047{
1048 u32 res = 0;
1049
1050 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1051 case FMODE_READ:
1052 res = NFS4_SHARE_ACCESS_READ;
1053 break;
1054 case FMODE_WRITE:
1055 res = NFS4_SHARE_ACCESS_WRITE;
1056 break;
1057 case FMODE_READ|FMODE_WRITE:
1058 res = NFS4_SHARE_ACCESS_BOTH;
1059 }
1060 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1061 goto out;
1062 /* Want no delegation if we're using O_DIRECT */
1063 if (openflags & O_DIRECT)
1064 res |= NFS4_SHARE_WANT_NO_DELEG;
1065out:
1066 return res;
1067}
1068
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001069static enum open_claim_type4
1070nfs4_map_atomic_open_claim(struct nfs_server *server,
1071 enum open_claim_type4 claim)
1072{
1073 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1074 return claim;
1075 switch (claim) {
1076 default:
1077 return claim;
1078 case NFS4_OPEN_CLAIM_FH:
1079 return NFS4_OPEN_CLAIM_NULL;
1080 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1081 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1082 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1083 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1084 }
1085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001088{
1089 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001090 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001091 p->o_res.seqid = p->o_arg.seqid;
1092 p->c_res.seqid = p->c_arg.seqid;
1093 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001094 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001095 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001096 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001097}
1098
Al Viro82a2c1b2011-06-22 18:30:55 -04001099static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001100 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001101 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001102 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001103 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001104 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001105{
Al Viro82a2c1b2011-06-22 18:30:55 -04001106 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001107 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001108 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001109 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110 struct nfs4_opendata *p;
1111
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001112 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001113 if (p == NULL)
1114 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001115
1116 p->f_label = nfs4_label_alloc(server, gfp_mask);
1117 if (IS_ERR(p->f_label))
1118 goto err_free_p;
1119
Kinglong Meea49c2692015-07-27 15:31:38 +08001120 p->a_label = nfs4_label_alloc(server, gfp_mask);
1121 if (IS_ERR(p->a_label))
1122 goto err_free_f;
1123
Trond Myklebust63f5f792015-01-23 19:19:25 -05001124 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1125 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001126 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001127 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001128 nfs_sb_active(dentry->d_sb);
1129 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001130 p->dir = parent;
1131 p->owner = sp;
1132 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133 p->o_arg.open_flags = flags;
1134 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001135 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1136 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001137 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1138 * will return permission denied for all bits until close */
1139 if (!(flags & O_EXCL)) {
1140 /* ask server to check for all possible rights as results
1141 * are cached */
1142 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1143 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001144 }
David Howells7539bba2006-08-22 20:06:09 -04001145 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001146 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1147 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001148 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001149 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001150 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001151 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001152 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001153 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1154 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001155 case NFS4_OPEN_CLAIM_NULL:
1156 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1157 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1158 p->o_arg.fh = NFS_FH(dir);
1159 break;
1160 case NFS4_OPEN_CLAIM_PREVIOUS:
1161 case NFS4_OPEN_CLAIM_FH:
1162 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1163 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001164 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001165 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001166 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001167 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001168
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 p->o_arg.u.attrs = &p->attrs;
1170 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001171
1172 verf[0] = jiffies;
1173 verf[1] = current->pid;
1174 memcpy(p->o_arg.u.verifier.data, verf,
1175 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001177 p->c_arg.fh = &p->o_res.fh;
1178 p->c_arg.stateid = &p->o_res.stateid;
1179 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001181 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001183
1184err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001185 nfs4_label_free(p->a_label);
1186err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001187 nfs4_label_free(p->f_label);
1188err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 kfree(p);
1190err:
1191 dput(parent);
1192 return NULL;
1193}
1194
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001195static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001197 struct nfs4_opendata *p = container_of(kref,
1198 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001199 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001200
1201 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001202 if (p->state != NULL)
1203 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001204 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001205
Kinglong Meea49c2692015-07-27 15:31:38 +08001206 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001207 nfs4_label_free(p->f_label);
1208
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001209 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001210 dput(p->dentry);
1211 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001212 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001213 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001214 kfree(p);
1215}
1216
1217static void nfs4_opendata_put(struct nfs4_opendata *p)
1218{
1219 if (p != NULL)
1220 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221}
1222
Trond Myklebust06f814a2006-01-03 09:55:07 +01001223static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1224{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001225 int ret;
1226
Trond Myklebust06f814a2006-01-03 09:55:07 +01001227 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001228 return ret;
1229}
1230
Trond Myklebust24311f82015-09-20 10:50:17 -04001231static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1232 fmode_t fmode)
1233{
1234 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1235 case FMODE_READ|FMODE_WRITE:
1236 return state->n_rdwr != 0;
1237 case FMODE_WRITE:
1238 return state->n_wronly != 0;
1239 case FMODE_READ:
1240 return state->n_rdonly != 0;
1241 }
1242 WARN_ON_ONCE(1);
1243 return false;
1244}
1245
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001246static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001247{
1248 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001249
Trond Myklebust536e43d2012-01-17 22:04:26 -05001250 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251 goto out;
1252 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001253 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001254 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1255 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001256 break;
1257 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001258 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1259 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001260 break;
1261 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001262 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1263 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001266 return ret;
1267}
1268
Trond Myklebust2a606182015-08-19 22:30:00 -05001269static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1270 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001271{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001272 if (delegation == NULL)
1273 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001276 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1277 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001278 switch (claim) {
1279 case NFS4_OPEN_CLAIM_NULL:
1280 case NFS4_OPEN_CLAIM_FH:
1281 break;
1282 case NFS4_OPEN_CLAIM_PREVIOUS:
1283 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1284 break;
1285 default:
1286 return 0;
1287 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001288 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001289 return 1;
1290}
1291
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001292static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001293{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001294 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001295 case FMODE_WRITE:
1296 state->n_wronly++;
1297 break;
1298 case FMODE_READ:
1299 state->n_rdonly++;
1300 break;
1301 case FMODE_READ|FMODE_WRITE:
1302 state->n_rdwr++;
1303 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001305}
1306
Trond Myklebust4f14c192014-02-12 19:15:06 -05001307static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001308{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001309 struct nfs_client *clp = state->owner->so_server->nfs_client;
1310 bool need_recover = false;
1311
1312 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1313 need_recover = true;
1314 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1315 need_recover = true;
1316 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1317 need_recover = true;
1318 if (need_recover)
1319 nfs4_state_mark_reclaim_nograce(clp, state);
1320}
1321
Trond Myklebuste999e802014-02-10 18:20:47 -05001322static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1323 nfs4_stateid *stateid)
1324{
1325 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1326 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001327 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1328 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001329 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001330 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001331 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1332 return true;
1333 return false;
1334}
1335
Trond Myklebustf95549c2015-01-23 18:06:09 -05001336static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1337{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001338 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1339 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001340 if (state->n_wronly)
1341 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1342 if (state->n_rdonly)
1343 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1344 if (state->n_rdwr)
1345 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001346 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001347}
1348
Trond Myklebust226056c2014-02-11 10:41:07 -05001349static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001350 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001351 nfs4_stateid *stateid, fmode_t fmode)
1352{
1353 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1354 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1355 case FMODE_WRITE:
1356 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1357 break;
1358 case FMODE_READ:
1359 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1360 break;
1361 case 0:
1362 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1363 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1364 clear_bit(NFS_OPEN_STATE, &state->flags);
1365 }
1366 if (stateid == NULL)
1367 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001368 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001369 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1370 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1371 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001372 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001373 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001374 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001375 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001376 nfs4_stateid_copy(&state->stateid, stateid);
1377 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001378}
1379
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001380static void nfs_clear_open_stateid(struct nfs4_state *state,
1381 nfs4_stateid *arg_stateid,
1382 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001383{
1384 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001385 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001386 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001387 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1388 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001389}
1390
Trond Myklebust003707c2007-07-05 18:07:55 -04001391static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1392{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001393 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001394 case FMODE_READ:
1395 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1396 break;
1397 case FMODE_WRITE:
1398 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1399 break;
1400 case FMODE_READ|FMODE_WRITE:
1401 set_bit(NFS_O_RDWR_STATE, &state->flags);
1402 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001403 if (!nfs_need_update_open_stateid(state, stateid))
1404 return;
1405 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1406 nfs4_stateid_copy(&state->stateid, stateid);
1407 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001408}
1409
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001410static 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 -07001411{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001412 /*
1413 * Protect the call to nfs4_state_set_mode_locked and
1414 * serialise the stateid update
1415 */
Andrew Elble361cad32015-12-02 09:20:57 -05001416 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001417 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001418 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001419 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001420 set_bit(NFS_DELEGATED_STATE, &state->flags);
1421 }
1422 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001424 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001425 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001426 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001429static 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 -05001430{
1431 struct nfs_inode *nfsi = NFS_I(state->inode);
1432 struct nfs_delegation *deleg_cur;
1433 int ret = 0;
1434
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001435 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001436
1437 rcu_read_lock();
1438 deleg_cur = rcu_dereference(nfsi->delegation);
1439 if (deleg_cur == NULL)
1440 goto no_delegation;
1441
1442 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001443 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001444 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001446 goto no_delegation_unlock;
1447
1448 if (delegation == NULL)
1449 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001450 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001451 goto no_delegation_unlock;
1452
Trond Myklebustb7391f42008-12-23 15:21:52 -05001453 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001454 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001455 ret = 1;
1456no_delegation_unlock:
1457 spin_unlock(&deleg_cur->lock);
1458no_delegation:
1459 rcu_read_unlock();
1460
1461 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001463 ret = 1;
1464 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001465 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1466 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001467
1468 return ret;
1469}
1470
Trond Myklebust39071e62015-01-24 15:07:56 -05001471static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1472 const nfs4_stateid *stateid)
1473{
1474 struct nfs4_state *state = lsp->ls_state;
1475 bool ret = false;
1476
1477 spin_lock(&state->state_lock);
1478 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1479 goto out_noupdate;
1480 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1481 goto out_noupdate;
1482 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1483 ret = true;
1484out_noupdate:
1485 spin_unlock(&state->state_lock);
1486 return ret;
1487}
Trond Myklebust34310432008-12-23 15:21:38 -05001488
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001489static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001490{
1491 struct nfs_delegation *delegation;
1492
1493 rcu_read_lock();
1494 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001496 rcu_read_unlock();
1497 return;
1498 }
1499 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001500 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001501}
1502
Trond Myklebust6ee41262007-07-08 14:11:36 -04001503static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001504{
1505 struct nfs4_state *state = opendata->state;
1506 struct nfs_inode *nfsi = NFS_I(state->inode);
1507 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001508 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001509 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001510 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001511 nfs4_stateid stateid;
1512 int ret = -EAGAIN;
1513
Trond Myklebustaac00a82007-07-05 19:02:21 -04001514 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001515 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001516 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001517 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001518 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001519 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001520 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001521 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001522 rcu_read_lock();
1523 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001524 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001525 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001526 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001527 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001528 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001529 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001530 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001531 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001532 if (!opendata->is_recover) {
1533 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1534 if (ret != 0)
1535 goto out;
1536 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001538
1539 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001540 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001541 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543out:
1544 return ERR_PTR(ret);
1545out_return_state:
1546 atomic_inc(&state->count);
1547 return state;
1548}
1549
Andy Adamsone23008e2012-10-02 21:07:32 -04001550static void
1551nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1552{
1553 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1554 struct nfs_delegation *delegation;
1555 int delegation_flags = 0;
1556
1557 rcu_read_lock();
1558 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1559 if (delegation)
1560 delegation_flags = delegation->flags;
1561 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001562 switch (data->o_arg.claim) {
1563 default:
1564 break;
1565 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1566 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001567 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1568 "returning a delegation for "
1569 "OPEN(CLAIM_DELEGATE_CUR)\n",
1570 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001571 return;
1572 }
1573 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001574 nfs_inode_set_delegation(state->inode,
1575 data->owner->so_cred,
1576 &data->o_res);
1577 else
1578 nfs_inode_reclaim_delegation(state->inode,
1579 data->owner->so_cred,
1580 &data->o_res);
1581}
1582
1583/*
1584 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1585 * and update the nfs4_state.
1586 */
1587static struct nfs4_state *
1588_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1589{
1590 struct inode *inode = data->state->inode;
1591 struct nfs4_state *state = data->state;
1592 int ret;
1593
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001594 if (!data->rpc_done) {
1595 if (data->rpc_status) {
1596 ret = data->rpc_status;
1597 goto err;
1598 }
1599 /* cached opens have already been processed */
1600 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001601 }
1602
Andy Adamsone23008e2012-10-02 21:07:32 -04001603 ret = nfs_refresh_inode(inode, &data->f_attr);
1604 if (ret)
1605 goto err;
1606
1607 if (data->o_res.delegation_type != 0)
1608 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001609update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001610 update_open_stateid(state, &data->o_res.stateid, NULL,
1611 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001612 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001613
1614 return state;
1615err:
1616 return ERR_PTR(ret);
1617
1618}
1619
1620static struct nfs4_state *
1621_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622{
1623 struct inode *inode;
1624 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001625 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001628 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001629 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001630 goto out;
1631 }
1632
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001633 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001635 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001636 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001637 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001638 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001639 goto err;
1640 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 state = nfs4_get_open_state(inode, data->owner);
1642 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001643 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001644 if (data->o_res.delegation_type != 0)
1645 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001646 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001649out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001650 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001651 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001652err_put_inode:
1653 iput(inode);
1654err:
1655 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656}
1657
Andy Adamsone23008e2012-10-02 21:07:32 -04001658static struct nfs4_state *
1659nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1660{
1661 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1662 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1663 return _nfs4_opendata_to_nfs4_state(data);
1664}
1665
Trond Myklebust864472e2006-01-03 09:55:15 +01001666static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1667{
1668 struct nfs_inode *nfsi = NFS_I(state->inode);
1669 struct nfs_open_context *ctx;
1670
1671 spin_lock(&state->inode->i_lock);
1672 list_for_each_entry(ctx, &nfsi->open_files, list) {
1673 if (ctx->state != state)
1674 continue;
1675 get_nfs_open_context(ctx);
1676 spin_unlock(&state->inode->i_lock);
1677 return ctx;
1678 }
1679 spin_unlock(&state->inode->i_lock);
1680 return ERR_PTR(-ENOENT);
1681}
1682
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001683static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1684 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001685{
1686 struct nfs4_opendata *opendata;
1687
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001688 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001689 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001690 if (opendata == NULL)
1691 return ERR_PTR(-ENOMEM);
1692 opendata->state = state;
1693 atomic_inc(&state->count);
1694 return opendata;
1695}
1696
Trond Myklebust24311f82015-09-20 10:50:17 -04001697static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1698 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001699{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001700 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001701 int ret;
1702
Trond Myklebust24311f82015-09-20 10:50:17 -04001703 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001704 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001705 opendata->o_arg.open_flags = 0;
1706 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001707 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1708 NFS_SB(opendata->dentry->d_sb),
1709 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001710 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1711 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1712 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001713 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 if (ret != 0)
1715 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001716 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001717 if (IS_ERR(newstate))
1718 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001719 if (newstate != opendata->state)
1720 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001721 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001722 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001723}
1724
1725static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1726{
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 int ret;
1728
Trond Myklebust4f14c192014-02-12 19:15:06 -05001729 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1730 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1731 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1732 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001733 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001734 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001735 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001736 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001737 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1738 if (ret != 0)
1739 return ret;
1740 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1741 if (ret != 0)
1742 return ret;
1743 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1744 if (ret != 0)
1745 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001746 /*
1747 * We may have performed cached opens for all three recoveries.
1748 * Check if we need to update the current stateid.
1749 */
1750 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001751 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001752 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001753 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001754 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001755 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001756 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 return 0;
1758}
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760/*
1761 * OPEN_RECLAIM:
1762 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001764static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001766 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001767 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001768 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int status;
1770
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001771 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1772 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001773 if (IS_ERR(opendata))
1774 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001775 rcu_read_lock();
1776 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001777 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001778 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001779 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 opendata->o_arg.u.delegation_type = delegation_type;
1781 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001782 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return status;
1784}
1785
Trond Myklebust539cd032007-06-05 11:46:42 -04001786static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct nfs_server *server = NFS_SERVER(state->inode);
1789 struct nfs4_exception exception = { };
1790 int err;
1791 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001792 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001793 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001794 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1795 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001796 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001797 break;
1798 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 } while (exception.retry);
1800 return err;
1801}
1802
Trond Myklebust864472e2006-01-03 09:55:15 +01001803static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1804{
1805 struct nfs_open_context *ctx;
1806 int ret;
1807
1808 ctx = nfs4_state_find_open_context(state);
1809 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001810 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001811 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001812 put_nfs_open_context(ctx);
1813 return ret;
1814}
1815
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001816static 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 -07001817{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001818 switch (err) {
1819 default:
1820 printk(KERN_ERR "NFS: %s: unhandled error "
1821 "%d.\n", __func__, err);
1822 case 0:
1823 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001824 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001825 case -ESTALE:
1826 break;
1827 case -NFS4ERR_BADSESSION:
1828 case -NFS4ERR_BADSLOT:
1829 case -NFS4ERR_BAD_HIGH_SLOT:
1830 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1831 case -NFS4ERR_DEADSESSION:
1832 set_bit(NFS_DELEGATED_STATE, &state->flags);
1833 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1834 return -EAGAIN;
1835 case -NFS4ERR_STALE_CLIENTID:
1836 case -NFS4ERR_STALE_STATEID:
1837 set_bit(NFS_DELEGATED_STATE, &state->flags);
1838 case -NFS4ERR_EXPIRED:
1839 /* Don't recall a delegation if it was lost */
1840 nfs4_schedule_lease_recovery(server->nfs_client);
1841 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001842 case -NFS4ERR_MOVED:
1843 nfs4_schedule_migration_recovery(server);
1844 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001845 case -NFS4ERR_LEASE_MOVED:
1846 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1847 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001848 case -NFS4ERR_DELEG_REVOKED:
1849 case -NFS4ERR_ADMIN_REVOKED:
1850 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001851 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001852 nfs_inode_find_state_and_recover(state->inode,
1853 stateid);
1854 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001855 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001856 case -NFS4ERR_DELAY:
1857 case -NFS4ERR_GRACE:
1858 set_bit(NFS_DELEGATED_STATE, &state->flags);
1859 ssleep(1);
1860 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001861 case -ENOMEM:
1862 case -NFS4ERR_DENIED:
1863 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1864 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return err;
1867}
1868
Trond Myklebust24311f82015-09-20 10:50:17 -04001869int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1870 struct nfs4_state *state, const nfs4_stateid *stateid,
1871 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001872{
1873 struct nfs_server *server = NFS_SERVER(state->inode);
1874 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001875 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001876
1877 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1878 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1879 if (IS_ERR(opendata))
1880 return PTR_ERR(opendata);
1881 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001882 write_seqlock(&state->seqlock);
1883 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1884 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001885 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1886 switch (type & (FMODE_READ|FMODE_WRITE)) {
1887 case FMODE_READ|FMODE_WRITE:
1888 case FMODE_WRITE:
1889 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1890 if (err)
1891 break;
1892 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1893 if (err)
1894 break;
1895 case FMODE_READ:
1896 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1897 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001898 nfs4_opendata_put(opendata);
1899 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1900}
1901
Chuck Leverbe05c862013-08-09 12:49:47 -04001902static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1903{
1904 struct nfs4_opendata *data = calldata;
1905
Peng Taocb04ad22014-06-11 05:24:15 +08001906 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1907 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001908}
1909
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001910static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1911{
1912 struct nfs4_opendata *data = calldata;
1913
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001914 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001915
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001916 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001917 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001918 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001919 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001920 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001921 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001922 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001923}
1924
1925static void nfs4_open_confirm_release(void *calldata)
1926{
1927 struct nfs4_opendata *data = calldata;
1928 struct nfs4_state *state = NULL;
1929
1930 /* If this request hasn't been cancelled, do nothing */
1931 if (data->cancelled == 0)
1932 goto out_free;
1933 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001934 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001935 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001936 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001937 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001938 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001939out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001940 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001941}
1942
1943static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001944 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001945 .rpc_call_done = nfs4_open_confirm_done,
1946 .rpc_release = nfs4_open_confirm_release,
1947};
1948
1949/*
1950 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1951 */
1952static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1953{
David Howells2b0143b2015-03-17 22:25:59 +00001954 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001955 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001956 struct rpc_message msg = {
1957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1958 .rpc_argp = &data->c_arg,
1959 .rpc_resp = &data->c_res,
1960 .rpc_cred = data->owner->so_cred,
1961 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001962 struct rpc_task_setup task_setup_data = {
1963 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001964 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001965 .callback_ops = &nfs4_open_confirm_ops,
1966 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001967 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001968 .flags = RPC_TASK_ASYNC,
1969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 int status;
1971
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001972 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001973 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001974 data->rpc_done = 0;
1975 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001976 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001977 if (data->is_recover)
1978 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001979 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001980 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001981 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982 status = nfs4_wait_for_completion_rpc_task(task);
1983 if (status != 0) {
1984 data->cancelled = 1;
1985 smp_wmb();
1986 } else
1987 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001988 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return status;
1990}
1991
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001992static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994 struct nfs4_opendata *data = calldata;
1995 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001996 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001997 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001998
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001999 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002000 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002001 /*
2002 * Check if we still need to send an OPEN call, or if we can use
2003 * a delegation instead.
2004 */
2005 if (data->state != NULL) {
2006 struct nfs_delegation *delegation;
2007
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002008 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002009 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002010 rcu_read_lock();
2011 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002012 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002013 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002014 rcu_read_unlock();
2015 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002016 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002017 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002018 switch (claim) {
2019 default:
2020 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002021 case NFS4_OPEN_CLAIM_PREVIOUS:
2022 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2023 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002024 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002025 case NFS4_OPEN_CLAIM_FH:
2026 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002027 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2028 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002029 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002030 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002031 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002032 &data->o_res.seq_res,
2033 task) != 0)
2034 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002035
2036 /* Set the create mode (note dependency on the session type) */
2037 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2038 if (data->o_arg.open_flags & O_EXCL) {
2039 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2040 if (nfs4_has_persistent_session(clp))
2041 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2042 else if (clp->cl_mvops->minor_version > 0)
2043 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2044 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002045 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002046unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002047 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002048 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002049out_no_action:
2050 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002051out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002052 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002053}
2054
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002055static void nfs4_open_done(struct rpc_task *task, void *calldata)
2056{
2057 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002059 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002060
Trond Myklebust14516c32010-07-31 14:29:06 -04002061 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2062 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002063
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002064 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002065 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2066 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002067 case S_IFREG:
2068 break;
2069 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002070 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002071 break;
2072 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002073 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002074 break;
2075 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002076 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002077 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002078 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002079 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002080 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2081 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002082 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002083 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002084}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002085
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002086static void nfs4_open_release(void *calldata)
2087{
2088 struct nfs4_opendata *data = calldata;
2089 struct nfs4_state *state = NULL;
2090
2091 /* If this request hasn't been cancelled, do nothing */
2092 if (data->cancelled == 0)
2093 goto out_free;
2094 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002095 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002096 goto out_free;
2097 /* In case we need an open_confirm, no cleanup! */
2098 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2099 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002100 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002101 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002102 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002103out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002104 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002105}
2106
2107static const struct rpc_call_ops nfs4_open_ops = {
2108 .rpc_call_prepare = nfs4_open_prepare,
2109 .rpc_call_done = nfs4_open_done,
2110 .rpc_release = nfs4_open_release,
2111};
2112
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002113static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002114{
David Howells2b0143b2015-03-17 22:25:59 +00002115 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 struct nfs_server *server = NFS_SERVER(dir);
2117 struct nfs_openargs *o_arg = &data->o_arg;
2118 struct nfs_openres *o_res = &data->o_res;
2119 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002120 struct rpc_message msg = {
2121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2122 .rpc_argp = o_arg,
2123 .rpc_resp = o_res,
2124 .rpc_cred = data->owner->so_cred,
2125 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002126 struct rpc_task_setup task_setup_data = {
2127 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002128 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002129 .callback_ops = &nfs4_open_ops,
2130 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002131 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002132 .flags = RPC_TASK_ASYNC,
2133 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002134 int status;
2135
Chuck Levera9c92d62013-08-09 12:48:18 -04002136 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002137 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002138 data->rpc_done = 0;
2139 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002140 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002141 data->is_recover = 0;
2142 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002143 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002144 data->is_recover = 1;
2145 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002146 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002147 if (IS_ERR(task))
2148 return PTR_ERR(task);
2149 status = nfs4_wait_for_completion_rpc_task(task);
2150 if (status != 0) {
2151 data->cancelled = 1;
2152 smp_wmb();
2153 } else
2154 status = data->rpc_status;
2155 rpc_put_task(task);
2156
2157 return status;
2158}
2159
2160static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002163 struct nfs_openres *o_res = &data->o_res;
2164 int status;
2165
2166 status = nfs4_run_open_task(data, 1);
2167 if (status != 0 || !data->rpc_done)
2168 return status;
2169
Trond Myklebust6926afd2012-01-07 13:22:46 -05002170 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2171
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002172 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2173 status = _nfs4_proc_open_confirm(data);
2174 if (status != 0)
2175 return status;
2176 }
2177
2178 return status;
2179}
2180
Trond Myklebustf3792d62014-07-10 08:54:32 -04002181/*
2182 * Additional permission checks in order to distinguish between an
2183 * open for read, and an open for execute. This works around the
2184 * fact that NFSv4 OPEN treats read and execute permissions as being
2185 * the same.
2186 * Note that in the non-execute case, we want to turn off permission
2187 * checking if we just created a new file (POSIX open() semantics).
2188 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002189static int nfs4_opendata_access(struct rpc_cred *cred,
2190 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002191 struct nfs4_state *state, fmode_t fmode,
2192 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002193{
2194 struct nfs_access_entry cache;
2195 u32 mask;
2196
2197 /* access call failed or for some reason the server doesn't
2198 * support any access modes -- defer access call until later */
2199 if (opendata->o_res.access_supported == 0)
2200 return 0;
2201
2202 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002203 /*
2204 * Use openflags to check for exec, because fmode won't
2205 * always have FMODE_EXEC set when file open for exec.
2206 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002207 if (openflags & __FMODE_EXEC) {
2208 /* ONLY check for exec rights */
2209 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002210 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002211 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002212
2213 cache.cred = cred;
2214 cache.jiffies = jiffies;
2215 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2216 nfs_access_add_cache(state->inode, &cache);
2217
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002218 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002219 return 0;
2220
2221 /* even though OPEN succeeded, access is denied. Close the file */
2222 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002223 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002224}
2225
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002226/*
2227 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2228 */
2229static int _nfs4_proc_open(struct nfs4_opendata *data)
2230{
David Howells2b0143b2015-03-17 22:25:59 +00002231 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002232 struct nfs_server *server = NFS_SERVER(dir);
2233 struct nfs_openargs *o_arg = &data->o_arg;
2234 struct nfs_openres *o_res = &data->o_res;
2235 int status;
2236
2237 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002238 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002240 if (status != 0) {
2241 if (status == -NFS4ERR_BADNAME &&
2242 !(o_arg->open_flags & O_CREAT))
2243 return -ENOENT;
2244 return status;
2245 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002246
Trond Myklebust6926afd2012-01-07 13:22:46 -05002247 nfs_fattr_map_and_free_names(server, &data->f_attr);
2248
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002249 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002250 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002251 if (o_arg->open_flags & O_EXCL)
2252 data->file_created = 1;
2253 else if (o_res->cinfo.before != o_res->cinfo.after)
2254 data->file_created = 1;
2255 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002256 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2257 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002259 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002264 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Andy Adamsond83217c2011-03-01 01:34:17 +00002268static int nfs4_recover_expired_lease(struct nfs_server *server)
2269{
2270 return nfs4_client_recover_expired_lease(server->nfs_client);
2271}
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273/*
2274 * OPEN_EXPIRED:
2275 * reclaim state on the server after a network partition.
2276 * Assumes caller holds the appropriate lock
2277 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002278static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002280 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002283 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002284 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002285 if (IS_ERR(opendata))
2286 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002287 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002288 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002289 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002290 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002294static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002295{
Trond Myklebust539cd032007-06-05 11:46:42 -04002296 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002297 struct nfs4_exception exception = { };
2298 int err;
2299
2300 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002301 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002302 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002303 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2304 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002305 switch (err) {
2306 default:
2307 goto out;
2308 case -NFS4ERR_GRACE:
2309 case -NFS4ERR_DELAY:
2310 nfs4_handle_exception(server, err, &exception);
2311 err = 0;
2312 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002313 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002314out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002315 return err;
2316}
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002321 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Trond Myklebust864472e2006-01-03 09:55:15 +01002323 ctx = nfs4_state_find_open_context(state);
2324 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002325 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002326 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002327 put_nfs_open_context(ctx);
2328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002331static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2332{
2333 nfs_remove_bad_delegation(state->inode);
2334 write_seqlock(&state->seqlock);
2335 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2336 write_sequnlock(&state->seqlock);
2337 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2338}
2339
2340static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2341{
2342 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2343 nfs_finish_clear_delegation_stateid(state);
2344}
2345
2346static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2347{
2348 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2349 nfs40_clear_delegation_stateid(state);
2350 return nfs4_open_expired(sp, state);
2351}
2352
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002353#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002354static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002355{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002356 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002357 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002358 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002359 struct rpc_cred *cred;
2360 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002361
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002362 /* Get the delegation credential for use by test/free_stateid */
2363 rcu_read_lock();
2364 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002365 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002366 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002367 return;
2368 }
2369
2370 nfs4_stateid_copy(&stateid, &delegation->stateid);
2371 cred = get_rpccred(delegation->cred);
2372 rcu_read_unlock();
2373 status = nfs41_test_stateid(server, &stateid, cred);
2374 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002375
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002376 if (status != NFS_OK) {
2377 /* Free the stateid unless the server explicitly
2378 * informs us the stateid is unrecognized. */
2379 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002380 nfs41_free_stateid(server, &stateid, cred);
2381 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002382 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002383
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002384 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002385}
2386
2387/**
2388 * nfs41_check_open_stateid - possibly free an open stateid
2389 *
2390 * @state: NFSv4 state for an inode
2391 *
2392 * Returns NFS_OK if recovery for this stateid is now finished.
2393 * Otherwise a negative NFS4ERR value is returned.
2394 */
2395static int nfs41_check_open_stateid(struct nfs4_state *state)
2396{
2397 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002398 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002399 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002400 int status;
2401
2402 /* If a state reset has been done, test_stateid is unneeded */
2403 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2404 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2405 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2406 return -NFS4ERR_BAD_STATEID;
2407
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002408 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002409 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002410 if (status != NFS_OK) {
2411 /* Free the stateid unless the server explicitly
2412 * informs us the stateid is unrecognized. */
2413 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002414 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002415
2416 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2417 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2418 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002419 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002420 }
2421 return status;
2422}
2423
2424static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2425{
Chuck Levereb64cf92012-07-11 16:30:05 -04002426 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002427
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002428 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002429 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002430 if (status != NFS_OK)
2431 status = nfs4_open_expired(sp, state);
2432 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002433}
2434#endif
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002437 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2438 * fields corresponding to attributes that were used to store the verifier.
2439 * Make sure we clobber those fields in the later setattr call
2440 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002441static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2442 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002443{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002444 const u32 *attrset = opendata->o_res.attrset;
2445
2446 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002447 !(sattr->ia_valid & ATTR_ATIME_SET))
2448 sattr->ia_valid |= ATTR_ATIME;
2449
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002450 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002451 !(sattr->ia_valid & ATTR_MTIME_SET))
2452 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002453
2454 /* Except MODE, it seems harmless of setting twice. */
2455 if ((attrset[1] & FATTR4_WORD1_MODE))
2456 sattr->ia_valid &= ~ATTR_MODE;
2457
2458 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2459 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002460}
2461
Trond Myklebustc21443c2013-02-07 14:26:21 -05002462static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2463 fmode_t fmode,
2464 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002465 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002466{
2467 struct nfs4_state_owner *sp = opendata->owner;
2468 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002469 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002470 struct nfs4_state *state;
2471 unsigned int seq;
2472 int ret;
2473
2474 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2475
2476 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002477 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002478 goto out;
2479
2480 state = nfs4_opendata_to_nfs4_state(opendata);
2481 ret = PTR_ERR(state);
2482 if (IS_ERR(state))
2483 goto out;
2484 if (server->caps & NFS_CAP_POSIX_LOCK)
2485 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2486
Trond Myklebust275bb302013-05-29 13:11:28 -04002487 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002488 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002489 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002490 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002491 alias = d_exact_alias(dentry, state->inode);
2492 if (!alias)
2493 alias = d_splice_alias(igrab(state->inode), dentry);
2494 /* d_splice_alias() can't fail here - it's a non-directory */
2495 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002496 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002497 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002498 }
2499 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002500 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002501 }
2502
Trond Myklebustc21443c2013-02-07 14:26:21 -05002503 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2504 if (ret != 0)
2505 goto out;
2506
Trond Myklebust3efb9722013-05-29 13:17:04 -04002507 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002508 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002509 nfs_inode_attach_open_context(ctx);
2510 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2511 nfs4_schedule_stateid_recovery(server, state);
2512 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002513out:
2514 return ret;
2515}
2516
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002517/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002518 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
Andy Adamson82be4172012-05-23 05:02:35 -04002520static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002521 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002522 int flags,
2523 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002524 struct nfs4_label *label,
2525 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 struct nfs4_state_owner *sp;
2528 struct nfs4_state *state = NULL;
2529 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002530 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002531 struct dentry *dentry = ctx->dentry;
2532 struct rpc_cred *cred = ctx->cred;
2533 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2534 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002535 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002536 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002537 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002541 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2542 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2544 goto out_err;
2545 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002546 status = nfs4_recover_expired_lease(server);
2547 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002548 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002549 if (d_really_is_positive(dentry))
2550 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002551 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002552 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002553 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002554 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002555 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002556 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002557 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
David Quigley14c43f72013-05-22 12:50:43 -04002559 if (label) {
2560 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2561 if (IS_ERR(olabel)) {
2562 status = PTR_ERR(olabel);
2563 goto err_opendata_put;
2564 }
2565 }
2566
Trond Myklebuste911b812014-03-26 13:24:37 -07002567 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2568 if (!opendata->f_attr.mdsthreshold) {
2569 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2570 if (!opendata->f_attr.mdsthreshold)
2571 goto err_free_label;
2572 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002573 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002574 }
David Howells2b0143b2015-03-17 22:25:59 +00002575 if (d_really_is_positive(dentry))
2576 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002577
Trond Myklebust3efb9722013-05-29 13:17:04 -04002578 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002579 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002580 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002581 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002582
NeilBrownefcbc042015-07-30 13:00:56 +10002583 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002584 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002585 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002586 /*
2587 * send create attributes which was not set by open
2588 * with an extra setattr.
2589 */
2590 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2591 nfs_fattr_init(opendata->o_res.f_attr);
2592 status = nfs4_do_setattr(state->inode, cred,
2593 opendata->o_res.f_attr, sattr,
2594 state, label, olabel);
2595 if (status == 0) {
2596 nfs_setattr_update_inode(state->inode, sattr,
2597 opendata->o_res.f_attr);
2598 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2599 }
David Quigley1775fd32013-05-22 12:50:42 -04002600 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002601 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002602 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002603 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002604
Trond Myklebuste911b812014-03-26 13:24:37 -07002605 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002606 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002607 opendata->f_attr.mdsthreshold = NULL;
2608 }
Andy Adamson82be4172012-05-23 05:02:35 -04002609
David Quigley14c43f72013-05-22 12:50:43 -04002610 nfs4_label_free(olabel);
2611
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002612 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002615err_free_label:
2616 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002617err_opendata_put:
2618 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002619err_put_state_owner:
2620 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return status;
2623}
2624
2625
Andy Adamson82be4172012-05-23 05:02:35 -04002626static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002627 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002628 int flags,
2629 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002630 struct nfs4_label *label,
2631 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002633 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 struct nfs4_exception exception = { };
2635 struct nfs4_state *res;
2636 int status;
2637
2638 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002639 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002640 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002641 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (status == 0)
2643 break;
2644 /* NOTE: BAD_SEQID means the server and client disagree about the
2645 * book-keeping w.r.t. state-changing operations
2646 * (OPEN/CLOSE/LOCK/LOCKU...)
2647 * It is actually a sign of a bug on the client or on the server.
2648 *
2649 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002650 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 * have unhashed the old state_owner for us, and that we can
2652 * therefore safely retry using a new one. We should still warn
2653 * the user though...
2654 */
2655 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002656 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002657 " returned a bad sequence-id error!\n",
2658 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 exception.retry = 1;
2660 continue;
2661 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002662 /*
2663 * BAD_STATEID on OPEN means that the server cancelled our
2664 * state before it received the OPEN_CONFIRM.
2665 * Recover by retrying the request as per the discussion
2666 * on Page 181 of RFC3530.
2667 */
2668 if (status == -NFS4ERR_BAD_STATEID) {
2669 exception.retry = 1;
2670 continue;
2671 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002672 if (status == -EAGAIN) {
2673 /* We must have found a delegation */
2674 exception.retry = 1;
2675 continue;
2676 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002677 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2678 continue;
2679 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 status, &exception));
2681 } while (exception.retry);
2682 return res;
2683}
2684
Trond Myklebust8487c472016-06-26 08:44:35 -04002685static int _nfs4_do_setattr(struct inode *inode,
2686 struct nfs_setattrargs *arg,
2687 struct nfs_setattrres *res,
2688 struct rpc_cred *cred,
2689 struct nfs4_state *state)
2690{
2691 struct nfs_server *server = NFS_SERVER(inode);
2692 struct rpc_message msg = {
2693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2694 .rpc_argp = arg,
2695 .rpc_resp = res,
2696 .rpc_cred = cred,
2697 };
2698 struct rpc_cred *delegation_cred = NULL;
2699 unsigned long timestamp = jiffies;
2700 fmode_t fmode;
2701 bool truncate;
2702 int status;
2703
2704 nfs_fattr_init(res->fattr);
2705
2706 /* Servers should only apply open mode checks for file size changes */
2707 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2708 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2709
2710 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2711 /* Use that stateid */
2712 } else if (truncate && state != NULL) {
2713 struct nfs_lockowner lockowner = {
2714 .l_owner = current->files,
2715 .l_pid = current->tgid,
2716 };
2717 if (!nfs4_valid_open_stateid(state))
2718 return -EBADF;
2719 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2720 &arg->stateid, &delegation_cred) == -EIO)
2721 return -EBADF;
2722 } else
2723 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2724 if (delegation_cred)
2725 msg.rpc_cred = delegation_cred;
2726
2727 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2728
2729 put_rpccred(delegation_cred);
2730 if (status == 0 && state != NULL)
2731 renew_lease(server, timestamp);
2732 trace_nfs4_setattr(inode, &arg->stateid, status);
2733 return status;
2734}
2735
2736static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2737 struct nfs_fattr *fattr, struct iattr *sattr,
2738 struct nfs4_state *state, struct nfs4_label *ilabel,
2739 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002741 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002743 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 .iap = sattr,
2745 .server = server,
2746 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002747 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 };
2749 struct nfs_setattrres res = {
2750 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002751 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .server = server,
2753 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002754 struct nfs4_exception exception = {
2755 .state = state,
2756 .inode = inode,
2757 .stateid = &arg.stateid,
2758 };
2759 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
David Quigleyaa9c2662013-05-22 12:50:44 -04002761 arg.bitmask = nfs4_bitmask(server, ilabel);
2762 if (ilabel)
2763 arg.bitmask = nfs4_bitmask(server, olabel);
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002766 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002767 switch (err) {
2768 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002769 if (!(sattr->ia_valid & ATTR_SIZE)) {
2770 pr_warn_once("NFSv4: server %s is incorrectly "
2771 "applying open mode checks to "
2772 "a SETATTR that is not "
2773 "changing file size.\n",
2774 server->nfs_client->cl_hostname);
2775 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002776 if (state && !(state->state & FMODE_WRITE)) {
2777 err = -EBADF;
2778 if (sattr->ia_valid & ATTR_OPEN)
2779 err = -EACCES;
2780 goto out;
2781 }
2782 }
2783 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002785out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return err;
2787}
2788
Peng Tao500d7012015-09-22 11:35:22 +08002789static bool
2790nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2791{
2792 if (inode == NULL || !nfs_have_layout(inode))
2793 return false;
2794
2795 return pnfs_wait_on_layoutreturn(inode, task);
2796}
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798struct nfs4_closedata {
2799 struct inode *inode;
2800 struct nfs4_state *state;
2801 struct nfs_closeargs arg;
2802 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002803 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002804 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002805 bool roc;
2806 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807};
2808
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002809static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002810{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002811 struct nfs4_closedata *calldata = data;
2812 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002813 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002814
Fred Isamanf7e89172011-01-06 11:36:32 +00002815 if (calldata->roc)
2816 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002817 nfs4_put_open_state(calldata->state);
2818 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002819 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002820 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002821 kfree(calldata);
2822}
2823
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002824static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002826 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002829 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Chuck Levera3ca5652012-03-01 17:00:40 -05002831 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002832 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2833 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002834 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 /* hmm. we are done with the inode, and in the process of freeing
2836 * the state_owner. we keep this around to process errors
2837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 switch (task->tk_status) {
2839 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002840 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002841 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002842 pnfs_roc_set_barrier(state->inode,
2843 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002844 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002845 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002846 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002848 case -NFS4ERR_OLD_STATEID:
2849 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002851 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002852 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002853 rpc_restart_call_prepare(task);
2854 goto out_release;
2855 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002856 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002859 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002860 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002861 goto out_release;
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002864 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2865 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002866out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002867 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002868 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002869 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002872static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002874 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002875 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002876 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002877 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002878 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002879
Chuck Levera3ca5652012-03-01 17:00:40 -05002880 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002881 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002882 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002883
Trond Myklebust88069f72009-12-08 08:33:16 -05002884 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002885 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002886 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2887 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2888 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002889 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002890 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002891 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002892 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002893 if (state->n_rdonly == 0)
2894 call_close |= is_rdonly;
2895 else if (is_rdonly)
2896 calldata->arg.fmode |= FMODE_READ;
2897 if (state->n_wronly == 0)
2898 call_close |= is_wronly;
2899 else if (is_wronly)
2900 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002901 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2902 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002903 } else if (is_rdwr)
2904 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2905
Trond Myklebust5d422302013-03-14 16:57:48 -04002906 if (!nfs4_valid_open_stateid(state))
2907 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002908 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002909
2910 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002911 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002912 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002913 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002914
Peng Tao500d7012015-09-22 11:35:22 +08002915 if (nfs4_wait_on_layoutreturn(inode, task)) {
2916 nfs_release_seqid(calldata->arg.seqid);
2917 goto out_wait;
2918 }
2919
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002920 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002921 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002922 if (calldata->roc)
2923 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2924
Trond Myklebust6ae37332015-01-30 14:21:14 -05002925 calldata->arg.share_access =
2926 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2927 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002928
Trond Myklebust516a6af2005-10-27 22:12:41 -04002929 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002930 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002931 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002932 &calldata->arg.seq_args,
2933 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002934 task) != 0)
2935 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002936 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002937 return;
2938out_no_action:
2939 task->tk_action = NULL;
2940out_wait:
2941 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002944static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002945 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002946 .rpc_call_done = nfs4_close_done,
2947 .rpc_release = nfs4_free_closedata,
2948};
2949
Peng Taofe08c542014-07-03 13:05:01 +08002950static bool nfs4_roc(struct inode *inode)
2951{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002952 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002953 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002954 return pnfs_roc(inode);
2955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957/*
2958 * It is possible for data to be read/written from a mem-mapped file
2959 * after the sys_close call (which hits the vfs layer as a flush).
2960 * This means that we can't safely call nfsv4 close on a file until
2961 * the inode is cleared. This in turn means that we are not good
2962 * NFSv4 citizens - we do not indicate to the server to update the file's
2963 * share state even when we are done with one of the three share
2964 * stateid's in the inode.
2965 *
2966 * NOTE: Caller must be holding the sp->so_owner semaphore!
2967 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002968int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002970 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002971 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002973 struct nfs4_state_owner *sp = state->owner;
2974 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002975 struct rpc_message msg = {
2976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2977 .rpc_cred = state->owner->so_cred,
2978 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002979 struct rpc_task_setup task_setup_data = {
2980 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002981 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002982 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002983 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002984 .flags = RPC_TASK_ASYNC,
2985 };
Trond Myklebust95121352005-10-18 14:20:12 -07002986 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002988 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2989 &task_setup_data.rpc_client, &msg);
2990
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002991 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002993 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002994 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002995 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002997 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002999 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3000 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003001 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003002 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003003 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003004 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003005 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003006 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003007 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003008 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003009 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003010
Trond Myklebust1174dd12010-12-21 10:52:24 -05003011 msg.rpc_argp = &calldata->arg;
3012 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003013 task_setup_data.callback_data = calldata;
3014 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003015 if (IS_ERR(task))
3016 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003017 status = 0;
3018 if (wait)
3019 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003020 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003021 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003022out_free_calldata:
3023 kfree(calldata);
3024out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003025 nfs4_put_open_state(state);
3026 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Trond Myklebust2b484292010-09-17 10:56:51 -04003030static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003031nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3032 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003035 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3036
3037 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Trond Myklebust565277f2007-10-15 18:17:53 -04003039 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003040 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003041
3042 nfs4_label_release_security(label);
3043
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003044 if (IS_ERR(state))
3045 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003046 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Trond Myklebust1185a552009-12-03 15:54:02 -05003049static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003050{
3051 if (ctx->state == NULL)
3052 return;
3053 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003054 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003055 else
Al Viro643168c2011-06-22 18:20:23 -04003056 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Trond Myklebustb944dba2013-11-04 15:20:20 -05003059#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3060#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003061#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3064{
Kinglong Mee8c612822015-08-26 21:12:58 +08003065 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003066 struct nfs4_server_caps_arg args = {
3067 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003068 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct nfs4_server_caps_res res = {};
3071 struct rpc_message msg = {
3072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003073 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .rpc_resp = &res,
3075 };
3076 int status;
3077
Kinglong Mee8c612822015-08-26 21:12:58 +08003078 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3079 FATTR4_WORD0_FH_EXPIRE_TYPE |
3080 FATTR4_WORD0_LINK_SUPPORT |
3081 FATTR4_WORD0_SYMLINK_SUPPORT |
3082 FATTR4_WORD0_ACLSUPPORT;
3083 if (minorversion)
3084 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3085
Bryan Schumaker7c513052011-03-24 17:12:24 +00003086 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003088 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003089 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003090 case 0:
3091 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3092 res.attr_bitmask[2] = 0;
3093 break;
3094 case 1:
3095 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3096 break;
3097 case 2:
3098 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003101 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3102 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3103 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3104 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003105 NFS_CAP_CTIME|NFS_CAP_MTIME|
3106 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003107 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3108 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 server->caps |= NFS_CAP_ACLS;
3110 if (res.has_links != 0)
3111 server->caps |= NFS_CAP_HARDLINKS;
3112 if (res.has_symlinks != 0)
3113 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003114 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3115 server->caps |= NFS_CAP_FILEID;
3116 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3117 server->caps |= NFS_CAP_MODE;
3118 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3119 server->caps |= NFS_CAP_NLINK;
3120 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3121 server->caps |= NFS_CAP_OWNER;
3122 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3123 server->caps |= NFS_CAP_OWNER_GROUP;
3124 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3125 server->caps |= NFS_CAP_ATIME;
3126 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3127 server->caps |= NFS_CAP_CTIME;
3128 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3129 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003130#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3131 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3132 server->caps |= NFS_CAP_SECURITY_LABEL;
3133#endif
3134 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3135 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003136 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003137
Trond Myklebusta65318b2009-03-11 14:10:28 -04003138 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3139 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3140 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003141 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003142 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3143 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003145 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return status;
3149}
3150
Trond Myklebust55a97592006-06-09 09:34:19 -04003151int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct nfs4_exception exception = { };
3154 int err;
3155 do {
3156 err = nfs4_handle_exception(server,
3157 _nfs4_server_capabilities(server, fhandle),
3158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3164 struct nfs_fsinfo *info)
3165{
David Quigleyaa9c2662013-05-22 12:50:44 -04003166 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003168 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 };
3170 struct nfs4_lookup_res res = {
3171 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003172 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 .fh = fhandle,
3174 };
3175 struct rpc_message msg = {
3176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3177 .rpc_argp = &args,
3178 .rpc_resp = &res,
3179 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003180
David Quigleyaa9c2662013-05-22 12:50:44 -04003181 bitmask[0] = nfs4_fattr_bitmap[0];
3182 bitmask[1] = nfs4_fattr_bitmap[1];
3183 /*
3184 * Process the label in the upcoming getfattr
3185 */
3186 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3187
Trond Myklebust0e574af2005-10-27 22:12:38 -04003188 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003189 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
3192static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3193 struct nfs_fsinfo *info)
3194{
3195 struct nfs4_exception exception = { };
3196 int err;
3197 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003198 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003199 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003200 switch (err) {
3201 case 0:
3202 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003203 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003204 default:
3205 err = nfs4_handle_exception(server, err, &exception);
3206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003208out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return err;
3210}
3211
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003212static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3213 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3214{
Trond Myklebustc2190662013-08-26 19:23:04 -04003215 struct rpc_auth_create_args auth_args = {
3216 .pseudoflavor = flavor,
3217 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003218 struct rpc_auth *auth;
3219 int ret;
3220
Trond Myklebustc2190662013-08-26 19:23:04 -04003221 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003222 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003223 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003224 goto out;
3225 }
3226 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003227out:
3228 return ret;
3229}
3230
Chuck Lever9a744ba2013-03-16 15:56:02 -04003231/*
3232 * Retry pseudoroot lookup with various security flavors. We do this when:
3233 *
3234 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3235 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3236 *
3237 * Returns zero on success, or a negative NFS4ERR value, or a
3238 * negative errno value.
3239 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003240static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003241 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003243 /* Per 3530bis 15.33.5 */
3244 static const rpc_authflavor_t flav_array[] = {
3245 RPC_AUTH_GSS_KRB5P,
3246 RPC_AUTH_GSS_KRB5I,
3247 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003248 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003249 RPC_AUTH_NULL,
3250 };
3251 int status = -EPERM;
3252 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003254 if (server->auth_info.flavor_len > 0) {
3255 /* try each flavor specified by user */
3256 for (i = 0; i < server->auth_info.flavor_len; i++) {
3257 status = nfs4_lookup_root_sec(server, fhandle, info,
3258 server->auth_info.flavors[i]);
3259 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3260 continue;
3261 break;
3262 }
3263 } else {
3264 /* no flavors specified by user, try default list */
3265 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3266 status = nfs4_lookup_root_sec(server, fhandle, info,
3267 flav_array[i]);
3268 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3269 continue;
3270 break;
3271 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003272 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003273
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003274 /*
3275 * -EACCESS could mean that the user doesn't have correct permissions
3276 * to access the mount. It could also mean that we tried to mount
3277 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3278 * existing mount programs don't handle -EACCES very well so it should
3279 * be mapped to -EPERM instead.
3280 */
3281 if (status == -EACCES)
3282 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003283 return status;
3284}
3285
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003286/**
3287 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3288 * @server: initialized nfs_server handle
3289 * @fhandle: we fill in the pseudo-fs root file handle
3290 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003291 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003292 *
3293 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003294 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003295int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003296 struct nfs_fsinfo *info,
3297 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003298{
Andre Przywarac7757072015-04-23 17:17:40 +01003299 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003300
Andre Przywarac7757072015-04-23 17:17:40 +01003301 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003302 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003303
3304 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003305 status = server->nfs_client->cl_mvops->find_root_sec(server,
3306 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (status == 0)
3309 status = nfs4_server_capabilities(server, fhandle);
3310 if (status == 0)
3311 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003312
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003313 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Bryan Schumakerbae36242012-05-10 15:07:31 -04003316static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3317 struct nfs_fsinfo *info)
3318{
3319 int error;
3320 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003321 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003322
3323 error = nfs4_server_capabilities(server, mntfh);
3324 if (error < 0) {
3325 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3326 return error;
3327 }
3328
David Quigley14c43f72013-05-22 12:50:43 -04003329 label = nfs4_label_alloc(server, GFP_KERNEL);
3330 if (IS_ERR(label))
3331 return PTR_ERR(label);
3332
David Quigley1775fd32013-05-22 12:50:42 -04003333 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003334 if (error < 0) {
3335 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003336 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003337 }
3338
3339 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3340 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3341 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3342
David Quigley14c43f72013-05-22 12:50:43 -04003343err_free_label:
3344 nfs4_label_free(label);
3345
Bryan Schumakerbae36242012-05-10 15:07:31 -04003346 return error;
3347}
3348
Manoj Naik6b97fd32006-06-09 09:34:29 -04003349/*
3350 * Get locations and (maybe) other attributes of a referral.
3351 * Note that we'll actually follow the referral later when
3352 * we detect fsid mismatch in inode revalidation
3353 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003354static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3355 const struct qstr *name, struct nfs_fattr *fattr,
3356 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003357{
3358 int status = -ENOMEM;
3359 struct page *page = NULL;
3360 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003361
3362 page = alloc_page(GFP_KERNEL);
3363 if (page == NULL)
3364 goto out;
3365 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3366 if (locations == NULL)
3367 goto out;
3368
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003369 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003370 if (status != 0)
3371 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003372
3373 /*
3374 * If the fsid didn't change, this is a migration event, not a
3375 * referral. Cause us to drop into the exception handler, which
3376 * will kick off migration recovery.
3377 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003378 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003379 dprintk("%s: server did not return a different fsid for"
3380 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003381 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003382 goto out;
3383 }
Andy Adamson533eb462011-06-13 18:25:56 -04003384 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3385 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003386
Andy Adamson533eb462011-06-13 18:25:56 -04003387 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003388 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003389 memset(fhandle, 0, sizeof(struct nfs_fh));
3390out:
3391 if (page)
3392 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003393 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003394 return status;
3395}
3396
David Quigley1775fd32013-05-22 12:50:42 -04003397static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3398 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 struct nfs4_getattr_arg args = {
3401 .fh = fhandle,
3402 .bitmask = server->attr_bitmask,
3403 };
3404 struct nfs4_getattr_res res = {
3405 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003406 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 .server = server,
3408 };
3409 struct rpc_message msg = {
3410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3411 .rpc_argp = &args,
3412 .rpc_resp = &res,
3413 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003414
3415 args.bitmask = nfs4_bitmask(server, label);
3416
Trond Myklebust0e574af2005-10-27 22:12:38 -04003417 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003418 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
David Quigley1775fd32013-05-22 12:50:42 -04003421static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3422 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
3424 struct nfs4_exception exception = { };
3425 int err;
3426 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003427 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3428 trace_nfs4_getattr(server, fhandle, fattr, err);
3429 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 &exception);
3431 } while (exception.retry);
3432 return err;
3433}
3434
3435/*
3436 * The file is not closed if it is opened due to the a request to change
3437 * the size of the file. The open call will not be needed once the
3438 * VFS layer lookup-intents are implemented.
3439 *
3440 * Close is called when the inode is destroyed.
3441 * If we haven't opened the file for O_WRONLY, we
3442 * need to in the size_change case to obtain a stateid.
3443 *
3444 * Got race?
3445 * Because OPEN is always done by name in nfsv4, it is
3446 * possible that we opened a different file by the same
3447 * name. We can recognize this race condition, but we
3448 * can't do anything about it besides returning an error.
3449 *
3450 * This will be fixed with VFS changes (lookup-intent).
3451 */
3452static int
3453nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3454 struct iattr *sattr)
3455{
David Howells2b0143b2015-03-17 22:25:59 +00003456 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003457 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003458 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003459 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 int status;
3461
Peng Tao88ac8152014-09-12 11:04:10 +08003462 if (pnfs_ld_layoutret_on_setattr(inode) &&
3463 sattr->ia_valid & ATTR_SIZE &&
3464 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003465 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003466
Trond Myklebust0e574af2005-10-27 22:12:38 -04003467 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Andy Adamson26699402012-05-30 16:12:24 -04003469 /* Deal with open(O_TRUNC) */
3470 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003471 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003472
3473 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003474 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003475 return 0;
3476
Trond Myklebustd5308382005-11-04 15:33:38 -05003477 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003478 if (sattr->ia_valid & ATTR_FILE) {
3479 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003480
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003481 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003482 if (ctx) {
3483 cred = ctx->cred;
3484 state = ctx->state;
3485 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003486 }
3487
David Quigley14c43f72013-05-22 12:50:43 -04003488 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3489 if (IS_ERR(label))
3490 return PTR_ERR(label);
3491
3492 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003493 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003494 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003495 nfs_setsecurity(inode, fattr, label);
3496 }
David Quigley14c43f72013-05-22 12:50:43 -04003497 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return status;
3499}
3500
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003501static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3502 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003503 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003504{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003505 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003506 int status;
3507 struct nfs4_lookup_arg args = {
3508 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003509 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003510 .name = name,
3511 };
3512 struct nfs4_lookup_res res = {
3513 .server = server,
3514 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003515 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003516 .fh = fhandle,
3517 };
3518 struct rpc_message msg = {
3519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3520 .rpc_argp = &args,
3521 .rpc_resp = &res,
3522 };
3523
David Quigleyaa9c2662013-05-22 12:50:44 -04003524 args.bitmask = nfs4_bitmask(server, label);
3525
David Howells2b3de442006-08-22 20:06:09 -04003526 nfs_fattr_init(fattr);
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003529 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 dprintk("NFS reply lookup: %d\n", status);
3531 return status;
3532}
3533
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003534static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003535{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003536 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003537 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003538 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3539 fattr->nlink = 2;
3540}
3541
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003542static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003543 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003544 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003545{
3546 struct nfs4_exception exception = { };
3547 struct rpc_clnt *client = *clnt;
3548 int err;
3549 do {
David Quigley1775fd32013-05-22 12:50:42 -04003550 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003551 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003552 switch (err) {
3553 case -NFS4ERR_BADNAME:
3554 err = -ENOENT;
3555 goto out;
3556 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003557 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003558 if (err == -NFS4ERR_MOVED)
3559 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003560 goto out;
3561 case -NFS4ERR_WRONGSEC:
3562 err = -EPERM;
3563 if (client != *clnt)
3564 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003565 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003566 if (IS_ERR(client))
3567 return PTR_ERR(client);
3568
3569 exception.retry = 1;
3570 break;
3571 default:
3572 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3573 }
3574 } while (exception.retry);
3575
3576out:
3577 if (err == 0)
3578 *clnt = client;
3579 else if (client != *clnt)
3580 rpc_shutdown_client(client);
3581
3582 return err;
3583}
3584
Al Virobeffb8f2016-07-20 16:34:42 -04003585static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003586 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3587 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003589 int status;
3590 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003591
David Quigley1775fd32013-05-22 12:50:42 -04003592 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003593 if (client != NFS_CLIENT(dir)) {
3594 rpc_shutdown_client(client);
3595 nfs_fixup_secinfo_attributes(fattr);
3596 }
3597 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003600struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003601nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003602 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3603{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003604 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003605 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003606
David Quigley1775fd32013-05-22 12:50:42 -04003607 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003608 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003609 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003610 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003611}
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3614{
Trond Myklebust76b32992007-08-10 17:45:11 -04003615 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 struct nfs4_accessargs args = {
3617 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003618 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003620 struct nfs4_accessres res = {
3621 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 struct rpc_message msg = {
3624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3625 .rpc_argp = &args,
3626 .rpc_resp = &res,
3627 .rpc_cred = entry->cred,
3628 };
3629 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003630 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 /*
3633 * Determine which access bits we want to ask for...
3634 */
3635 if (mode & MAY_READ)
3636 args.access |= NFS4_ACCESS_READ;
3637 if (S_ISDIR(inode->i_mode)) {
3638 if (mode & MAY_WRITE)
3639 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3640 if (mode & MAY_EXEC)
3641 args.access |= NFS4_ACCESS_LOOKUP;
3642 } else {
3643 if (mode & MAY_WRITE)
3644 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3645 if (mode & MAY_EXEC)
3646 args.access |= NFS4_ACCESS_EXECUTE;
3647 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003648
3649 res.fattr = nfs_alloc_fattr();
3650 if (res.fattr == NULL)
3651 return -ENOMEM;
3652
Bryan Schumaker7c513052011-03-24 17:12:24 +00003653 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003655 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003656 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003658 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return status;
3660}
3661
3662static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3663{
3664 struct nfs4_exception exception = { };
3665 int err;
3666 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003667 err = _nfs4_proc_access(inode, entry);
3668 trace_nfs4_access(inode, err);
3669 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 &exception);
3671 } while (exception.retry);
3672 return err;
3673}
3674
3675/*
3676 * TODO: For the time being, we don't try to get any attributes
3677 * along with any of the zero-copy operations READ, READDIR,
3678 * READLINK, WRITE.
3679 *
3680 * In the case of the first three, we want to put the GETATTR
3681 * after the read-type operation -- this is because it is hard
3682 * to predict the length of a GETATTR response in v4, and thus
3683 * align the READ data correctly. This means that the GETATTR
3684 * may end up partially falling into the page cache, and we should
3685 * shift it into the 'tail' of the xdr_buf before processing.
3686 * To do this efficiently, we need to know the total length
3687 * of data received, which doesn't seem to be available outside
3688 * of the RPC layer.
3689 *
3690 * In the case of WRITE, we also want to put the GETATTR after
3691 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003692 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 *
3694 * Both of these changes to the XDR layer would in fact be quite
3695 * minor, but I decided to leave them for a subsequent patch.
3696 */
3697static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3698 unsigned int pgbase, unsigned int pglen)
3699{
3700 struct nfs4_readlink args = {
3701 .fh = NFS_FH(inode),
3702 .pgbase = pgbase,
3703 .pglen = pglen,
3704 .pages = &page,
3705 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003706 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 struct rpc_message msg = {
3708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3709 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003710 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 };
3712
Bryan Schumaker7c513052011-03-24 17:12:24 +00003713 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 -07003714}
3715
3716static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3717 unsigned int pgbase, unsigned int pglen)
3718{
3719 struct nfs4_exception exception = { };
3720 int err;
3721 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003722 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3723 trace_nfs4_readlink(inode, err);
3724 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 &exception);
3726 } while (exception.retry);
3727 return err;
3728}
3729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003731 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733static int
3734nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003735 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
David Quigleyaa9c2662013-05-22 12:50:44 -04003737 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003738 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 int status = 0;
3741
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003742 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3743 if (IS_ERR(ctx))
3744 return PTR_ERR(ctx);
3745
David Quigleyaa9c2662013-05-22 12:50:44 -04003746 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3747
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003748 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003749 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (IS_ERR(state)) {
3751 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003752 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003755 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003756 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return status;
3758}
3759
Al Virobeffb8f2016-07-20 16:34:42 -04003760static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Trond Myklebust16e42952005-10-27 22:12:44 -04003762 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003763 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003765 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003767 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003768 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003769 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3772 .rpc_argp = &args,
3773 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003775 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003776
Bryan Schumaker7c513052011-03-24 17:12:24 +00003777 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003778 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003779 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return status;
3781}
3782
Al Virobeffb8f2016-07-20 16:34:42 -04003783static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 struct nfs4_exception exception = { };
3786 int err;
3787 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003788 err = _nfs4_proc_remove(dir, name);
3789 trace_nfs4_remove(dir, name, err);
3790 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 &exception);
3792 } while (exception.retry);
3793 return err;
3794}
3795
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003796static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003798 struct nfs_server *server = NFS_SERVER(dir);
3799 struct nfs_removeargs *args = msg->rpc_argp;
3800 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003802 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003804 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003805
3806 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003809static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3810{
Al Viro884be172016-04-28 23:56:31 -04003811 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003812 &data->args.seq_args,
3813 &data->res.seq_res,
3814 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003817static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003819 struct nfs_unlinkdata *data = task->tk_calldata;
3820 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003821
Trond Myklebust14516c32010-07-31 14:29:06 -04003822 if (!nfs4_sequence_done(task, &res->seq_res))
3823 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003824 if (nfs4_async_handle_error(task, res->server, NULL,
3825 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003826 return 0;
3827 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003828 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
Jeff Laytond3d41522010-09-17 17:31:57 -04003831static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3832{
3833 struct nfs_server *server = NFS_SERVER(dir);
3834 struct nfs_renameargs *arg = msg->rpc_argp;
3835 struct nfs_renameres *res = msg->rpc_resp;
3836
3837 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003838 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003839 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003840}
3841
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003842static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3843{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003844 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3845 &data->args.seq_args,
3846 &data->res.seq_res,
3847 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003848}
3849
3850static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3851 struct inode *new_dir)
3852{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003853 struct nfs_renamedata *data = task->tk_calldata;
3854 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003855
3856 if (!nfs4_sequence_done(task, &res->seq_res))
3857 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003858 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003859 return 0;
3860
3861 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003862 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003863 return 1;
3864}
3865
Al Virobeffb8f2016-07-20 16:34:42 -04003866static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003868 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 struct nfs4_link_arg arg = {
3870 .fh = NFS_FH(inode),
3871 .dir_fh = NFS_FH(dir),
3872 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003873 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003875 struct nfs4_link_res res = {
3876 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003877 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003878 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 struct rpc_message msg = {
3880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3881 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003882 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003884 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Trond Myklebust136f2622010-04-16 16:22:49 -04003886 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003887 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003888 goto out;
3889
David Quigley14c43f72013-05-22 12:50:43 -04003890 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3891 if (IS_ERR(res.label)) {
3892 status = PTR_ERR(res.label);
3893 goto out;
3894 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003895 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003896
Bryan Schumaker7c513052011-03-24 17:12:24 +00003897 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003898 if (!status) {
3899 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003900 status = nfs_post_op_update_inode(inode, res.fattr);
3901 if (!status)
3902 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003903 }
David Quigley14c43f72013-05-22 12:50:43 -04003904
3905
3906 nfs4_label_free(res.label);
3907
Trond Myklebust136f2622010-04-16 16:22:49 -04003908out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003909 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return status;
3911}
3912
Al Virobeffb8f2016-07-20 16:34:42 -04003913static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
3915 struct nfs4_exception exception = { };
3916 int err;
3917 do {
3918 err = nfs4_handle_exception(NFS_SERVER(inode),
3919 _nfs4_proc_link(inode, dir, name),
3920 &exception);
3921 } while (exception.retry);
3922 return err;
3923}
3924
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003925struct nfs4_createdata {
3926 struct rpc_message msg;
3927 struct nfs4_create_arg arg;
3928 struct nfs4_create_res res;
3929 struct nfs_fh fh;
3930 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003931 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003932};
3933
3934static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003935 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003936{
3937 struct nfs4_createdata *data;
3938
3939 data = kzalloc(sizeof(*data), GFP_KERNEL);
3940 if (data != NULL) {
3941 struct nfs_server *server = NFS_SERVER(dir);
3942
David Quigley14c43f72013-05-22 12:50:43 -04003943 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3944 if (IS_ERR(data->label))
3945 goto out_free;
3946
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003947 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3948 data->msg.rpc_argp = &data->arg;
3949 data->msg.rpc_resp = &data->res;
3950 data->arg.dir_fh = NFS_FH(dir);
3951 data->arg.server = server;
3952 data->arg.name = name;
3953 data->arg.attrs = sattr;
3954 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003955 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003956 data->res.server = server;
3957 data->res.fh = &data->fh;
3958 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003959 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003960 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003961 }
3962 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003963out_free:
3964 kfree(data);
3965 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003966}
3967
3968static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3969{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003970 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003971 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003972 if (status == 0) {
3973 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003974 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003975 }
3976 return status;
3977}
3978
3979static void nfs4_free_createdata(struct nfs4_createdata *data)
3980{
David Quigley14c43f72013-05-22 12:50:43 -04003981 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003982 kfree(data);
3983}
3984
Chuck Lever4f390c12006-08-22 20:06:22 -04003985static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003986 struct page *page, unsigned int len, struct iattr *sattr,
3987 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003989 struct nfs4_createdata *data;
3990 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Chuck Lever94a6d752006-08-22 20:06:23 -04003992 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003993 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003994
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003995 status = -ENOMEM;
3996 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3997 if (data == NULL)
3998 goto out;
3999
4000 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4001 data->arg.u.symlink.pages = &page;
4002 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004003 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004005 status = nfs4_do_create(dir, dentry, data);
4006
4007 nfs4_free_createdata(data);
4008out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 return status;
4010}
4011
Chuck Lever4f390c12006-08-22 20:06:22 -04004012static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004013 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004016 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004018
4019 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004022 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4023 trace_nfs4_symlink(dir, &dentry->d_name, err);
4024 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 &exception);
4026 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004027
4028 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return err;
4030}
4031
4032static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004033 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004035 struct nfs4_createdata *data;
4036 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004038 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4039 if (data == NULL)
4040 goto out;
4041
David Quigley1775fd32013-05-22 12:50:42 -04004042 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004043 status = nfs4_do_create(dir, dentry, data);
4044
4045 nfs4_free_createdata(data);
4046out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return status;
4048}
4049
4050static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4051 struct iattr *sattr)
4052{
4053 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004054 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004056
David Quigleyaa9c2662013-05-22 12:50:44 -04004057 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4058
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004059 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004061 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4062 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4063 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 &exception);
4065 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004066 nfs4_label_release_security(label);
4067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 return err;
4069}
4070
4071static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004072 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
David Howells2b0143b2015-03-17 22:25:59 +00004074 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 struct nfs4_readdir_arg args = {
4076 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004077 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 .pgbase = 0,
4079 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004080 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004081 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 };
4083 struct nfs4_readdir_res res;
4084 struct rpc_message msg = {
4085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4086 .rpc_argp = &args,
4087 .rpc_resp = &res,
4088 .rpc_cred = cred,
4089 };
4090 int status;
4091
Al Viro6de14722013-09-16 10:53:17 -04004092 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4093 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004094 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004095 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004097 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 -05004098 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004099 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004100 status += args.pgbase;
4101 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004102
4103 nfs_invalidate_atime(dir);
4104
Harvey Harrison3110ff82008-05-02 13:42:44 -07004105 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return status;
4107}
4108
4109static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004110 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
4112 struct nfs4_exception exception = { };
4113 int err;
4114 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004115 err = _nfs4_proc_readdir(dentry, cred, cookie,
4116 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004117 trace_nfs4_readdir(d_inode(dentry), err);
4118 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 &exception);
4120 } while (exception.retry);
4121 return err;
4122}
4123
4124static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004125 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004127 struct nfs4_createdata *data;
4128 int mode = sattr->ia_mode;
4129 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004131 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4132 if (data == NULL)
4133 goto out;
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004136 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004138 data->arg.ftype = NF4BLK;
4139 data->arg.u.device.specdata1 = MAJOR(rdev);
4140 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 }
4142 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004143 data->arg.ftype = NF4CHR;
4144 data->arg.u.device.specdata1 = MAJOR(rdev);
4145 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004146 } else if (!S_ISSOCK(mode)) {
4147 status = -EINVAL;
4148 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
David Quigley1775fd32013-05-22 12:50:42 -04004150
David Quigleyaa9c2662013-05-22 12:50:44 -04004151 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004152 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004153out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004154 nfs4_free_createdata(data);
4155out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return status;
4157}
4158
4159static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4160 struct iattr *sattr, dev_t rdev)
4161{
4162 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004163 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004165
David Quigleyaa9c2662013-05-22 12:50:44 -04004166 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4167
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004168 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004170 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4171 trace_nfs4_mknod(dir, &dentry->d_name, err);
4172 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 &exception);
4174 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004175
4176 nfs4_label_release_security(label);
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return err;
4179}
4180
4181static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4182 struct nfs_fsstat *fsstat)
4183{
4184 struct nfs4_statfs_arg args = {
4185 .fh = fhandle,
4186 .bitmask = server->attr_bitmask,
4187 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004188 struct nfs4_statfs_res res = {
4189 .fsstat = fsstat,
4190 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 struct rpc_message msg = {
4192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4193 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004194 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 };
4196
Trond Myklebust0e574af2005-10-27 22:12:38 -04004197 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004198 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
4201static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4202{
4203 struct nfs4_exception exception = { };
4204 int err;
4205 do {
4206 err = nfs4_handle_exception(server,
4207 _nfs4_proc_statfs(server, fhandle, fsstat),
4208 &exception);
4209 } while (exception.retry);
4210 return err;
4211}
4212
4213static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4214 struct nfs_fsinfo *fsinfo)
4215{
4216 struct nfs4_fsinfo_arg args = {
4217 .fh = fhandle,
4218 .bitmask = server->attr_bitmask,
4219 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004220 struct nfs4_fsinfo_res res = {
4221 .fsinfo = fsinfo,
4222 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 struct rpc_message msg = {
4224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4225 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004226 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 };
4228
Bryan Schumaker7c513052011-03-24 17:12:24 +00004229 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230}
4231
4232static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4233{
4234 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004235 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 int err;
4237
4238 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004239 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004240 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004241 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004242 nfs4_set_lease_period(server->nfs_client,
4243 fsinfo->lease_time * HZ,
4244 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004245 break;
4246 }
4247 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 } while (exception.retry);
4249 return err;
4250}
4251
4252static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4253{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004254 int error;
4255
Trond Myklebust0e574af2005-10-27 22:12:38 -04004256 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004257 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004258 if (error == 0) {
4259 /* block layout checks this! */
4260 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004261 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004262 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004263
4264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
4267static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4268 struct nfs_pathconf *pathconf)
4269{
4270 struct nfs4_pathconf_arg args = {
4271 .fh = fhandle,
4272 .bitmask = server->attr_bitmask,
4273 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004274 struct nfs4_pathconf_res res = {
4275 .pathconf = pathconf,
4276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 struct rpc_message msg = {
4278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4279 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004280 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 };
4282
4283 /* None of the pathconf attributes are mandatory to implement */
4284 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4285 memset(pathconf, 0, sizeof(*pathconf));
4286 return 0;
4287 }
4288
Trond Myklebust0e574af2005-10-27 22:12:38 -04004289 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004290 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
4293static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4294 struct nfs_pathconf *pathconf)
4295{
4296 struct nfs4_exception exception = { };
4297 int err;
4298
4299 do {
4300 err = nfs4_handle_exception(server,
4301 _nfs4_proc_pathconf(server, fhandle, pathconf),
4302 &exception);
4303 } while (exception.retry);
4304 return err;
4305}
4306
Trond Myklebust5521abf2013-03-16 20:54:34 -04004307int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004308 const struct nfs_open_context *ctx,
4309 const struct nfs_lock_context *l_ctx,
4310 fmode_t fmode)
4311{
4312 const struct nfs_lockowner *lockowner = NULL;
4313
4314 if (l_ctx != NULL)
4315 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004316 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004317}
4318EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4319
Trond Myklebust5521abf2013-03-16 20:54:34 -04004320static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4321 const struct nfs_open_context *ctx,
4322 const struct nfs_lock_context *l_ctx,
4323 fmode_t fmode)
4324{
4325 nfs4_stateid current_stateid;
4326
Trond Myklebuste1253be2014-03-05 08:44:23 -05004327 /* If the current stateid represents a lost lock, then exit */
4328 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4329 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004330 return nfs4_stateid_match(stateid, &current_stateid);
4331}
4332
4333static bool nfs4_error_stateid_expired(int err)
4334{
4335 switch (err) {
4336 case -NFS4ERR_DELEG_REVOKED:
4337 case -NFS4ERR_ADMIN_REVOKED:
4338 case -NFS4ERR_BAD_STATEID:
4339 case -NFS4ERR_STALE_STATEID:
4340 case -NFS4ERR_OLD_STATEID:
4341 case -NFS4ERR_OPENMODE:
4342 case -NFS4ERR_EXPIRED:
4343 return true;
4344 }
4345 return false;
4346}
4347
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004348void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004349{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004350 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004351}
4352
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004353static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004355 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004357 trace_nfs4_read(hdr, task->tk_status);
4358 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004359 hdr->args.context->state,
4360 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004361 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004362 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004364
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004365 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004367 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370
Trond Myklebust5521abf2013-03-16 20:54:34 -04004371static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004372 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004373{
4374
4375 if (!nfs4_error_stateid_expired(task->tk_status) ||
4376 nfs4_stateid_is_current(&args->stateid,
4377 args->context,
4378 args->lock_context,
4379 FMODE_READ))
4380 return false;
4381 rpc_restart_call_prepare(task);
4382 return true;
4383}
4384
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004385static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004386{
4387
4388 dprintk("--> %s\n", __func__);
4389
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004390 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004391 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004392 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004393 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004394 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4395 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004396}
4397
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004398static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4399 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004401 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004402 if (!hdr->pgio_done_cb)
4403 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004404 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004405 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406}
4407
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004408static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4409 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004410{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004411 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4412 &hdr->args.seq_args,
4413 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004414 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004415 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004416 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4417 hdr->args.lock_context,
4418 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004419 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004420 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004421 return -EIO;
4422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004425static int nfs4_write_done_cb(struct rpc_task *task,
4426 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004428 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004429
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004430 trace_nfs4_write(hdr, task->tk_status);
4431 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004432 hdr->args.context->state,
4433 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004434 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004435 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004437 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004438 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004439 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004440 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Trond Myklebust5521abf2013-03-16 20:54:34 -04004444static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004445 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004446{
4447
4448 if (!nfs4_error_stateid_expired(task->tk_status) ||
4449 nfs4_stateid_is_current(&args->stateid,
4450 args->context,
4451 args->lock_context,
4452 FMODE_WRITE))
4453 return false;
4454 rpc_restart_call_prepare(task);
4455 return true;
4456}
4457
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004458static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004459{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004460 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004461 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004462 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004463 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004464 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4465 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004466}
4467
Trond Myklebust5a37f852012-04-28 14:55:16 -04004468static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004469bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004470{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004471 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004472 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004473 return false;
4474 /* Otherwise, request attributes if and only if we don't hold
4475 * a delegation
4476 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004477 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004478}
Fred Isamana69aef12011-03-03 15:13:47 +00004479
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004480static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4481 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004483 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004484
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004485 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4486 hdr->args.bitmask = NULL;
4487 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004488 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004489 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004490
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004491 if (!hdr->pgio_done_cb)
4492 hdr->pgio_done_cb = nfs4_write_done_cb;
4493 hdr->res.server = server;
4494 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004496 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004497 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
Fred Isaman0b7c0152012-04-20 14:47:39 -04004500static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4501{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004502 nfs4_setup_sequence(NFS_SERVER(data->inode),
4503 &data->args.seq_args,
4504 &data->res.seq_res,
4505 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
Fred Isaman0b7c0152012-04-20 14:47:39 -04004508static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004511
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004512 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004513 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4514 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004515 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004516 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519}
4520
Fred Isaman0b7c0152012-04-20 14:47:39 -04004521static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004522{
4523 if (!nfs4_sequence_done(task, &data->res.seq_res))
4524 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004525 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004526}
4527
Fred Isaman0b7c0152012-04-20 14:47:39 -04004528static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004530 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004531
Fred Isaman0b7c0152012-04-20 14:47:39 -04004532 if (data->commit_done_cb == NULL)
4533 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004534 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004535 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004536 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537}
4538
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004539struct nfs4_renewdata {
4540 struct nfs_client *client;
4541 unsigned long timestamp;
4542};
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544/*
4545 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4546 * standalone procedure for queueing an asynchronous RENEW.
4547 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004548static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004549{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004550 struct nfs4_renewdata *data = calldata;
4551 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004552
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004553 if (atomic_read(&clp->cl_count) > 1)
4554 nfs4_schedule_state_renewal(clp);
4555 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004556 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004557}
4558
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004559static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004561 struct nfs4_renewdata *data = calldata;
4562 struct nfs_client *clp = data->client;
4563 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004565 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004566 switch (task->tk_status) {
4567 case 0:
4568 break;
4569 case -NFS4ERR_LEASE_MOVED:
4570 nfs4_schedule_lease_moved_recovery(clp);
4571 break;
4572 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004573 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004574 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4575 return;
4576 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004577 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004578 return;
4579 }
4580 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004582 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583}
4584
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004585static const struct rpc_call_ops nfs4_renew_ops = {
4586 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004587 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004588};
4589
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004590static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
4592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4594 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004595 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004597 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004599 if (renew_flags == 0)
4600 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004601 if (!atomic_inc_not_zero(&clp->cl_count))
4602 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004603 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004604 if (data == NULL)
4605 return -ENOMEM;
4606 data->client = clp;
4607 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004608 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004609 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
4611
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004612static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
4614 struct rpc_message msg = {
4615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4616 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004617 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 };
4619 unsigned long now = jiffies;
4620 int status;
4621
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004622 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (status < 0)
4624 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004625 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 return 0;
4627}
4628
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004629static inline int nfs4_server_supports_acls(struct nfs_server *server)
4630{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004631 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004632}
4633
Trond Myklebust21f498c2012-08-24 10:59:25 -04004634/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4635 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004636 * the stack.
4637 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004638#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004639
Neil Hormane9e3d722011-03-04 19:26:03 -05004640static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004641 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004642{
4643 struct page *newpage, **spages;
4644 int rc = 0;
4645 size_t len;
4646 spages = pages;
4647
4648 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004649 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004650 newpage = alloc_page(GFP_KERNEL);
4651
4652 if (newpage == NULL)
4653 goto unwind;
4654 memcpy(page_address(newpage), buf, len);
4655 buf += len;
4656 buflen -= len;
4657 *pages++ = newpage;
4658 rc++;
4659 } while (buflen != 0);
4660
4661 return rc;
4662
4663unwind:
4664 for(; rc > 0; rc--)
4665 __free_page(spages[rc-1]);
4666 return -ENOMEM;
4667}
4668
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004669struct nfs4_cached_acl {
4670 int cached;
4671 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004672 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004673};
4674
4675static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004676{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004677 struct nfs_inode *nfsi = NFS_I(inode);
4678
4679 spin_lock(&inode->i_lock);
4680 kfree(nfsi->nfs4_acl);
4681 nfsi->nfs4_acl = acl;
4682 spin_unlock(&inode->i_lock);
4683}
4684
4685static void nfs4_zap_acl_attr(struct inode *inode)
4686{
4687 nfs4_set_cached_acl(inode, NULL);
4688}
4689
4690static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4691{
4692 struct nfs_inode *nfsi = NFS_I(inode);
4693 struct nfs4_cached_acl *acl;
4694 int ret = -ENOENT;
4695
4696 spin_lock(&inode->i_lock);
4697 acl = nfsi->nfs4_acl;
4698 if (acl == NULL)
4699 goto out;
4700 if (buf == NULL) /* user is just asking for length */
4701 goto out_len;
4702 if (acl->cached == 0)
4703 goto out;
4704 ret = -ERANGE; /* see getxattr(2) man page */
4705 if (acl->len > buflen)
4706 goto out;
4707 memcpy(buf, acl->data, acl->len);
4708out_len:
4709 ret = acl->len;
4710out:
4711 spin_unlock(&inode->i_lock);
4712 return ret;
4713}
4714
Sachin Prabhu5794d212012-04-17 14:36:40 +01004715static 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 +00004716{
4717 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004718 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004719
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004720 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004721 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004722 if (acl == NULL)
4723 goto out;
4724 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004725 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004726 } else {
4727 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4728 if (acl == NULL)
4729 goto out;
4730 acl->cached = 0;
4731 }
4732 acl->len = acl_len;
4733out:
4734 nfs4_set_cached_acl(inode, acl);
4735}
4736
Andy Adamsonbf118a32011-12-07 11:55:27 -05004737/*
4738 * The getxattr API returns the required buffer length when called with a
4739 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4740 * the required buf. On a NULL buf, we send a page of data to the server
4741 * guessing that the ACL request can be serviced by a page. If so, we cache
4742 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4743 * the cache. If not so, we throw away the page, and cache the required
4744 * length. The next getxattr call will then produce another round trip to
4745 * the server, this time with the input buf of the required size.
4746 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004747static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004748{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004749 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004750 struct nfs_getaclargs args = {
4751 .fh = NFS_FH(inode),
4752 .acl_pages = pages,
4753 .acl_len = buflen,
4754 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004755 struct nfs_getaclres res = {
4756 .acl_len = buflen,
4757 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004758 struct rpc_message msg = {
4759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4760 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004761 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004762 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004763 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4764 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004765
Andy Adamsonbf118a32011-12-07 11:55:27 -05004766 /* As long as we're doing a round trip to the server anyway,
4767 * let's be prepared for a page of acl data. */
4768 if (npages == 0)
4769 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004770 if (npages > ARRAY_SIZE(pages))
4771 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004772
Andy Adamsonbf118a32011-12-07 11:55:27 -05004773 for (i = 0; i < npages; i++) {
4774 pages[i] = alloc_page(GFP_KERNEL);
4775 if (!pages[i])
4776 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004777 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004778
4779 /* for decoding across pages */
4780 res.acl_scratch = alloc_page(GFP_KERNEL);
4781 if (!res.acl_scratch)
4782 goto out_free;
4783
Andy Adamsonbf118a32011-12-07 11:55:27 -05004784 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004785
Peng Taode040be2012-01-10 22:42:47 +08004786 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004787 __func__, buf, buflen, npages, args.acl_len);
4788 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4789 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004790 if (ret)
4791 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004792
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004793 /* Handle the case where the passed-in buffer is too short */
4794 if (res.acl_flags & NFS4_ACL_TRUNC) {
4795 /* Did the user only issue a request for the acl length? */
4796 if (buf == NULL)
4797 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004798 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004799 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004800 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004801 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004802 if (buf) {
4803 if (res.acl_len > buflen) {
4804 ret = -ERANGE;
4805 goto out_free;
4806 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004807 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004808 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004809out_ok:
4810 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004811out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004812 for (i = 0; i < npages; i++)
4813 if (pages[i])
4814 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004815 if (res.acl_scratch)
4816 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004817 return ret;
4818}
4819
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004820static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4821{
4822 struct nfs4_exception exception = { };
4823 ssize_t ret;
4824 do {
4825 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004826 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004827 if (ret >= 0)
4828 break;
4829 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4830 } while (exception.retry);
4831 return ret;
4832}
4833
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004834static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4835{
4836 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004837 int ret;
4838
4839 if (!nfs4_server_supports_acls(server))
4840 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004841 ret = nfs_revalidate_inode(server, inode);
4842 if (ret < 0)
4843 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004844 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4845 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004846 ret = nfs4_read_cached_acl(inode, buf, buflen);
4847 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004848 /* -ENOENT is returned if there is no ACL or if there is an ACL
4849 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004850 return ret;
4851 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004852}
4853
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004854static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004855{
4856 struct nfs_server *server = NFS_SERVER(inode);
4857 struct page *pages[NFS4ACL_MAXPAGES];
4858 struct nfs_setaclargs arg = {
4859 .fh = NFS_FH(inode),
4860 .acl_pages = pages,
4861 .acl_len = buflen,
4862 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004863 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004864 struct rpc_message msg = {
4865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4866 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004867 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004868 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004869 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004870 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004871
4872 if (!nfs4_server_supports_acls(server))
4873 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004874 if (npages > ARRAY_SIZE(pages))
4875 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004876 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004877 if (i < 0)
4878 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004879 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004880 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004881
4882 /*
4883 * Free each page after tx, so the only ref left is
4884 * held by the network stack
4885 */
4886 for (; i > 0; i--)
4887 put_page(pages[i-1]);
4888
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004889 /*
4890 * Acl update can result in inode attribute update.
4891 * so mark the attribute cache invalid.
4892 */
4893 spin_lock(&inode->i_lock);
4894 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4895 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004896 nfs_access_zap_cache(inode);
4897 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004898 return ret;
4899}
4900
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004901static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4902{
4903 struct nfs4_exception exception = { };
4904 int err;
4905 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004906 err = __nfs4_proc_set_acl(inode, buf, buflen);
4907 trace_nfs4_set_acl(inode, err);
4908 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004909 &exception);
4910 } while (exception.retry);
4911 return err;
4912}
4913
David Quigleyaa9c2662013-05-22 12:50:44 -04004914#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4915static int _nfs4_get_security_label(struct inode *inode, void *buf,
4916 size_t buflen)
4917{
4918 struct nfs_server *server = NFS_SERVER(inode);
4919 struct nfs_fattr fattr;
4920 struct nfs4_label label = {0, 0, buflen, buf};
4921
4922 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004923 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004924 .fh = NFS_FH(inode),
4925 .bitmask = bitmask,
4926 };
4927 struct nfs4_getattr_res res = {
4928 .fattr = &fattr,
4929 .label = &label,
4930 .server = server,
4931 };
4932 struct rpc_message msg = {
4933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004934 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004935 .rpc_resp = &res,
4936 };
4937 int ret;
4938
4939 nfs_fattr_init(&fattr);
4940
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004941 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004942 if (ret)
4943 return ret;
4944 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4945 return -ENOENT;
4946 if (buflen < label.len)
4947 return -ERANGE;
4948 return 0;
4949}
4950
4951static int nfs4_get_security_label(struct inode *inode, void *buf,
4952 size_t buflen)
4953{
4954 struct nfs4_exception exception = { };
4955 int err;
4956
4957 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4958 return -EOPNOTSUPP;
4959
4960 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004961 err = _nfs4_get_security_label(inode, buf, buflen);
4962 trace_nfs4_get_security_label(inode, err);
4963 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004964 &exception);
4965 } while (exception.retry);
4966 return err;
4967}
4968
4969static int _nfs4_do_set_security_label(struct inode *inode,
4970 struct nfs4_label *ilabel,
4971 struct nfs_fattr *fattr,
4972 struct nfs4_label *olabel)
4973{
4974
4975 struct iattr sattr = {0};
4976 struct nfs_server *server = NFS_SERVER(inode);
4977 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004978 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004979 .fh = NFS_FH(inode),
4980 .iap = &sattr,
4981 .server = server,
4982 .bitmask = bitmask,
4983 .label = ilabel,
4984 };
4985 struct nfs_setattrres res = {
4986 .fattr = fattr,
4987 .label = olabel,
4988 .server = server,
4989 };
4990 struct rpc_message msg = {
4991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004992 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004993 .rpc_resp = &res,
4994 };
4995 int status;
4996
Jeff Layton12207f62013-11-01 10:49:32 -04004997 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004998
Jeff Layton12207f62013-11-01 10:49:32 -04004999 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005000 if (status)
5001 dprintk("%s failed: %d\n", __func__, status);
5002
5003 return status;
5004}
5005
5006static int nfs4_do_set_security_label(struct inode *inode,
5007 struct nfs4_label *ilabel,
5008 struct nfs_fattr *fattr,
5009 struct nfs4_label *olabel)
5010{
5011 struct nfs4_exception exception = { };
5012 int err;
5013
5014 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005015 err = _nfs4_do_set_security_label(inode, ilabel,
5016 fattr, olabel);
5017 trace_nfs4_set_security_label(inode, err);
5018 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005019 &exception);
5020 } while (exception.retry);
5021 return err;
5022}
5023
5024static int
Al Viro59301222016-05-27 10:19:30 -04005025nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005026{
5027 struct nfs4_label ilabel, *olabel = NULL;
5028 struct nfs_fattr fattr;
5029 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005030 int status;
5031
5032 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5033 return -EOPNOTSUPP;
5034
5035 nfs_fattr_init(&fattr);
5036
5037 ilabel.pi = 0;
5038 ilabel.lfs = 0;
5039 ilabel.label = (char *)buf;
5040 ilabel.len = buflen;
5041
5042 cred = rpc_lookup_cred();
5043 if (IS_ERR(cred))
5044 return PTR_ERR(cred);
5045
5046 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5047 if (IS_ERR(olabel)) {
5048 status = -PTR_ERR(olabel);
5049 goto out;
5050 }
5051
5052 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5053 if (status == 0)
5054 nfs_setsecurity(inode, &fattr, olabel);
5055
5056 nfs4_label_free(olabel);
5057out:
5058 put_rpccred(cred);
5059 return status;
5060}
5061#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5062
5063
Chuck Leverf0920752012-05-21 22:45:41 -04005064static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5065 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005066{
5067 __be32 verf[2];
5068
Chuck Lever2c820d92012-05-21 22:45:33 -04005069 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5070 /* An impossible timestamp guarantees this value
5071 * will never match a generated boot time. */
5072 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005073 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005074 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005075 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005076 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5077 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005078 }
Chuck Levercd937102012-03-02 17:14:31 -05005079 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5080}
5081
Jeff Laytona3192682015-06-09 19:43:59 -04005082static int
5083nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005084{
Jeff Laytona3192682015-06-09 19:43:59 -04005085 size_t len;
5086 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005087
Trond Myklebustceb3a162015-01-03 15:16:04 -05005088 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005089 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005090
Jeff Laytona3192682015-06-09 19:43:59 -04005091 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005092 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005093 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5094 1 +
5095 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5096 1;
5097 rcu_read_unlock();
5098
5099 if (len > NFS4_OPAQUE_LIMIT + 1)
5100 return -EINVAL;
5101
5102 /*
5103 * Since this string is allocated at mount time, and held until the
5104 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5105 * about a memory-reclaim deadlock.
5106 */
5107 str = kmalloc(len, GFP_KERNEL);
5108 if (!str)
5109 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005110
Chuck Levere984a552012-09-14 17:24:21 -04005111 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005112 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005113 clp->cl_ipaddr,
5114 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5115 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005116 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005117
Jeff Laytona3192682015-06-09 19:43:59 -04005118 clp->cl_owner_id = str;
5119 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005120}
5121
Jeff Layton873e3852015-06-09 19:44:00 -04005122static int
5123nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005124{
Jeff Layton873e3852015-06-09 19:44:00 -04005125 size_t len;
5126 char *str;
5127
5128 len = 10 + 10 + 1 + 10 + 1 +
5129 strlen(nfs4_client_id_uniquifier) + 1 +
5130 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5131
5132 if (len > NFS4_OPAQUE_LIMIT + 1)
5133 return -EINVAL;
5134
5135 /*
5136 * Since this string is allocated at mount time, and held until the
5137 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5138 * about a memory-reclaim deadlock.
5139 */
5140 str = kmalloc(len, GFP_KERNEL);
5141 if (!str)
5142 return -ENOMEM;
5143
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005144 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005145 clp->rpc_ops->version, clp->cl_minorversion,
5146 nfs4_client_id_uniquifier,
5147 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005148 clp->cl_owner_id = str;
5149 return 0;
5150}
5151
5152static int
5153nfs4_init_uniform_client_string(struct nfs_client *clp)
5154{
Jeff Layton873e3852015-06-09 19:44:00 -04005155 size_t len;
5156 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005157
5158 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005159 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005160
5161 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005162 return nfs4_init_uniquifier_client_string(clp);
5163
5164 len = 10 + 10 + 1 + 10 + 1 +
5165 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5166
5167 if (len > NFS4_OPAQUE_LIMIT + 1)
5168 return -EINVAL;
5169
5170 /*
5171 * Since this string is allocated at mount time, and held until the
5172 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5173 * about a memory-reclaim deadlock.
5174 */
5175 str = kmalloc(len, GFP_KERNEL);
5176 if (!str)
5177 return -ENOMEM;
5178
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005179 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005180 clp->rpc_ops->version, clp->cl_minorversion,
5181 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005182 clp->cl_owner_id = str;
5183 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005184}
5185
Chuck Lever706cb8d2014-03-12 12:51:47 -04005186/*
5187 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5188 * services. Advertise one based on the address family of the
5189 * clientaddr.
5190 */
5191static unsigned int
5192nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5193{
5194 if (strchr(clp->cl_ipaddr, ':') != NULL)
5195 return scnprintf(buf, len, "tcp6");
5196 else
5197 return scnprintf(buf, len, "tcp");
5198}
5199
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005200static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5201{
5202 struct nfs4_setclientid *sc = calldata;
5203
5204 if (task->tk_status == 0)
5205 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5206}
5207
5208static const struct rpc_call_ops nfs4_setclientid_ops = {
5209 .rpc_call_done = nfs4_setclientid_done,
5210};
5211
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005212/**
5213 * nfs4_proc_setclientid - Negotiate client ID
5214 * @clp: state data structure
5215 * @program: RPC program for NFSv4 callback service
5216 * @port: IP port number for NFS4 callback service
5217 * @cred: RPC credential to use for this call
5218 * @res: where to place the result
5219 *
5220 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5221 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005222int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5223 unsigned short port, struct rpc_cred *cred,
5224 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
5226 nfs4_verifier sc_verifier;
5227 struct nfs4_setclientid setclientid = {
5228 .sc_verifier = &sc_verifier,
5229 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005230 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 };
5232 struct rpc_message msg = {
5233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5234 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005235 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005236 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005238 struct rpc_task *task;
5239 struct rpc_task_setup task_setup_data = {
5240 .rpc_client = clp->cl_rpcclient,
5241 .rpc_message = &msg,
5242 .callback_ops = &nfs4_setclientid_ops,
5243 .callback_data = &setclientid,
5244 .flags = RPC_TASK_TIMEOUT,
5245 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005246 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Chuck Leverde734832012-07-11 16:30:50 -04005248 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005249 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005250
5251 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5252 status = nfs4_init_uniform_client_string(clp);
5253 else
Jeff Laytona3192682015-06-09 19:43:59 -04005254 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005255
5256 if (status)
5257 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005258
Chuck Leverde734832012-07-11 16:30:50 -04005259 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005260 setclientid.sc_netid_len =
5261 nfs4_init_callback_netid(clp,
5262 setclientid.sc_netid,
5263 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005264 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005265 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 clp->cl_ipaddr, port >> 8, port & 255);
5267
Jeff Layton3a6bb732015-06-09 19:43:57 -04005268 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005269 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005270 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005271 task = rpc_run_task(&task_setup_data);
5272 if (IS_ERR(task)) {
5273 status = PTR_ERR(task);
5274 goto out;
5275 }
5276 status = task->tk_status;
5277 if (setclientid.sc_cred) {
5278 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5279 put_rpccred(setclientid.sc_cred);
5280 }
5281 rpc_put_task(task);
5282out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005283 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005284 dprintk("NFS reply setclientid: %d\n", status);
5285 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286}
5287
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005288/**
5289 * nfs4_proc_setclientid_confirm - Confirm client ID
5290 * @clp: state data structure
5291 * @res: result of a previous SETCLIENTID
5292 * @cred: RPC credential to use for this call
5293 *
5294 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5295 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005296int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005297 struct nfs4_setclientid_res *arg,
5298 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 struct rpc_message msg = {
5301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005302 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005303 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 int status;
5306
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005307 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5308 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5309 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005310 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005311 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005312 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 return status;
5314}
5315
Trond Myklebustfe650402006-01-03 09:55:18 +01005316struct nfs4_delegreturndata {
5317 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005318 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005319 struct nfs_fh fh;
5320 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005321 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005322 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005323 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005324 struct inode *inode;
5325 bool roc;
5326 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005327};
5328
Trond Myklebustfe650402006-01-03 09:55:18 +01005329static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5330{
5331 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005332
Trond Myklebust14516c32010-07-31 14:29:06 -04005333 if (!nfs4_sequence_done(task, &data->res.seq_res))
5334 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005335
Trond Myklebustca8acf82013-08-13 10:36:56 -04005336 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005337 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005338 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005339 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005340 case -NFS4ERR_ADMIN_REVOKED:
5341 case -NFS4ERR_DELEG_REVOKED:
5342 case -NFS4ERR_BAD_STATEID:
5343 case -NFS4ERR_OLD_STATEID:
5344 case -NFS4ERR_STALE_STATEID:
5345 case -NFS4ERR_EXPIRED:
5346 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005347 if (data->roc)
5348 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005349 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005350 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005351 if (nfs4_async_handle_error(task, data->res.server,
5352 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005353 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005354 return;
5355 }
5356 }
5357 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005358}
5359
5360static void nfs4_delegreturn_release(void *calldata)
5361{
Peng Tao039b7562014-07-03 13:05:02 +08005362 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005363 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005364
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005365 if (inode) {
5366 if (data->roc)
5367 pnfs_roc_release(inode);
5368 nfs_iput_and_deactive(inode);
5369 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005370 kfree(calldata);
5371}
5372
Andy Adamson938e1012009-04-01 09:22:28 -04005373static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5374{
5375 struct nfs4_delegreturndata *d_data;
5376
5377 d_data = (struct nfs4_delegreturndata *)data;
5378
Peng Tao500d7012015-09-22 11:35:22 +08005379 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5380 return;
5381
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005382 if (d_data->roc)
5383 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005384
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005385 nfs4_setup_sequence(d_data->res.server,
5386 &d_data->args.seq_args,
5387 &d_data->res.seq_res,
5388 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005389}
Andy Adamson938e1012009-04-01 09:22:28 -04005390
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005391static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005392 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005393 .rpc_call_done = nfs4_delegreturn_done,
5394 .rpc_release = nfs4_delegreturn_release,
5395};
5396
Trond Myklebuste6f81072008-01-24 18:14:34 -05005397static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005398{
5399 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005400 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005401 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005402 struct rpc_message msg = {
5403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5404 .rpc_cred = cred,
5405 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005406 struct rpc_task_setup task_setup_data = {
5407 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005408 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005409 .callback_ops = &nfs4_delegreturn_ops,
5410 .flags = RPC_TASK_ASYNC,
5411 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005412 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005413
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005414 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005415 if (data == NULL)
5416 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005417 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005418
5419 nfs4_state_protect(server->nfs_client,
5420 NFS_SP4_MACH_CRED_CLEANUP,
5421 &task_setup_data.rpc_client, &msg);
5422
Trond Myklebustfe650402006-01-03 09:55:18 +01005423 data->args.fhandle = &data->fh;
5424 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005425 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005426 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005427 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005428 data->res.fattr = &data->fattr;
5429 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005430 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005431 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005432 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005433 data->inode = nfs_igrab_and_active(inode);
5434 if (data->inode)
5435 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005436
Trond Myklebustc970aa82007-07-14 15:39:59 -04005437 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005438 msg.rpc_argp = &data->args;
5439 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005440 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005441 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005442 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005443 if (!issync)
5444 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005445 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005446 if (status != 0)
5447 goto out;
5448 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005449 if (status == 0)
5450 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5451 else
5452 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005453out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005454 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005455 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456}
5457
Trond Myklebuste6f81072008-01-24 18:14:34 -05005458int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
5460 struct nfs_server *server = NFS_SERVER(inode);
5461 struct nfs4_exception exception = { };
5462 int err;
5463 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005464 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005465 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 switch (err) {
5467 case -NFS4ERR_STALE_STATEID:
5468 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 case 0:
5470 return 0;
5471 }
5472 err = nfs4_handle_exception(server, err, &exception);
5473 } while (exception.retry);
5474 return err;
5475}
5476
5477#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5478#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5479
5480/*
5481 * sleep, with exponential backoff, and retry the LOCK operation.
5482 */
5483static unsigned long
5484nfs4_set_lock_task_retry(unsigned long timeout)
5485{
Colin Cross416ad3c2013-05-06 23:50:06 +00005486 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 timeout <<= 1;
5488 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5489 return NFS4_LOCK_MAXTIMEOUT;
5490 return timeout;
5491}
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5494{
5495 struct inode *inode = state->inode;
5496 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005497 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005498 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005500 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005502 struct nfs_lockt_res res = {
5503 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 };
5505 struct rpc_message msg = {
5506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5507 .rpc_argp = &arg,
5508 .rpc_resp = &res,
5509 .rpc_cred = state->owner->so_cred,
5510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 struct nfs4_lock_state *lsp;
5512 int status;
5513
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005514 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005515 status = nfs4_set_lock_state(state, request);
5516 if (status != 0)
5517 goto out;
5518 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005519 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005520 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005521 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005522 switch (status) {
5523 case 0:
5524 request->fl_type = F_UNLCK;
5525 break;
5526 case -NFS4ERR_DENIED:
5527 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005529 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005530 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return status;
5533}
5534
5535static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5536{
5537 struct nfs4_exception exception = { };
5538 int err;
5539
5540 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005541 err = _nfs4_proc_getlk(state, cmd, request);
5542 trace_nfs4_get_lock(request, state, cmd, err);
5543 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 &exception);
5545 } while (exception.retry);
5546 return err;
5547}
5548
Jeff Layton83bfff22015-07-11 06:43:03 -04005549static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Benjamin Coddington4f656362015-10-22 13:38:14 -04005551 return locks_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005554struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005555 struct nfs_locku_args arg;
5556 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005557 struct nfs4_lock_state *lsp;
5558 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005559 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005560 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005561 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005562};
5563
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005564static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5565 struct nfs_open_context *ctx,
5566 struct nfs4_lock_state *lsp,
5567 struct nfs_seqid *seqid)
5568{
5569 struct nfs4_unlockdata *p;
5570 struct inode *inode = lsp->ls_state->inode;
5571
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005572 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005573 if (p == NULL)
5574 return NULL;
5575 p->arg.fh = NFS_FH(inode);
5576 p->arg.fl = &p->fl;
5577 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005578 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005579 p->lsp = lsp;
5580 atomic_inc(&lsp->ls_count);
5581 /* Ensure we don't close file until we're done freeing locks! */
5582 p->ctx = get_nfs_open_context(ctx);
5583 memcpy(&p->fl, fl, sizeof(p->fl));
5584 p->server = NFS_SERVER(inode);
5585 return p;
5586}
5587
Trond Myklebust06f814a2006-01-03 09:55:07 +01005588static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005589{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005590 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005591 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005592 nfs4_put_lock_state(calldata->lsp);
5593 put_nfs_open_context(calldata->ctx);
5594 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005595}
5596
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005597static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005598{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005599 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005600
Trond Myklebust14516c32010-07-31 14:29:06 -04005601 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5602 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005603 switch (task->tk_status) {
5604 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005605 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005606 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005607 if (nfs4_update_lock_stateid(calldata->lsp,
5608 &calldata->res.stateid))
5609 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005610 case -NFS4ERR_BAD_STATEID:
5611 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005612 case -NFS4ERR_STALE_STATEID:
5613 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005614 if (!nfs4_stateid_match(&calldata->arg.stateid,
5615 &calldata->lsp->ls_stateid))
5616 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005617 break;
5618 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005619 if (nfs4_async_handle_error(task, calldata->server,
5620 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005621 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005622 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005623 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005624}
5625
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005626static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005627{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005628 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005630 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005631 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005632 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005633 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005634 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005635 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005636 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005637 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005638 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005639 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005640 &calldata->res.seq_res,
5641 task) != 0)
5642 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005643 return;
5644out_no_action:
5645 task->tk_action = NULL;
5646out_wait:
5647 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648}
5649
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005650static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005651 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005652 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005653 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005654};
5655
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005656static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5657 struct nfs_open_context *ctx,
5658 struct nfs4_lock_state *lsp,
5659 struct nfs_seqid *seqid)
5660{
5661 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5664 .rpc_cred = ctx->cred,
5665 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005666 struct rpc_task_setup task_setup_data = {
5667 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005668 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005669 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005670 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005671 .flags = RPC_TASK_ASYNC,
5672 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005673
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005674 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5675 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5676
Frank Filz137d6ac2007-07-09 15:32:29 -07005677 /* Ensure this is an unlock - when canceling a lock, the
5678 * canceled lock is passed in, and it won't be an unlock.
5679 */
5680 fl->fl_type = F_UNLCK;
5681
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005682 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5683 if (data == NULL) {
5684 nfs_free_seqid(seqid);
5685 return ERR_PTR(-ENOMEM);
5686 }
5687
Chuck Levera9c92d62013-08-09 12:48:18 -04005688 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005689 msg.rpc_argp = &data->arg;
5690 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005691 task_setup_data.callback_data = data;
5692 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005693}
5694
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5696{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005697 struct inode *inode = state->inode;
5698 struct nfs4_state_owner *sp = state->owner;
5699 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005700 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005701 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005702 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005703 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005704 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005705 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Trond Myklebust9b073572006-06-29 16:38:34 -04005707 status = nfs4_set_lock_state(state, request);
5708 /* Unlock _before_ we do the RPC call */
5709 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005710 /* Exclude nfs_delegation_claim_locks() */
5711 mutex_lock(&sp->so_delegreturn_mutex);
5712 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005713 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005714 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005715 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005716 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005717 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005718 }
5719 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005720 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005721 if (status != 0)
5722 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005723 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005724 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005725 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5726 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005727 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5728 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005729 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005730 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005731 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005732 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005733 status = PTR_ERR(task);
5734 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005735 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005736 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005737 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005738out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005739 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005740 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005741 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742}
5743
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005744struct nfs4_lockdata {
5745 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005746 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005747 struct nfs4_lock_state *lsp;
5748 struct nfs_open_context *ctx;
5749 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005750 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005751 int rpc_status;
5752 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005753 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005754};
5755
5756static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005757 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5758 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759{
5760 struct nfs4_lockdata *p;
5761 struct inode *inode = lsp->ls_state->inode;
5762 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005763 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005764
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005765 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005766 if (p == NULL)
5767 return NULL;
5768
5769 p->arg.fh = NFS_FH(inode);
5770 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005771 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005772 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005773 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005774 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5775 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005776 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005777 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005778 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005779 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005780 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005781 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005782 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005783 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005784 atomic_inc(&lsp->ls_count);
5785 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005786 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005787 memcpy(&p->fl, fl, sizeof(p->fl));
5788 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005789out_free_seqid:
5790 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005791out_free:
5792 kfree(p);
5793 return NULL;
5794}
5795
5796static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5797{
5798 struct nfs4_lockdata *data = calldata;
5799 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Harvey Harrison3110ff82008-05-02 13:42:44 -07005801 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005802 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005803 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005804 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005805 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005806 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005807 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005808 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005809 nfs4_stateid_copy(&data->arg.open_stateid,
5810 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005811 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005812 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005813 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005814 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005815 nfs4_stateid_copy(&data->arg.lock_stateid,
5816 &data->lsp->ls_stateid);
5817 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005818 if (!nfs4_valid_open_stateid(state)) {
5819 data->rpc_status = -EBADF;
5820 task->tk_action = NULL;
5821 goto out_release_open_seqid;
5822 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005823 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005824 if (nfs4_setup_sequence(data->server,
5825 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005826 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005827 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005828 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005829out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005830 nfs_release_seqid(data->arg.open_seqid);
5831out_release_lock_seqid:
5832 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005833out_wait:
5834 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005835 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005836}
5837
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005838static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5839{
5840 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005841 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005842
Harvey Harrison3110ff82008-05-02 13:42:44 -07005843 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005844
Trond Myklebust14516c32010-07-31 14:29:06 -04005845 if (!nfs4_sequence_done(task, &data->res.seq_res))
5846 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005847
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005848 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005849 switch (task->tk_status) {
5850 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005851 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005852 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005853 if (data->arg.new_lock) {
5854 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005855 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005856 rpc_restart_call_prepare(task);
5857 break;
5858 }
5859 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005860 if (data->arg.new_lock_owner != 0) {
5861 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5862 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5863 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5864 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5865 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005866 break;
5867 case -NFS4ERR_BAD_STATEID:
5868 case -NFS4ERR_OLD_STATEID:
5869 case -NFS4ERR_STALE_STATEID:
5870 case -NFS4ERR_EXPIRED:
5871 if (data->arg.new_lock_owner != 0) {
5872 if (!nfs4_stateid_match(&data->arg.open_stateid,
5873 &lsp->ls_state->open_stateid))
5874 rpc_restart_call_prepare(task);
5875 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5876 &lsp->ls_stateid))
5877 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005878 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005879 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005880}
5881
5882static void nfs4_lock_release(void *calldata)
5883{
5884 struct nfs4_lockdata *data = calldata;
5885
Harvey Harrison3110ff82008-05-02 13:42:44 -07005886 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005887 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005888 if (data->cancelled != 0) {
5889 struct rpc_task *task;
5890 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5891 data->arg.lock_seqid);
5892 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005893 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005894 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895 } else
5896 nfs_free_seqid(data->arg.lock_seqid);
5897 nfs4_put_lock_state(data->lsp);
5898 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005899 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005900 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005901 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005902}
5903
5904static const struct rpc_call_ops nfs4_lock_ops = {
5905 .rpc_call_prepare = nfs4_lock_prepare,
5906 .rpc_call_done = nfs4_lock_done,
5907 .rpc_release = nfs4_lock_release,
5908};
5909
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005910static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5911{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005912 switch (error) {
5913 case -NFS4ERR_ADMIN_REVOKED:
5914 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005915 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005916 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005917 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005918 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005919 break;
5920 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005921 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005922 case -NFS4ERR_EXPIRED:
5923 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005924 };
5925}
5926
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005927static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005928{
5929 struct nfs4_lockdata *data;
5930 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005931 struct rpc_message msg = {
5932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5933 .rpc_cred = state->owner->so_cred,
5934 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005935 struct rpc_task_setup task_setup_data = {
5936 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005937 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005938 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005939 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 .flags = RPC_TASK_ASYNC,
5941 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005942 int ret;
5943
Harvey Harrison3110ff82008-05-02 13:42:44 -07005944 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005945 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005946 fl->fl_u.nfs4_fl.owner,
5947 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005948 if (data == NULL)
5949 return -ENOMEM;
5950 if (IS_SETLKW(cmd))
5951 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005952 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005953 msg.rpc_argp = &data->arg;
5954 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005955 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005956 if (recovery_type > NFS_LOCK_NEW) {
5957 if (recovery_type == NFS_LOCK_RECLAIM)
5958 data->arg.reclaim = NFS_LOCK_RECLAIM;
5959 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005960 } else
5961 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005962 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005963 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005964 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005965 ret = nfs4_wait_for_completion_rpc_task(task);
5966 if (ret == 0) {
5967 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005968 if (ret)
5969 nfs4_handle_setlk_error(data->server, data->lsp,
5970 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005971 } else
5972 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005973 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005974 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005975 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977}
5978
5979static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5980{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005981 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005982 struct nfs4_exception exception = {
5983 .inode = state->inode,
5984 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005985 int err;
5986
5987 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005988 /* Cache the lock if possible... */
5989 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5990 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005991 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005992 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005993 break;
5994 nfs4_handle_exception(server, err, &exception);
5995 } while (exception.retry);
5996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997}
5998
5999static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6000{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006001 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006002 struct nfs4_exception exception = {
6003 .inode = state->inode,
6004 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006005 int err;
6006
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006007 err = nfs4_set_lock_state(state, request);
6008 if (err != 0)
6009 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006010 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006011 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6012 return 0;
6013 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006014 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006015 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6016 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006017 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006018 switch (err) {
6019 default:
6020 goto out;
6021 case -NFS4ERR_GRACE:
6022 case -NFS4ERR_DELAY:
6023 nfs4_handle_exception(server, err, &exception);
6024 err = 0;
6025 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006026 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006027out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006031#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006032/**
6033 * nfs41_check_expired_locks - possibly free a lock stateid
6034 *
6035 * @state: NFSv4 state for an inode
6036 *
6037 * Returns NFS_OK if recovery for this stateid is now finished.
6038 * Otherwise a negative NFS4ERR value is returned.
6039 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006040static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006041{
Chuck Levereb64cf92012-07-11 16:30:05 -04006042 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006043 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006044 struct nfs_server *server = NFS_SERVER(state->inode);
6045
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006046 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006047 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006048 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6049
6050 status = nfs41_test_stateid(server,
6051 &lsp->ls_stateid,
6052 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006053 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006054 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006055 /* Free the stateid unless the server
6056 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006057 if (status != -NFS4ERR_BAD_STATEID)
6058 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006059 &lsp->ls_stateid,
6060 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006061 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006062 ret = status;
6063 }
6064 }
6065 };
6066
6067 return ret;
6068}
6069
6070static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6071{
6072 int status = NFS_OK;
6073
6074 if (test_bit(LK_STATE_IN_USE, &state->flags))
6075 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006076 if (status != NFS_OK)
6077 status = nfs4_lock_expired(state, request);
6078 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006079}
6080#endif
6081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6083{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006084 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006085 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006086 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006087 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006089 if ((fl_flags & FL_POSIX) &&
6090 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6091 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006092 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006093 status = nfs4_set_lock_state(state, request);
6094 if (status != 0)
6095 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006096 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006097 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006098 if (status < 0)
6099 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006100 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006101 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006102 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006104 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006105 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006106 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006107 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006108 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006109 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006110 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006111 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006112 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006113 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006114out:
6115 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 return status;
6117}
6118
6119static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6120{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006121 struct nfs4_exception exception = {
6122 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006123 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 int err;
6126
6127 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006128 err = _nfs4_proc_setlk(state, cmd, request);
6129 if (err == -NFS4ERR_DENIED)
6130 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006132 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 } while (exception.retry);
6134 return err;
6135}
6136
6137static int
6138nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6139{
6140 struct nfs_open_context *ctx;
6141 struct nfs4_state *state;
6142 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6143 int status;
6144
6145 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006146 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 state = ctx->state;
6148
6149 if (request->fl_start < 0 || request->fl_end < 0)
6150 return -EINVAL;
6151
Trond Myklebustd9531262009-07-21 19:22:38 -04006152 if (IS_GETLK(cmd)) {
6153 if (state != NULL)
6154 return nfs4_proc_getlk(state, F_GETLK, request);
6155 return 0;
6156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
6158 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6159 return -EINVAL;
6160
Trond Myklebustd9531262009-07-21 19:22:38 -04006161 if (request->fl_type == F_UNLCK) {
6162 if (state != NULL)
6163 return nfs4_proc_unlck(state, cmd, request);
6164 return 0;
6165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Trond Myklebustd9531262009-07-21 19:22:38 -04006167 if (state == NULL)
6168 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006169 /*
6170 * Don't rely on the VFS having checked the file open mode,
6171 * since it won't do this for flock() locks.
6172 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006173 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006174 case F_RDLCK:
6175 if (!(filp->f_mode & FMODE_READ))
6176 return -EBADF;
6177 break;
6178 case F_WRLCK:
6179 if (!(filp->f_mode & FMODE_WRITE))
6180 return -EBADF;
6181 }
6182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 do {
6184 status = nfs4_proc_setlk(state, cmd, request);
6185 if ((status != -EAGAIN) || IS_SETLK(cmd))
6186 break;
6187 timeout = nfs4_set_lock_task_retry(timeout);
6188 status = -ERESTARTSYS;
6189 if (signalled())
6190 break;
6191 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 return status;
6193}
6194
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006195int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006196{
6197 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006198 int err;
6199
6200 err = nfs4_set_lock_state(state, fl);
6201 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006202 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006203 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006204 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006205}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006206
Trond Myklebustcf470c32012-03-07 13:49:12 -05006207struct nfs_release_lockowner_data {
6208 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006209 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006210 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006211 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006212 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006213};
6214
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006215static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6216{
6217 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006218 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006219 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6220 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006221 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006222 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006223}
6224
6225static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6226{
6227 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006228 struct nfs_server *server = data->server;
6229
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006230 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006231
6232 switch (task->tk_status) {
6233 case 0:
6234 renew_lease(server, data->timestamp);
6235 break;
6236 case -NFS4ERR_STALE_CLIENTID:
6237 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006238 nfs4_schedule_lease_recovery(server->nfs_client);
6239 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006240 case -NFS4ERR_LEASE_MOVED:
6241 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006242 if (nfs4_async_handle_error(task, server,
6243 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006244 rpc_restart_call_prepare(task);
6245 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006246}
6247
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006248static void nfs4_release_lockowner_release(void *calldata)
6249{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006250 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006251 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006252 kfree(calldata);
6253}
6254
Trond Myklebust17280172012-03-11 13:11:00 -04006255static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006256 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6257 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006258 .rpc_release = nfs4_release_lockowner_release,
6259};
6260
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006261static void
6262nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006263{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006264 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006265 struct rpc_message msg = {
6266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6267 };
6268
6269 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006270 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006271
Trond Myklebustcf470c32012-03-07 13:49:12 -05006272 data = kmalloc(sizeof(*data), GFP_NOFS);
6273 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006274 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006275 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006276 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006277 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6278 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6279 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006280
Trond Myklebustcf470c32012-03-07 13:49:12 -05006281 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006282 msg.rpc_resp = &data->res;
6283 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006284 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006285}
6286
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006287#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6288
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006289static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006290 struct dentry *unused, struct inode *inode,
6291 const char *key, const void *buf,
6292 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006293{
Al Viro59301222016-05-27 10:19:30 -04006294 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006295}
6296
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006297static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006298 struct dentry *unused, struct inode *inode,
6299 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006300{
Al Virob2968212016-04-10 20:48:24 -04006301 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006302}
6303
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006304static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006305{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006306 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006307}
6308
David Quigleyc9bccef2013-05-22 12:50:45 -04006309#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006310
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006311static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006312 struct dentry *unused, struct inode *inode,
6313 const char *key, const void *buf,
6314 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006315{
6316 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006317 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006318
6319 return -EOPNOTSUPP;
6320}
6321
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006322static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006323 struct dentry *unused, struct inode *inode,
6324 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006325{
6326 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006327 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006328 return -EOPNOTSUPP;
6329}
6330
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006331static ssize_t
6332nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006333{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006334 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006335
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006336 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6337 len = security_inode_listsecurity(inode, list, list_len);
6338 if (list_len && len > list_len)
6339 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006340 }
6341 return len;
6342}
6343
6344static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6345 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006346 .get = nfs4_xattr_get_nfs4_label,
6347 .set = nfs4_xattr_set_nfs4_label,
6348};
David Quigleyc9bccef2013-05-22 12:50:45 -04006349
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006350#else
6351
6352static ssize_t
6353nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6354{
6355 return 0;
6356}
6357
6358#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006359
Andy Adamson533eb462011-06-13 18:25:56 -04006360/*
6361 * nfs_fhget will use either the mounted_on_fileid or the fileid
6362 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006363static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6364{
Andy Adamson533eb462011-06-13 18:25:56 -04006365 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6366 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6367 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006368 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006369 return;
6370
6371 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006372 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006373 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6374 fattr->nlink = 2;
6375}
6376
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006377static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6378 const struct qstr *name,
6379 struct nfs4_fs_locations *fs_locations,
6380 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006381{
6382 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006383 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006384 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006385 };
6386 struct nfs4_fs_locations_arg args = {
6387 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006388 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006389 .page = page,
6390 .bitmask = bitmask,
6391 };
Benny Halevy22958462009-04-01 09:22:02 -04006392 struct nfs4_fs_locations_res res = {
6393 .fs_locations = fs_locations,
6394 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006395 struct rpc_message msg = {
6396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6397 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006398 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006399 };
6400 int status;
6401
Harvey Harrison3110ff82008-05-02 13:42:44 -07006402 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006403
6404 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6405 * is not supported */
6406 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6407 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6408 else
6409 bitmask[0] |= FATTR4_WORD0_FILEID;
6410
Trond Myklebustc228fd32007-01-13 02:28:11 -05006411 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006412 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006413 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006414 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006415 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006416 return status;
6417}
6418
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006419int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6420 const struct qstr *name,
6421 struct nfs4_fs_locations *fs_locations,
6422 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006423{
6424 struct nfs4_exception exception = { };
6425 int err;
6426 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006427 err = _nfs4_proc_fs_locations(client, dir, name,
6428 fs_locations, page);
6429 trace_nfs4_get_fs_locations(dir, name, err);
6430 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006431 &exception);
6432 } while (exception.retry);
6433 return err;
6434}
6435
Chuck Leverb03d7352013-10-17 14:12:50 -04006436/*
6437 * This operation also signals the server that this client is
6438 * performing migration recovery. The server can stop returning
6439 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6440 * appended to this compound to identify the client ID which is
6441 * performing recovery.
6442 */
6443static int _nfs40_proc_get_locations(struct inode *inode,
6444 struct nfs4_fs_locations *locations,
6445 struct page *page, struct rpc_cred *cred)
6446{
6447 struct nfs_server *server = NFS_SERVER(inode);
6448 struct rpc_clnt *clnt = server->client;
6449 u32 bitmask[2] = {
6450 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6451 };
6452 struct nfs4_fs_locations_arg args = {
6453 .clientid = server->nfs_client->cl_clientid,
6454 .fh = NFS_FH(inode),
6455 .page = page,
6456 .bitmask = bitmask,
6457 .migration = 1, /* skip LOOKUP */
6458 .renew = 1, /* append RENEW */
6459 };
6460 struct nfs4_fs_locations_res res = {
6461 .fs_locations = locations,
6462 .migration = 1,
6463 .renew = 1,
6464 };
6465 struct rpc_message msg = {
6466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6467 .rpc_argp = &args,
6468 .rpc_resp = &res,
6469 .rpc_cred = cred,
6470 };
6471 unsigned long now = jiffies;
6472 int status;
6473
6474 nfs_fattr_init(&locations->fattr);
6475 locations->server = server;
6476 locations->nlocations = 0;
6477
6478 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6479 nfs4_set_sequence_privileged(&args.seq_args);
6480 status = nfs4_call_sync_sequence(clnt, server, &msg,
6481 &args.seq_args, &res.seq_res);
6482 if (status)
6483 return status;
6484
6485 renew_lease(server, now);
6486 return 0;
6487}
6488
6489#ifdef CONFIG_NFS_V4_1
6490
6491/*
6492 * This operation also signals the server that this client is
6493 * performing migration recovery. The server can stop asserting
6494 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6495 * performing this operation is identified in the SEQUENCE
6496 * operation in this compound.
6497 *
6498 * When the client supports GETATTR(fs_locations_info), it can
6499 * be plumbed in here.
6500 */
6501static int _nfs41_proc_get_locations(struct inode *inode,
6502 struct nfs4_fs_locations *locations,
6503 struct page *page, struct rpc_cred *cred)
6504{
6505 struct nfs_server *server = NFS_SERVER(inode);
6506 struct rpc_clnt *clnt = server->client;
6507 u32 bitmask[2] = {
6508 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6509 };
6510 struct nfs4_fs_locations_arg args = {
6511 .fh = NFS_FH(inode),
6512 .page = page,
6513 .bitmask = bitmask,
6514 .migration = 1, /* skip LOOKUP */
6515 };
6516 struct nfs4_fs_locations_res res = {
6517 .fs_locations = locations,
6518 .migration = 1,
6519 };
6520 struct rpc_message msg = {
6521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6522 .rpc_argp = &args,
6523 .rpc_resp = &res,
6524 .rpc_cred = cred,
6525 };
6526 int status;
6527
6528 nfs_fattr_init(&locations->fattr);
6529 locations->server = server;
6530 locations->nlocations = 0;
6531
6532 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6533 nfs4_set_sequence_privileged(&args.seq_args);
6534 status = nfs4_call_sync_sequence(clnt, server, &msg,
6535 &args.seq_args, &res.seq_res);
6536 if (status == NFS4_OK &&
6537 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6538 status = -NFS4ERR_LEASE_MOVED;
6539 return status;
6540}
6541
6542#endif /* CONFIG_NFS_V4_1 */
6543
6544/**
6545 * nfs4_proc_get_locations - discover locations for a migrated FSID
6546 * @inode: inode on FSID that is migrating
6547 * @locations: result of query
6548 * @page: buffer
6549 * @cred: credential to use for this operation
6550 *
6551 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6552 * operation failed, or a negative errno if a local error occurred.
6553 *
6554 * On success, "locations" is filled in, but if the server has
6555 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6556 * asserted.
6557 *
6558 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6559 * from this client that require migration recovery.
6560 */
6561int nfs4_proc_get_locations(struct inode *inode,
6562 struct nfs4_fs_locations *locations,
6563 struct page *page, struct rpc_cred *cred)
6564{
6565 struct nfs_server *server = NFS_SERVER(inode);
6566 struct nfs_client *clp = server->nfs_client;
6567 const struct nfs4_mig_recovery_ops *ops =
6568 clp->cl_mvops->mig_recovery_ops;
6569 struct nfs4_exception exception = { };
6570 int status;
6571
6572 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6573 (unsigned long long)server->fsid.major,
6574 (unsigned long long)server->fsid.minor,
6575 clp->cl_hostname);
6576 nfs_display_fhandle(NFS_FH(inode), __func__);
6577
6578 do {
6579 status = ops->get_locations(inode, locations, page, cred);
6580 if (status != -NFS4ERR_DELAY)
6581 break;
6582 nfs4_handle_exception(server, status, &exception);
6583 } while (exception.retry);
6584 return status;
6585}
6586
Chuck Lever44c99932013-10-17 14:13:30 -04006587/*
6588 * This operation also signals the server that this client is
6589 * performing "lease moved" recovery. The server can stop
6590 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6591 * is appended to this compound to identify the client ID which is
6592 * performing recovery.
6593 */
6594static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6595{
6596 struct nfs_server *server = NFS_SERVER(inode);
6597 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6598 struct rpc_clnt *clnt = server->client;
6599 struct nfs4_fsid_present_arg args = {
6600 .fh = NFS_FH(inode),
6601 .clientid = clp->cl_clientid,
6602 .renew = 1, /* append RENEW */
6603 };
6604 struct nfs4_fsid_present_res res = {
6605 .renew = 1,
6606 };
6607 struct rpc_message msg = {
6608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6609 .rpc_argp = &args,
6610 .rpc_resp = &res,
6611 .rpc_cred = cred,
6612 };
6613 unsigned long now = jiffies;
6614 int status;
6615
6616 res.fh = nfs_alloc_fhandle();
6617 if (res.fh == NULL)
6618 return -ENOMEM;
6619
6620 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6621 nfs4_set_sequence_privileged(&args.seq_args);
6622 status = nfs4_call_sync_sequence(clnt, server, &msg,
6623 &args.seq_args, &res.seq_res);
6624 nfs_free_fhandle(res.fh);
6625 if (status)
6626 return status;
6627
6628 do_renew_lease(clp, now);
6629 return 0;
6630}
6631
6632#ifdef CONFIG_NFS_V4_1
6633
6634/*
6635 * This operation also signals the server that this client is
6636 * performing "lease moved" recovery. The server can stop asserting
6637 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6638 * this operation is identified in the SEQUENCE operation in this
6639 * compound.
6640 */
6641static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6642{
6643 struct nfs_server *server = NFS_SERVER(inode);
6644 struct rpc_clnt *clnt = server->client;
6645 struct nfs4_fsid_present_arg args = {
6646 .fh = NFS_FH(inode),
6647 };
6648 struct nfs4_fsid_present_res res = {
6649 };
6650 struct rpc_message msg = {
6651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6652 .rpc_argp = &args,
6653 .rpc_resp = &res,
6654 .rpc_cred = cred,
6655 };
6656 int status;
6657
6658 res.fh = nfs_alloc_fhandle();
6659 if (res.fh == NULL)
6660 return -ENOMEM;
6661
6662 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6663 nfs4_set_sequence_privileged(&args.seq_args);
6664 status = nfs4_call_sync_sequence(clnt, server, &msg,
6665 &args.seq_args, &res.seq_res);
6666 nfs_free_fhandle(res.fh);
6667 if (status == NFS4_OK &&
6668 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6669 status = -NFS4ERR_LEASE_MOVED;
6670 return status;
6671}
6672
6673#endif /* CONFIG_NFS_V4_1 */
6674
6675/**
6676 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6677 * @inode: inode on FSID to check
6678 * @cred: credential to use for this operation
6679 *
6680 * Server indicates whether the FSID is present, moved, or not
6681 * recognized. This operation is necessary to clear a LEASE_MOVED
6682 * condition for this client ID.
6683 *
6684 * Returns NFS4_OK if the FSID is present on this server,
6685 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6686 * NFS4ERR code if some error occurred on the server, or a
6687 * negative errno if a local failure occurred.
6688 */
6689int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6690{
6691 struct nfs_server *server = NFS_SERVER(inode);
6692 struct nfs_client *clp = server->nfs_client;
6693 const struct nfs4_mig_recovery_ops *ops =
6694 clp->cl_mvops->mig_recovery_ops;
6695 struct nfs4_exception exception = { };
6696 int status;
6697
6698 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6699 (unsigned long long)server->fsid.major,
6700 (unsigned long long)server->fsid.minor,
6701 clp->cl_hostname);
6702 nfs_display_fhandle(NFS_FH(inode), __func__);
6703
6704 do {
6705 status = ops->fsid_present(inode, cred);
6706 if (status != -NFS4ERR_DELAY)
6707 break;
6708 nfs4_handle_exception(server, status, &exception);
6709 } while (exception.retry);
6710 return status;
6711}
6712
Andy Adamson5ec16a82013-08-08 10:57:55 -04006713/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006714 * If 'use_integrity' is true and the state managment nfs_client
6715 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6716 * and the machine credential as per RFC3530bis and RFC5661 Security
6717 * Considerations sections. Otherwise, just use the user cred with the
6718 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006719 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006720static 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 +00006721{
6722 int status;
6723 struct nfs4_secinfo_arg args = {
6724 .dir_fh = NFS_FH(dir),
6725 .name = name,
6726 };
6727 struct nfs4_secinfo_res res = {
6728 .flavors = flavors,
6729 };
6730 struct rpc_message msg = {
6731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6732 .rpc_argp = &args,
6733 .rpc_resp = &res,
6734 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006735 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006736 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006737
6738 if (use_integrity) {
6739 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006740 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6741 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006742 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006743
6744 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006745
6746 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6747 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6748
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006749 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6750 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006751 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006752
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006753 if (cred)
6754 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006755
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006756 return status;
6757}
6758
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006759int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6760 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006761{
6762 struct nfs4_exception exception = { };
6763 int err;
6764 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006765 err = -NFS4ERR_WRONGSEC;
6766
6767 /* try to use integrity protection with machine cred */
6768 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6769 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6770
6771 /*
6772 * if unable to use integrity protection, or SECINFO with
6773 * integrity protection returns NFS4ERR_WRONGSEC (which is
6774 * disallowed by spec, but exists in deployed servers) use
6775 * the current filesystem's rpc_client and the user cred.
6776 */
6777 if (err == -NFS4ERR_WRONGSEC)
6778 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6779
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006780 trace_nfs4_secinfo(dir, name, err);
6781 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006782 &exception);
6783 } while (exception.retry);
6784 return err;
6785}
6786
Andy Adamson557134a2009-04-01 09:21:53 -04006787#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006788/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006789 * Check the exchange flags returned by the server for invalid flags, having
6790 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6791 * DS flags set.
6792 */
6793static int nfs4_check_cl_exchange_flags(u32 flags)
6794{
6795 if (flags & ~EXCHGID4_FLAG_MASK_R)
6796 goto out_inval;
6797 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6798 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6799 goto out_inval;
6800 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6801 goto out_inval;
6802 return NFS_OK;
6803out_inval:
6804 return -NFS4ERR_INVAL;
6805}
6806
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006807static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006808nfs41_same_server_scope(struct nfs41_server_scope *a,
6809 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006810{
6811 if (a->server_scope_sz == b->server_scope_sz &&
6812 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6813 return true;
6814
6815 return false;
6816}
6817
Andy Adamson02a95de2016-02-05 16:08:37 -05006818static void
6819nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6820{
6821}
6822
6823static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6824 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6825};
6826
Andy Adamson357f54d2010-12-14 10:11:57 -05006827/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006828 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006829 *
6830 * The 4.1 client currently uses the same TCP connection for the
6831 * fore and backchannel.
6832 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006833static
6834int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6835 struct rpc_xprt *xprt,
6836 struct nfs_client *clp,
6837 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006838{
6839 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006840 struct nfs41_bind_conn_to_session_args args = {
6841 .client = clp,
6842 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6843 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006844 struct nfs41_bind_conn_to_session_res res;
6845 struct rpc_message msg = {
6846 .rpc_proc =
6847 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006848 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006849 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006850 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006851 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006852 struct rpc_task_setup task_setup_data = {
6853 .rpc_client = clnt,
6854 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006855 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006856 .rpc_message = &msg,
6857 .flags = RPC_TASK_TIMEOUT,
6858 };
6859 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006860
6861 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006862
Trond Myklebust71a097c2015-02-18 09:27:18 -08006863 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6864 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6865 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006866
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006867 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6868 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6869 args.dir = NFS4_CDFC4_FORE;
6870
6871 task = rpc_run_task(&task_setup_data);
6872 if (!IS_ERR(task)) {
6873 status = task->tk_status;
6874 rpc_put_task(task);
6875 } else
6876 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006877 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006878 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006879 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006880 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6881 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6882 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006883 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006884 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006885 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006886 dprintk("NFS: %s: Unexpected direction from server\n",
6887 __func__);
6888 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006889 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006890 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006891 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006892 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6893 __func__);
6894 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006895 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006896 }
6897 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006898out:
6899 dprintk("<-- %s status= %d\n", __func__, status);
6900 return status;
6901}
6902
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006903struct rpc_bind_conn_calldata {
6904 struct nfs_client *clp;
6905 struct rpc_cred *cred;
6906};
6907
6908static int
6909nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6910 struct rpc_xprt *xprt,
6911 void *calldata)
6912{
6913 struct rpc_bind_conn_calldata *p = calldata;
6914
6915 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6916}
6917
6918int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6919{
6920 struct rpc_bind_conn_calldata data = {
6921 .clp = clp,
6922 .cred = cred,
6923 };
6924 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6925 nfs4_proc_bind_conn_to_session_callback, &data);
6926}
6927
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006928/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006929 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6930 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006931 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006932static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6933 .how = SP4_MACH_CRED,
6934 .enforce.u.words = {
6935 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6936 1 << (OP_EXCHANGE_ID - 32) |
6937 1 << (OP_CREATE_SESSION - 32) |
6938 1 << (OP_DESTROY_SESSION - 32) |
6939 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006940 },
6941 .allow.u.words = {
6942 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006943 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006944 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006945 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006946 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006947 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006948 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006949 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006950 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006951 1 << (OP_FREE_STATEID - 32) |
6952 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006953 }
6954};
6955
6956/*
6957 * Select the state protection mode for client `clp' given the server results
6958 * from exchange_id in `sp'.
6959 *
6960 * Returns 0 on success, negative errno otherwise.
6961 */
6962static int nfs4_sp4_select_mode(struct nfs_client *clp,
6963 struct nfs41_state_protection *sp)
6964{
6965 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6966 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6967 1 << (OP_EXCHANGE_ID - 32) |
6968 1 << (OP_CREATE_SESSION - 32) |
6969 1 << (OP_DESTROY_SESSION - 32) |
6970 1 << (OP_DESTROY_CLIENTID - 32)
6971 };
6972 unsigned int i;
6973
6974 if (sp->how == SP4_MACH_CRED) {
6975 /* Print state protect result */
6976 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6977 for (i = 0; i <= LAST_NFS4_OP; i++) {
6978 if (test_bit(i, sp->enforce.u.longs))
6979 dfprintk(MOUNT, " enforce op %d\n", i);
6980 if (test_bit(i, sp->allow.u.longs))
6981 dfprintk(MOUNT, " allow op %d\n", i);
6982 }
6983
6984 /* make sure nothing is on enforce list that isn't supported */
6985 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6986 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6987 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6988 return -EINVAL;
6989 }
6990 }
6991
6992 /*
6993 * Minimal mode - state operations are allowed to use machine
6994 * credential. Note this already happens by default, so the
6995 * client doesn't have to do anything more than the negotiation.
6996 *
6997 * NOTE: we don't care if EXCHANGE_ID is in the list -
6998 * we're already using the machine cred for exchange_id
6999 * and will never use a different cred.
7000 */
7001 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7002 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7003 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7004 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7005 dfprintk(MOUNT, "sp4_mach_cred:\n");
7006 dfprintk(MOUNT, " minimal mode enabled\n");
7007 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7008 } else {
7009 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7010 return -EINVAL;
7011 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007012
7013 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007014 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7015 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007016 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7017 dfprintk(MOUNT, " cleanup mode enabled\n");
7018 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7019 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007020
Andrew Elble99ade3c2015-12-02 09:39:51 -05007021 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7022 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7023 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7024 &clp->cl_sp4_flags);
7025 }
7026
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007027 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7028 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7029 dfprintk(MOUNT, " secinfo mode enabled\n");
7030 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7031 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007032
7033 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7034 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7035 dfprintk(MOUNT, " stateid mode enabled\n");
7036 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7037 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007038
7039 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7040 dfprintk(MOUNT, " write mode enabled\n");
7041 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7042 }
7043
7044 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7045 dfprintk(MOUNT, " commit mode enabled\n");
7046 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7047 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007048 }
7049
7050 return 0;
7051}
7052
7053/*
7054 * _nfs4_proc_exchange_id()
7055 *
7056 * Wrapper for EXCHANGE_ID operation.
7057 */
7058static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7059 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007060{
7061 nfs4_verifier verifier;
7062 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007063 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007064 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007065#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007066 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007067 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7068 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7069#else
7070 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7071 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7072#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007073 };
7074 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007075 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007076 };
7077 int status;
7078 struct rpc_message msg = {
7079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7080 .rpc_argp = &args,
7081 .rpc_resp = &res,
7082 .rpc_cred = cred,
7083 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007084
Chuck Leverf0920752012-05-21 22:45:41 -04007085 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007086
7087 status = nfs4_init_uniform_client_string(clp);
7088 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007089 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007090
7091 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007092 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007093 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007094
Chuck Leveracdeb692012-05-21 22:46:16 -04007095 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007096 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007097 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007098 status = -ENOMEM;
7099 goto out;
7100 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007101
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007102 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007103 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007104 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007105 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007106 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007107 }
7108
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007109 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007110 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007111 status = -ENOMEM;
7112 goto out_server_scope;
7113 }
7114
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007115 switch (sp4_how) {
7116 case SP4_NONE:
7117 args.state_protect.how = SP4_NONE;
7118 break;
7119
7120 case SP4_MACH_CRED:
7121 args.state_protect = nfs4_sp4_mach_cred_request;
7122 break;
7123
7124 default:
7125 /* unsupported! */
7126 WARN_ON_ONCE(1);
7127 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007128 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007129 }
7130
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007131 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007132 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007133 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007134 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007135
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007136 if (status == 0)
7137 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7138
Chuck Lever177313f2012-05-21 22:44:58 -04007139 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007140 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007141 clp->cl_exchange_flags = res.flags;
7142 /* Client ID is not confirmed */
7143 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7144 clear_bit(NFS4_SESSION_ESTABLISHED,
7145 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007146 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007147 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007148
Chuck Leveracdeb692012-05-21 22:46:16 -04007149 kfree(clp->cl_serverowner);
7150 clp->cl_serverowner = res.server_owner;
7151 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007152
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007153 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007154 kfree(clp->cl_implid);
7155 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007156 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007157
Chuck Lever177313f2012-05-21 22:44:58 -04007158 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007159 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007160 res.server_scope)) {
7161 dprintk("%s: server_scope mismatch detected\n",
7162 __func__);
7163 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007164 kfree(clp->cl_serverscope);
7165 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007166 }
7167
Chuck Lever177313f2012-05-21 22:44:58 -04007168 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007169 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007170 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007171 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007172 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007173
Kinglong Mee6b559702015-07-01 11:54:53 +08007174out_impl_id:
7175 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007176out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007177 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007178out_server_owner:
7179 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007180out:
Chuck Lever177313f2012-05-21 22:44:58 -04007181 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007182 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007183 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007184 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007185 clp->cl_implid->date.seconds,
7186 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007187 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007188 return status;
7189}
7190
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007191/*
7192 * nfs4_proc_exchange_id()
7193 *
7194 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7195 *
7196 * Since the clientid has expired, all compounds using sessions
7197 * associated with the stale clientid will be returning
7198 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7199 * be in some phase of session reset.
7200 *
7201 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7202 */
7203int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7204{
7205 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7206 int status;
7207
7208 /* try SP4_MACH_CRED if krb5i/p */
7209 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7210 authflavor == RPC_AUTH_GSS_KRB5P) {
7211 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7212 if (!status)
7213 return 0;
7214 }
7215
7216 /* try SP4_NONE */
7217 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7218}
7219
Trond Myklebust66245532012-05-25 17:18:09 -04007220static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7221 struct rpc_cred *cred)
7222{
7223 struct rpc_message msg = {
7224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7225 .rpc_argp = clp,
7226 .rpc_cred = cred,
7227 };
7228 int status;
7229
7230 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007231 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007232 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007233 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007234 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7235 return status;
7236}
7237
7238static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7239 struct rpc_cred *cred)
7240{
7241 unsigned int loop;
7242 int ret;
7243
7244 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7245 ret = _nfs4_proc_destroy_clientid(clp, cred);
7246 switch (ret) {
7247 case -NFS4ERR_DELAY:
7248 case -NFS4ERR_CLIENTID_BUSY:
7249 ssleep(1);
7250 break;
7251 default:
7252 return ret;
7253 }
7254 }
7255 return 0;
7256}
7257
7258int nfs4_destroy_clientid(struct nfs_client *clp)
7259{
7260 struct rpc_cred *cred;
7261 int ret = 0;
7262
7263 if (clp->cl_mvops->minor_version < 1)
7264 goto out;
7265 if (clp->cl_exchange_flags == 0)
7266 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007267 if (clp->cl_preserve_clid)
7268 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007269 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007270 ret = nfs4_proc_destroy_clientid(clp, cred);
7271 if (cred)
7272 put_rpccred(cred);
7273 switch (ret) {
7274 case 0:
7275 case -NFS4ERR_STALE_CLIENTID:
7276 clp->cl_exchange_flags = 0;
7277 }
7278out:
7279 return ret;
7280}
7281
Andy Adamson2050f0c2009-04-01 09:22:30 -04007282struct nfs4_get_lease_time_data {
7283 struct nfs4_get_lease_time_args *args;
7284 struct nfs4_get_lease_time_res *res;
7285 struct nfs_client *clp;
7286};
7287
7288static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7289 void *calldata)
7290{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007291 struct nfs4_get_lease_time_data *data =
7292 (struct nfs4_get_lease_time_data *)calldata;
7293
7294 dprintk("--> %s\n", __func__);
7295 /* just setup sequence, do not trigger session recovery
7296 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007297 nfs41_setup_sequence(data->clp->cl_session,
7298 &data->args->la_seq_args,
7299 &data->res->lr_seq_res,
7300 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007301 dprintk("<-- %s\n", __func__);
7302}
7303
7304/*
7305 * Called from nfs4_state_manager thread for session setup, so don't recover
7306 * from sequence operation or clientid errors.
7307 */
7308static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7309{
7310 struct nfs4_get_lease_time_data *data =
7311 (struct nfs4_get_lease_time_data *)calldata;
7312
7313 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007314 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7315 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007316 switch (task->tk_status) {
7317 case -NFS4ERR_DELAY:
7318 case -NFS4ERR_GRACE:
7319 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7320 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7321 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007322 /* fall through */
7323 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007324 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007325 return;
7326 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007327 dprintk("<-- %s\n", __func__);
7328}
7329
Trond Myklebust17280172012-03-11 13:11:00 -04007330static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007331 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7332 .rpc_call_done = nfs4_get_lease_time_done,
7333};
7334
7335int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7336{
7337 struct rpc_task *task;
7338 struct nfs4_get_lease_time_args args;
7339 struct nfs4_get_lease_time_res res = {
7340 .lr_fsinfo = fsinfo,
7341 };
7342 struct nfs4_get_lease_time_data data = {
7343 .args = &args,
7344 .res = &res,
7345 .clp = clp,
7346 };
7347 struct rpc_message msg = {
7348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7349 .rpc_argp = &args,
7350 .rpc_resp = &res,
7351 };
7352 struct rpc_task_setup task_setup = {
7353 .rpc_client = clp->cl_rpcclient,
7354 .rpc_message = &msg,
7355 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007356 .callback_data = &data,
7357 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007358 };
7359 int status;
7360
Chuck Levera9c92d62013-08-09 12:48:18 -04007361 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007362 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007363 dprintk("--> %s\n", __func__);
7364 task = rpc_run_task(&task_setup);
7365
7366 if (IS_ERR(task))
7367 status = PTR_ERR(task);
7368 else {
7369 status = task->tk_status;
7370 rpc_put_task(task);
7371 }
7372 dprintk("<-- %s return %d\n", __func__, status);
7373
7374 return status;
7375}
7376
Andy Adamsonfc931582009-04-01 09:22:31 -04007377/*
7378 * Initialize the values to be used by the client in CREATE_SESSION
7379 * If nfs4_init_session set the fore channel request and response sizes,
7380 * use them.
7381 *
7382 * Set the back channel max_resp_sz_cached to zero to force the client to
7383 * always set csa_cachethis to FALSE because the current implementation
7384 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7385 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007386static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7387 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007388{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007389 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007390 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007391
Andy Adamson18aad3d2013-06-26 12:21:49 -04007392 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7393 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7394
Andy Adamsonfc931582009-04-01 09:22:31 -04007395 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007396 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7397 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007398 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007399 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007400
7401 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007402 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007403 __func__,
7404 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007405 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007406
7407 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007408 args->bc_attrs.max_rqst_sz = max_bc_payload;
7409 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007410 args->bc_attrs.max_resp_sz_cached = 0;
7411 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebustf4f58ed2016-01-23 14:12:52 -05007412 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
Andy Adamsonfc931582009-04-01 09:22:31 -04007413
7414 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7415 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7416 __func__,
7417 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7418 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7419 args->bc_attrs.max_reqs);
7420}
7421
Trond Myklebust79969dd2015-02-18 11:30:18 -08007422static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7423 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007424{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007425 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007426 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007427
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007428 if (rcvd->max_resp_sz > sent->max_resp_sz)
7429 return -EINVAL;
7430 /*
7431 * Our requested max_ops is the minimum we need; we're not
7432 * prepared to break up compounds into smaller pieces than that.
7433 * So, no point even trying to continue if the server won't
7434 * cooperate:
7435 */
7436 if (rcvd->max_ops < sent->max_ops)
7437 return -EINVAL;
7438 if (rcvd->max_reqs == 0)
7439 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007440 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7441 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007442 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007443}
7444
Trond Myklebust79969dd2015-02-18 11:30:18 -08007445static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7446 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007447{
7448 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007449 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007450
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007451 if (!(res->flags & SESSION4_BACK_CHAN))
7452 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007453 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7454 return -EINVAL;
7455 if (rcvd->max_resp_sz < sent->max_resp_sz)
7456 return -EINVAL;
7457 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7458 return -EINVAL;
7459 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007460 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007461 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007462 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007463 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007464out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007465 return 0;
7466}
Andy Adamson8d353012009-04-01 09:22:32 -04007467
Andy Adamson8d353012009-04-01 09:22:32 -04007468static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007469 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007470{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007471 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007472
Trond Myklebust79969dd2015-02-18 11:30:18 -08007473 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007474 if (ret)
7475 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007476 return nfs4_verify_back_channel_attrs(args, res);
7477}
7478
7479static void nfs4_update_session(struct nfs4_session *session,
7480 struct nfs41_create_session_res *res)
7481{
7482 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007483 /* Mark client id and session as being confirmed */
7484 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7485 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007486 session->flags = res->flags;
7487 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007488 if (res->flags & SESSION4_BACK_CHAN)
7489 memcpy(&session->bc_attrs, &res->bc_attrs,
7490 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007491}
7492
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007493static int _nfs4_proc_create_session(struct nfs_client *clp,
7494 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007495{
7496 struct nfs4_session *session = clp->cl_session;
7497 struct nfs41_create_session_args args = {
7498 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007499 .clientid = clp->cl_clientid,
7500 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007501 .cb_program = NFS4_CALLBACK,
7502 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007503 struct nfs41_create_session_res res;
7504
Andy Adamsonfc931582009-04-01 09:22:31 -04007505 struct rpc_message msg = {
7506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7507 .rpc_argp = &args,
7508 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007509 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007510 };
7511 int status;
7512
Chuck Lever6b26cc82016-05-02 14:40:40 -04007513 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007514 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007515
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007516 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007517 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007518
Trond Myklebust43095d32012-11-20 11:13:12 -05007519 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007520 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007521 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007522 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007523 if (clp->cl_seqid == res.seqid)
7524 clp->cl_seqid++;
7525 if (status)
7526 goto out;
7527 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007528 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007529out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007530 return status;
7531}
7532
7533/*
7534 * Issues a CREATE_SESSION operation to the server.
7535 * It is the responsibility of the caller to verify the session is
7536 * expired before calling this routine.
7537 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007538int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007539{
7540 int status;
7541 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007542 struct nfs4_session *session = clp->cl_session;
7543
7544 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7545
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007546 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007547 if (status)
7548 goto out;
7549
Andy Adamsonaacd5532011-11-09 13:58:21 -05007550 /* Init or reset the session slot tables */
7551 status = nfs4_setup_session_slot_tables(session);
7552 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007553 if (status)
7554 goto out;
7555
7556 ptr = (unsigned *)&session->sess_id.data[0];
7557 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7558 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007559out:
7560 dprintk("<-- %s\n", __func__);
7561 return status;
7562}
7563
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007564/*
7565 * Issue the over-the-wire RPC DESTROY_SESSION.
7566 * The caller must serialize access to this routine.
7567 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007568int nfs4_proc_destroy_session(struct nfs4_session *session,
7569 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007570{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007571 struct rpc_message msg = {
7572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7573 .rpc_argp = session,
7574 .rpc_cred = cred,
7575 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007576 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007577
7578 dprintk("--> nfs4_proc_destroy_session\n");
7579
7580 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007581 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7582 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007583
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007584 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007585 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007586
7587 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007588 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007589 "Session has been destroyed regardless...\n", status);
7590
7591 dprintk("<-- nfs4_proc_destroy_session\n");
7592 return status;
7593}
7594
Trond Myklebust7b38c362012-05-23 13:23:31 -04007595/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007596 * Renew the cl_session lease.
7597 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007598struct nfs4_sequence_data {
7599 struct nfs_client *clp;
7600 struct nfs4_sequence_args args;
7601 struct nfs4_sequence_res res;
7602};
7603
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007604static void nfs41_sequence_release(void *data)
7605{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007606 struct nfs4_sequence_data *calldata = data;
7607 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007608
Alexandros Batsakis71358402010-02-05 03:45:05 -08007609 if (atomic_read(&clp->cl_count) > 1)
7610 nfs4_schedule_state_renewal(clp);
7611 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007612 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007613}
7614
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007615static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7616{
7617 switch(task->tk_status) {
7618 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007619 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7620 return -EAGAIN;
7621 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007622 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007623 }
7624 return 0;
7625}
7626
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007627static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007628{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007629 struct nfs4_sequence_data *calldata = data;
7630 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007631
Trond Myklebust14516c32010-07-31 14:29:06 -04007632 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7633 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007634
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007635 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007636 if (task->tk_status < 0) {
7637 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007638 if (atomic_read(&clp->cl_count) == 1)
7639 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007640
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007641 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7642 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007643 return;
7644 }
7645 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007646 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007647out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007648 dprintk("<-- %s\n", __func__);
7649}
7650
7651static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7652{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007653 struct nfs4_sequence_data *calldata = data;
7654 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007655 struct nfs4_sequence_args *args;
7656 struct nfs4_sequence_res *res;
7657
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007658 args = task->tk_msg.rpc_argp;
7659 res = task->tk_msg.rpc_resp;
7660
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007661 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007662}
7663
7664static const struct rpc_call_ops nfs41_sequence_ops = {
7665 .rpc_call_done = nfs41_sequence_call_done,
7666 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007667 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007668};
7669
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007670static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7671 struct rpc_cred *cred,
7672 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007673{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007674 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007675 struct rpc_message msg = {
7676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7677 .rpc_cred = cred,
7678 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007679 struct rpc_task_setup task_setup_data = {
7680 .rpc_client = clp->cl_rpcclient,
7681 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007682 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007683 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007684 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007685
Alexandros Batsakis71358402010-02-05 03:45:05 -08007686 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007687 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007688 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007689 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007690 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007691 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007692 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007693 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007694 if (is_privileged)
7695 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007696 msg.rpc_argp = &calldata->args;
7697 msg.rpc_resp = &calldata->res;
7698 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007699 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007700
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007701 return rpc_run_task(&task_setup_data);
7702}
7703
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007704static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007705{
7706 struct rpc_task *task;
7707 int ret = 0;
7708
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007709 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007710 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007711 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007712 if (IS_ERR(task))
7713 ret = PTR_ERR(task);
7714 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007715 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007716 dprintk("<-- %s status=%d\n", __func__, ret);
7717 return ret;
7718}
7719
7720static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7721{
7722 struct rpc_task *task;
7723 int ret;
7724
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007725 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007726 if (IS_ERR(task)) {
7727 ret = PTR_ERR(task);
7728 goto out;
7729 }
7730 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007731 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007732 ret = task->tk_status;
7733 rpc_put_task(task);
7734out:
7735 dprintk("<-- %s status=%d\n", __func__, ret);
7736 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007737}
7738
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007739struct nfs4_reclaim_complete_data {
7740 struct nfs_client *clp;
7741 struct nfs41_reclaim_complete_args arg;
7742 struct nfs41_reclaim_complete_res res;
7743};
7744
7745static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7746{
7747 struct nfs4_reclaim_complete_data *calldata = data;
7748
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007749 nfs41_setup_sequence(calldata->clp->cl_session,
7750 &calldata->arg.seq_args,
7751 &calldata->res.seq_res,
7752 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007753}
7754
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007755static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7756{
7757 switch(task->tk_status) {
7758 case 0:
7759 case -NFS4ERR_COMPLETE_ALREADY:
7760 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7761 break;
7762 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007763 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007764 /* fall through */
7765 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007766 return -EAGAIN;
7767 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007768 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007769 }
7770 return 0;
7771}
7772
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007773static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7774{
7775 struct nfs4_reclaim_complete_data *calldata = data;
7776 struct nfs_client *clp = calldata->clp;
7777 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7778
7779 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007780 if (!nfs41_sequence_done(task, res))
7781 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007782
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007783 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007784 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7785 rpc_restart_call_prepare(task);
7786 return;
7787 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007788 dprintk("<-- %s\n", __func__);
7789}
7790
7791static void nfs4_free_reclaim_complete_data(void *data)
7792{
7793 struct nfs4_reclaim_complete_data *calldata = data;
7794
7795 kfree(calldata);
7796}
7797
7798static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7799 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7800 .rpc_call_done = nfs4_reclaim_complete_done,
7801 .rpc_release = nfs4_free_reclaim_complete_data,
7802};
7803
7804/*
7805 * Issue a global reclaim complete.
7806 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007807static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7808 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007809{
7810 struct nfs4_reclaim_complete_data *calldata;
7811 struct rpc_task *task;
7812 struct rpc_message msg = {
7813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007814 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007815 };
7816 struct rpc_task_setup task_setup_data = {
7817 .rpc_client = clp->cl_rpcclient,
7818 .rpc_message = &msg,
7819 .callback_ops = &nfs4_reclaim_complete_call_ops,
7820 .flags = RPC_TASK_ASYNC,
7821 };
7822 int status = -ENOMEM;
7823
7824 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007825 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007826 if (calldata == NULL)
7827 goto out;
7828 calldata->clp = clp;
7829 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007830
Chuck Levera9c92d62013-08-09 12:48:18 -04007831 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007832 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007833 msg.rpc_argp = &calldata->arg;
7834 msg.rpc_resp = &calldata->res;
7835 task_setup_data.callback_data = calldata;
7836 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007837 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007838 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007839 goto out;
7840 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007841 status = nfs4_wait_for_completion_rpc_task(task);
7842 if (status == 0)
7843 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007844 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007845 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007846out:
7847 dprintk("<-- %s status=%d\n", __func__, status);
7848 return status;
7849}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007850
7851static void
7852nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7853{
7854 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007855 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007856 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007857
7858 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007859 nfs41_setup_sequence(session, &lgp->args.seq_args,
7860 &lgp->res.seq_res, task);
7861 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007862}
7863
7864static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7865{
7866 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04007867
7868 dprintk("--> %s\n", __func__);
7869 nfs41_sequence_done(task, &lgp->res.seq_res);
7870 dprintk("<-- %s\n", __func__);
7871}
7872
7873static int
7874nfs4_layoutget_handle_exception(struct rpc_task *task,
7875 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
7876{
Trond Myklebustee314c22012-10-01 17:25:48 -07007877 struct inode *inode = lgp->args.inode;
7878 struct nfs_server *server = NFS_SERVER(inode);
7879 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007880 int nfs4err = task->tk_status;
7881 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007882 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007883
Boaz Harroshed7e5422014-01-22 20:34:54 +02007884 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007885
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007886 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007887 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007888 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007889
7890 /*
7891 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7892 * on the file. set tk_status to -ENODATA to tell upper layer to
7893 * retry go inband.
7894 */
7895 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04007896 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007897 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007898 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007899 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7900 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7901 */
7902 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007903 status = -EOVERFLOW;
7904 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007905 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007906 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007907 * (or clients) writing to the same RAID stripe except when
7908 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04007909 *
7910 * Treat it like we would RECALLCONFLICT -- we retry for a little
7911 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02007912 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007913 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04007914 if (lgp->args.minlength == 0) {
7915 status = -EOVERFLOW;
7916 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007917 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007918 status = -EBUSY;
7919 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04007920 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007921 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007922 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07007923 case -NFS4ERR_EXPIRED:
7924 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04007925 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07007926 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007927 lo = NFS_I(inode)->layout;
7928 /* If the open stateid was bad, then recover it. */
7929 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
7930 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04007931 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007932 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04007933 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007934 break;
7935 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007936
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007937 /*
7938 * Mark the bad layout state as invalid, then retry
7939 */
Trond Myklebust668f4552016-07-24 17:08:59 -04007940 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007941 spin_unlock(&inode->i_lock);
7942 pnfs_free_lseg_list(&head);
7943 status = -EAGAIN;
7944 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007945 }
Jeff Layton183d9e72016-05-17 12:28:47 -04007946
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007947 err = nfs4_handle_exception(server, nfs4err, exception);
7948 if (!status) {
7949 if (exception->retry)
7950 status = -EAGAIN;
7951 else
7952 status = err;
7953 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007954out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007955 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007956 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007957}
7958
Idan Kedar85541162012-08-02 11:47:10 +03007959static size_t max_response_pages(struct nfs_server *server)
7960{
7961 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7962 return nfs_page_array_len(0, max_resp_sz);
7963}
7964
7965static void nfs4_free_pages(struct page **pages, size_t size)
7966{
7967 int i;
7968
7969 if (!pages)
7970 return;
7971
7972 for (i = 0; i < size; i++) {
7973 if (!pages[i])
7974 break;
7975 __free_page(pages[i]);
7976 }
7977 kfree(pages);
7978}
7979
7980static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7981{
7982 struct page **pages;
7983 int i;
7984
7985 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7986 if (!pages) {
7987 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7988 return NULL;
7989 }
7990
7991 for (i = 0; i < size; i++) {
7992 pages[i] = alloc_page(gfp_flags);
7993 if (!pages[i]) {
7994 dprintk("%s: failed to allocate page\n", __func__);
7995 nfs4_free_pages(pages, size);
7996 return NULL;
7997 }
7998 }
7999
8000 return pages;
8001}
8002
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008003static void nfs4_layoutget_release(void *calldata)
8004{
8005 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008006 struct inode *inode = lgp->args.inode;
8007 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008008 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008009
8010 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008011 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008012 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008013 put_nfs_open_context(lgp->args.ctx);
8014 kfree(calldata);
8015 dprintk("<-- %s\n", __func__);
8016}
8017
8018static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8019 .rpc_call_prepare = nfs4_layoutget_prepare,
8020 .rpc_call_done = nfs4_layoutget_done,
8021 .rpc_release = nfs4_layoutget_release,
8022};
8023
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008024struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008025nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008026{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008027 struct inode *inode = lgp->args.inode;
8028 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008029 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008030 struct rpc_task *task;
8031 struct rpc_message msg = {
8032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8033 .rpc_argp = &lgp->args,
8034 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008035 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008036 };
8037 struct rpc_task_setup task_setup_data = {
8038 .rpc_client = server->client,
8039 .rpc_message = &msg,
8040 .callback_ops = &nfs4_layoutget_call_ops,
8041 .callback_data = lgp,
8042 .flags = RPC_TASK_ASYNC,
8043 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008044 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008045 struct nfs4_exception exception = {
8046 .inode = inode,
8047 .timeout = *timeout,
8048 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008049 int status = 0;
8050
8051 dprintk("--> %s\n", __func__);
8052
Peng Tao4bd5a982014-11-17 11:05:17 +08008053 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8054 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8055
Idan Kedar85541162012-08-02 11:47:10 +03008056 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8057 if (!lgp->args.layout.pages) {
8058 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008059 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008060 }
8061 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8062
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008063 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008064 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008065 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008066
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008067 task = rpc_run_task(&task_setup_data);
8068 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008069 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008070 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008071 if (status == 0) {
8072 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8073 *timeout = exception.timeout;
8074 }
8075
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008076 trace_nfs4_layoutget(lgp->args.ctx,
8077 &lgp->args.range,
8078 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008079 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008080 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008081
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008082 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8083 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008084 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008085 rpc_put_task(task);
8086 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008087 if (status)
8088 return ERR_PTR(status);
8089 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008090}
8091
Benny Halevycbe82602011-05-22 19:52:37 +03008092static void
8093nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8094{
8095 struct nfs4_layoutreturn *lrp = calldata;
8096
8097 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008098 nfs41_setup_sequence(lrp->clp->cl_session,
8099 &lrp->args.seq_args,
8100 &lrp->res.seq_res,
8101 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008102}
8103
8104static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8105{
8106 struct nfs4_layoutreturn *lrp = calldata;
8107 struct nfs_server *server;
8108
8109 dprintk("--> %s\n", __func__);
8110
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008111 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008112 return;
8113
8114 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008115 switch (task->tk_status) {
8116 default:
8117 task->tk_status = 0;
8118 case 0:
8119 break;
8120 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008121 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008122 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008123 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008124 return;
8125 }
Benny Halevycbe82602011-05-22 19:52:37 +03008126 dprintk("<-- %s\n", __func__);
8127}
8128
8129static void nfs4_layoutreturn_release(void *calldata)
8130{
8131 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008132 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008133 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008134
8135 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008136 spin_lock(&lo->plh_inode->i_lock);
Jeff Layton6d597e12016-05-17 12:28:42 -04008137 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range,
8138 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust45fcc7b2016-07-24 12:26:34 -04008139 if (lrp->res.lrs_present && pnfs_layout_is_valid(lo))
Trond Myklebust849b2862012-09-24 14:18:39 -04008140 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05008141 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008142 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008143 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008144 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008145 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008146 kfree(calldata);
8147 dprintk("<-- %s\n", __func__);
8148}
8149
8150static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8151 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8152 .rpc_call_done = nfs4_layoutreturn_done,
8153 .rpc_release = nfs4_layoutreturn_release,
8154};
8155
Peng Tao6c166052014-11-17 09:30:40 +08008156int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008157{
8158 struct rpc_task *task;
8159 struct rpc_message msg = {
8160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8161 .rpc_argp = &lrp->args,
8162 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008163 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008164 };
8165 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008166 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008167 .rpc_message = &msg,
8168 .callback_ops = &nfs4_layoutreturn_call_ops,
8169 .callback_data = lrp,
8170 };
Peng Tao6c166052014-11-17 09:30:40 +08008171 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008172
Andrew Elble99ade3c2015-12-02 09:39:51 -05008173 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8174 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8175 &task_setup_data.rpc_client, &msg);
8176
Benny Halevycbe82602011-05-22 19:52:37 +03008177 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008178 if (!sync) {
8179 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8180 if (!lrp->inode) {
8181 nfs4_layoutreturn_release(lrp);
8182 return -EAGAIN;
8183 }
8184 task_setup_data.flags |= RPC_TASK_ASYNC;
8185 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008186 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008187 task = rpc_run_task(&task_setup_data);
8188 if (IS_ERR(task))
8189 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008190 if (sync)
8191 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008192 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008193 dprintk("<-- %s status=%d\n", __func__, status);
8194 rpc_put_task(task);
8195 return status;
8196}
8197
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008198static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008199_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8200 struct pnfs_device *pdev,
8201 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008202{
8203 struct nfs4_getdeviceinfo_args args = {
8204 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008205 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8206 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008207 };
8208 struct nfs4_getdeviceinfo_res res = {
8209 .pdev = pdev,
8210 };
8211 struct rpc_message msg = {
8212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8213 .rpc_argp = &args,
8214 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008215 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008216 };
8217 int status;
8218
8219 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008220 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008221 if (res.notification & ~args.notify_types)
8222 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008223 if (res.notification != args.notify_types)
8224 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008225
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008226 dprintk("<-- %s status=%d\n", __func__, status);
8227
8228 return status;
8229}
8230
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008231int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8232 struct pnfs_device *pdev,
8233 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008234{
8235 struct nfs4_exception exception = { };
8236 int err;
8237
8238 do {
8239 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008240 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008241 &exception);
8242 } while (exception.retry);
8243 return err;
8244}
8245EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8246
Andy Adamson863a3c62011-03-23 13:27:54 +00008247static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8248{
8249 struct nfs4_layoutcommit_data *data = calldata;
8250 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008251 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008252
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008253 nfs41_setup_sequence(session,
8254 &data->args.seq_args,
8255 &data->res.seq_res,
8256 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008257}
8258
8259static void
8260nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8261{
8262 struct nfs4_layoutcommit_data *data = calldata;
8263 struct nfs_server *server = NFS_SERVER(data->args.inode);
8264
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008265 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008266 return;
8267
8268 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008269 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8270 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8271 case -NFS4ERR_BADLAYOUT: /* no layout */
8272 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008273 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008274 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008275 break;
8276 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008277 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008278 rpc_restart_call_prepare(task);
8279 return;
8280 }
8281 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008282}
8283
8284static void nfs4_layoutcommit_release(void *calldata)
8285{
8286 struct nfs4_layoutcommit_data *data = calldata;
8287
Andy Adamsondb29c082011-07-30 20:52:38 -04008288 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008289 nfs_post_op_update_inode_force_wcc(data->args.inode,
8290 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008291 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008292 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008293 kfree(data);
8294}
8295
8296static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8297 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8298 .rpc_call_done = nfs4_layoutcommit_done,
8299 .rpc_release = nfs4_layoutcommit_release,
8300};
8301
8302int
Andy Adamsonef311532011-03-12 02:58:10 -05008303nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008304{
8305 struct rpc_message msg = {
8306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8307 .rpc_argp = &data->args,
8308 .rpc_resp = &data->res,
8309 .rpc_cred = data->cred,
8310 };
8311 struct rpc_task_setup task_setup_data = {
8312 .task = &data->task,
8313 .rpc_client = NFS_CLIENT(data->args.inode),
8314 .rpc_message = &msg,
8315 .callback_ops = &nfs4_layoutcommit_ops,
8316 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008317 };
8318 struct rpc_task *task;
8319 int status = 0;
8320
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008321 dprintk("NFS: initiating layoutcommit call. sync %d "
8322 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008323 data->args.lastbytewritten,
8324 data->args.inode->i_ino);
8325
Trond Myklebust472e2592015-02-05 16:50:30 -05008326 if (!sync) {
8327 data->inode = nfs_igrab_and_active(data->args.inode);
8328 if (data->inode == NULL) {
8329 nfs4_layoutcommit_release(data);
8330 return -EAGAIN;
8331 }
8332 task_setup_data.flags = RPC_TASK_ASYNC;
8333 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008334 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008335 task = rpc_run_task(&task_setup_data);
8336 if (IS_ERR(task))
8337 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008338 if (sync)
8339 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008340 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008341 dprintk("%s: status %d\n", __func__, status);
8342 rpc_put_task(task);
8343 return status;
8344}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008345
Andy Adamson97431202013-08-08 10:57:56 -04008346/**
8347 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8348 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8349 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008350static int
8351_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008352 struct nfs_fsinfo *info,
8353 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008354{
8355 struct nfs41_secinfo_no_name_args args = {
8356 .style = SECINFO_STYLE_CURRENT_FH,
8357 };
8358 struct nfs4_secinfo_res res = {
8359 .flavors = flavors,
8360 };
8361 struct rpc_message msg = {
8362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8363 .rpc_argp = &args,
8364 .rpc_resp = &res,
8365 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008366 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008367 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008368 int status;
8369
8370 if (use_integrity) {
8371 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008372 cred = nfs4_get_clid_cred(server->nfs_client);
8373 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008374 }
8375
8376 dprintk("--> %s\n", __func__);
8377 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8378 &res.seq_res, 0);
8379 dprintk("<-- %s status=%d\n", __func__, status);
8380
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008381 if (cred)
8382 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008383
8384 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008385}
8386
8387static int
8388nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8389 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8390{
8391 struct nfs4_exception exception = { };
8392 int err;
8393 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008394 /* first try using integrity protection */
8395 err = -NFS4ERR_WRONGSEC;
8396
8397 /* try to use integrity protection with machine cred */
8398 if (_nfs4_is_integrity_protected(server->nfs_client))
8399 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8400 flavors, true);
8401
8402 /*
8403 * if unable to use integrity protection, or SECINFO with
8404 * integrity protection returns NFS4ERR_WRONGSEC (which is
8405 * disallowed by spec, but exists in deployed servers) use
8406 * the current filesystem's rpc_client and the user cred.
8407 */
8408 if (err == -NFS4ERR_WRONGSEC)
8409 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8410 flavors, false);
8411
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008412 switch (err) {
8413 case 0:
8414 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008415 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008416 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008417 default:
8418 err = nfs4_handle_exception(server, err, &exception);
8419 }
8420 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008421out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008422 return err;
8423}
8424
8425static int
8426nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8427 struct nfs_fsinfo *info)
8428{
8429 int err;
8430 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008431 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008432 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008433 struct nfs4_secinfo4 *secinfo;
8434 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008435
8436 page = alloc_page(GFP_KERNEL);
8437 if (!page) {
8438 err = -ENOMEM;
8439 goto out;
8440 }
8441
8442 flavors = page_address(page);
8443 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8444
8445 /*
8446 * Fall back on "guess and check" method if
8447 * the server doesn't support SECINFO_NO_NAME
8448 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008449 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008450 err = nfs4_find_root_sec(server, fhandle, info);
8451 goto out_freepage;
8452 }
8453 if (err)
8454 goto out_freepage;
8455
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008456 for (i = 0; i < flavors->num_flavors; i++) {
8457 secinfo = &flavors->flavors[i];
8458
8459 switch (secinfo->flavor) {
8460 case RPC_AUTH_NULL:
8461 case RPC_AUTH_UNIX:
8462 case RPC_AUTH_GSS:
8463 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8464 &secinfo->flavor_info);
8465 break;
8466 default:
8467 flavor = RPC_AUTH_MAXFLAVOR;
8468 break;
8469 }
8470
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008471 if (!nfs_auth_info_match(&server->auth_info, flavor))
8472 flavor = RPC_AUTH_MAXFLAVOR;
8473
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008474 if (flavor != RPC_AUTH_MAXFLAVOR) {
8475 err = nfs4_lookup_root_sec(server, fhandle,
8476 info, flavor);
8477 if (!err)
8478 break;
8479 }
8480 }
8481
8482 if (flavor == RPC_AUTH_MAXFLAVOR)
8483 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008484
8485out_freepage:
8486 put_page(page);
8487 if (err == -EACCES)
8488 return -EPERM;
8489out:
8490 return err;
8491}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008492
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008493static int _nfs41_test_stateid(struct nfs_server *server,
8494 nfs4_stateid *stateid,
8495 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008496{
8497 int status;
8498 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008499 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008500 };
8501 struct nfs41_test_stateid_res res;
8502 struct rpc_message msg = {
8503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8504 .rpc_argp = &args,
8505 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008506 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008507 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008508 struct rpc_clnt *rpc_client = server->client;
8509
8510 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8511 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008512
Chuck Lever38527b12012-07-11 16:30:23 -04008513 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008514 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008515 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008516 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008517 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008518 if (status != NFS_OK) {
8519 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008520 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008521 }
8522 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008523 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008524}
8525
Chuck Lever38527b12012-07-11 16:30:23 -04008526/**
8527 * nfs41_test_stateid - perform a TEST_STATEID operation
8528 *
8529 * @server: server / transport on which to perform the operation
8530 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008531 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008532 *
8533 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8534 * Otherwise a negative NFS4ERR value is returned if the operation
8535 * failed or the state ID is not currently valid.
8536 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008537static int nfs41_test_stateid(struct nfs_server *server,
8538 nfs4_stateid *stateid,
8539 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008540{
8541 struct nfs4_exception exception = { };
8542 int err;
8543 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008544 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008545 if (err != -NFS4ERR_DELAY)
8546 break;
8547 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008548 } while (exception.retry);
8549 return err;
8550}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008551
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008552struct nfs_free_stateid_data {
8553 struct nfs_server *server;
8554 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008555 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008556};
8557
8558static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8559{
8560 struct nfs_free_stateid_data *data = calldata;
8561 nfs41_setup_sequence(nfs4_get_session(data->server),
8562 &data->args.seq_args,
8563 &data->res.seq_res,
8564 task);
8565}
8566
8567static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8568{
8569 struct nfs_free_stateid_data *data = calldata;
8570
8571 nfs41_sequence_done(task, &data->res.seq_res);
8572
8573 switch (task->tk_status) {
8574 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008575 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008576 rpc_restart_call_prepare(task);
8577 }
8578}
8579
8580static void nfs41_free_stateid_release(void *calldata)
8581{
8582 kfree(calldata);
8583}
8584
Trond Myklebust17f26b12013-08-21 15:48:42 -04008585static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008586 .rpc_call_prepare = nfs41_free_stateid_prepare,
8587 .rpc_call_done = nfs41_free_stateid_done,
8588 .rpc_release = nfs41_free_stateid_release,
8589};
8590
8591static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8592 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008593 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008594 bool privileged)
8595{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008596 struct rpc_message msg = {
8597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008598 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008599 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008600 struct rpc_task_setup task_setup = {
8601 .rpc_client = server->client,
8602 .rpc_message = &msg,
8603 .callback_ops = &nfs41_free_stateid_ops,
8604 .flags = RPC_TASK_ASYNC,
8605 };
8606 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008607
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008608 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8609 &task_setup.rpc_client, &msg);
8610
Chuck Lever38527b12012-07-11 16:30:23 -04008611 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008612 data = kmalloc(sizeof(*data), GFP_NOFS);
8613 if (!data)
8614 return ERR_PTR(-ENOMEM);
8615 data->server = server;
8616 nfs4_stateid_copy(&data->args.stateid, stateid);
8617
8618 task_setup.callback_data = data;
8619
8620 msg.rpc_argp = &data->args;
8621 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008622 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008623 if (privileged)
8624 nfs4_set_sequence_privileged(&data->args.seq_args);
8625
8626 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008627}
8628
Chuck Lever38527b12012-07-11 16:30:23 -04008629/**
8630 * nfs41_free_stateid - perform a FREE_STATEID operation
8631 *
8632 * @server: server / transport on which to perform the operation
8633 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008634 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008635 *
8636 * Returns NFS_OK if the server freed "stateid". Otherwise a
8637 * negative NFS4ERR value is returned.
8638 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008639static int nfs41_free_stateid(struct nfs_server *server,
8640 nfs4_stateid *stateid,
8641 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008642{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008643 struct rpc_task *task;
8644 int ret;
8645
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008646 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008647 if (IS_ERR(task))
8648 return PTR_ERR(task);
8649 ret = rpc_wait_for_completion_task(task);
8650 if (!ret)
8651 ret = task->tk_status;
8652 rpc_put_task(task);
8653 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008654}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008655
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008656static void
8657nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008658{
8659 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008660 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008661
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008662 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008663 nfs4_free_lock_state(server, lsp);
8664 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008665 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008666 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008667}
8668
Trond Myklebust36281ca2012-03-04 18:13:56 -05008669static bool nfs41_match_stateid(const nfs4_stateid *s1,
8670 const nfs4_stateid *s2)
8671{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008672 if (s1->type != s2->type)
8673 return false;
8674
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008675 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008676 return false;
8677
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008678 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008679 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008680 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008681 return true;
8682
8683 return false;
8684}
8685
Andy Adamson557134a2009-04-01 09:21:53 -04008686#endif /* CONFIG_NFS_V4_1 */
8687
Trond Myklebust36281ca2012-03-04 18:13:56 -05008688static bool nfs4_match_stateid(const nfs4_stateid *s1,
8689 const nfs4_stateid *s2)
8690{
Trond Myklebustf597c532012-03-04 18:13:56 -05008691 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008692}
8693
8694
Trond Myklebust17280172012-03-11 13:11:00 -04008695static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008696 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008697 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 .recover_open = nfs4_open_reclaim,
8699 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008700 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008701 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702};
8703
Andy Adamson591d71c2009-04-01 09:22:47 -04008704#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008705static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008706 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8707 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8708 .recover_open = nfs4_open_reclaim,
8709 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008710 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008711 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008712 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008713};
8714#endif /* CONFIG_NFS_V4_1 */
8715
Trond Myklebust17280172012-03-11 13:11:00 -04008716static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008717 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008718 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008719 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008721 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722};
8723
Andy Adamson591d71c2009-04-01 09:22:47 -04008724#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008725static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008726 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8727 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008728 .recover_open = nfs41_open_expired,
8729 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008730 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008731};
8732#endif /* CONFIG_NFS_V4_1 */
8733
Trond Myklebust17280172012-03-11 13:11:00 -04008734static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008735 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008736 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008737 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008738};
8739
8740#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008741static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008742 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008743 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008744 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008745};
8746#endif
8747
Chuck Leverec011fe2013-10-17 14:12:39 -04008748static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008749 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008750 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008751};
8752
8753#if defined(CONFIG_NFS_V4_1)
8754static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008755 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008756 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008757};
8758#endif /* CONFIG_NFS_V4_1 */
8759
Trond Myklebust97dc1352010-06-16 09:52:26 -04008760static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8761 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008762 .init_caps = NFS_CAP_READDIRPLUS
8763 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008764 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008765 .init_client = nfs40_init_client,
8766 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008767 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008768 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008769 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008770 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008771 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008772 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8773 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8774 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008775 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008776};
8777
8778#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008779static struct nfs_seqid *
8780nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8781{
8782 return NULL;
8783}
8784
Trond Myklebust97dc1352010-06-16 09:52:26 -04008785static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8786 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008787 .init_caps = NFS_CAP_READDIRPLUS
8788 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008789 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008790 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008791 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008792 .init_client = nfs41_init_client,
8793 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008794 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008795 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008796 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008797 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008798 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008799 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8800 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8801 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008802 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008803};
8804#endif
8805
Steve Dickson42c2c422013-05-22 12:50:38 -04008806#if defined(CONFIG_NFS_V4_2)
8807static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8808 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008809 .init_caps = NFS_CAP_READDIRPLUS
8810 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008811 | NFS_CAP_POSIX_LOCK
8812 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008813 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008814 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008815 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008816 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008817 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008818 | NFS_CAP_LAYOUTSTATS
8819 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008820 .init_client = nfs41_init_client,
8821 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008822 .match_stateid = nfs41_match_stateid,
8823 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008824 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008825 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008826 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008827 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8828 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8829 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008830 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008831};
8832#endif
8833
Trond Myklebust97dc1352010-06-16 09:52:26 -04008834const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8835 [0] = &nfs_v4_0_minor_ops,
8836#if defined(CONFIG_NFS_V4_1)
8837 [1] = &nfs_v4_1_minor_ops,
8838#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008839#if defined(CONFIG_NFS_V4_2)
8840 [2] = &nfs_v4_2_minor_ops,
8841#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008842};
8843
Trond Myklebust13997822016-07-24 17:10:52 -04008844static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008845{
8846 ssize_t error, error2;
8847
8848 error = generic_listxattr(dentry, list, size);
8849 if (error < 0)
8850 return error;
8851 if (list) {
8852 list += error;
8853 size -= error;
8854 }
8855
8856 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8857 if (error2 < 0)
8858 return error2;
8859 return error + error2;
8860}
8861
Trond Myklebust17f26b12013-08-21 15:48:42 -04008862static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008863 .create = nfs_create,
8864 .lookup = nfs_lookup,
8865 .atomic_open = nfs_atomic_open,
8866 .link = nfs_link,
8867 .unlink = nfs_unlink,
8868 .symlink = nfs_symlink,
8869 .mkdir = nfs_mkdir,
8870 .rmdir = nfs_rmdir,
8871 .mknod = nfs_mknod,
8872 .rename = nfs_rename,
8873 .permission = nfs_permission,
8874 .getattr = nfs_getattr,
8875 .setattr = nfs_setattr,
8876 .getxattr = generic_getxattr,
8877 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008878 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04008879 .removexattr = generic_removexattr,
8880};
8881
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008882static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008883 .permission = nfs_permission,
8884 .getattr = nfs_getattr,
8885 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008886 .getxattr = generic_getxattr,
8887 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008888 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008889 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008890};
8891
David Howells509de812006-08-22 20:06:11 -04008892const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 .version = 4, /* protocol version */
8894 .dentry_ops = &nfs4_dentry_operations,
8895 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008896 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008897 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008899 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008900 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 .getattr = nfs4_proc_getattr,
8902 .setattr = nfs4_proc_setattr,
8903 .lookup = nfs4_proc_lookup,
8904 .access = nfs4_proc_access,
8905 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906 .create = nfs4_proc_create,
8907 .remove = nfs4_proc_remove,
8908 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008909 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008911 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008912 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008913 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 .link = nfs4_proc_link,
8915 .symlink = nfs4_proc_symlink,
8916 .mkdir = nfs4_proc_mkdir,
8917 .rmdir = nfs4_proc_remove,
8918 .readdir = nfs4_proc_readdir,
8919 .mknod = nfs4_proc_mknod,
8920 .statfs = nfs4_proc_statfs,
8921 .fsinfo = nfs4_proc_fsinfo,
8922 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008923 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008925 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008927 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008929 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008931 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008932 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008934 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008935 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008936 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008937 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008938 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008939 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008940 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008941 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008942 .create_server = nfs4_create_server,
8943 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944};
8945
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008946static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01008947 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008948 .list = nfs4_xattr_list_nfs4_acl,
8949 .get = nfs4_xattr_get_nfs4_acl,
8950 .set = nfs4_xattr_set_nfs4_acl,
8951};
8952
8953const struct xattr_handler *nfs4_xattr_handlers[] = {
8954 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008955#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8956 &nfs4_xattr_nfs4_label_handler,
8957#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008958 NULL
8959};
8960
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961/*
8962 * Local variables:
8963 * c-basic-offset: 8
8964 * End:
8965 */