blob: 02eab9113273a6f32242bef73dc220411b3bd77c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400366 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500368 int ret = errorcode;
369
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400370 exception->delay = 0;
371 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 exception->retry = 0;
373 switch(errorcode) {
374 case 0:
375 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500376 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400380 if (inode) {
381 int err;
382
383 err = nfs_async_inode_return_delegation(inode,
384 stateid);
385 if (err == 0)
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
389 break;
390 }
391 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 if (state == NULL)
393 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400394 ret = nfs4_schedule_stateid_recovery(server, state);
395 if (ret < 0)
396 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400398 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400399 if (state != NULL) {
400 ret = nfs4_schedule_stateid_recovery(server, state);
401 if (ret < 0)
402 break;
403 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500405 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400408 case -NFS4ERR_MOVED:
409 ret = nfs4_schedule_migration_recovery(server);
410 if (ret < 0)
411 break;
412 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500416#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
425 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400427 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500428#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
432 * fail
433 */
434 ret = -EBUSY;
435 break;
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400438 nfs_inc_server_stats(server, NFSIOS_DELAY);
439 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400440 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400441 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400442 exception->delay = 1;
443 return 0;
444
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800448 break;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
457 "uid/gids. "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
460 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 }
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500464wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->recovering = 1;
466 return 0;
467}
468
469/* This is the error handling routine for processes that are allowed
470 * to sleep.
471 */
472int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
473{
474 struct nfs_client *clp = server->nfs_client;
475 int ret;
476
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
480 goto out_retry;
481 }
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
485 return -EIO;
486 goto out_retry;
487 }
488 return ret;
489out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500490 if (ret == 0)
491 exception->retry = 1;
492 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500493}
494
Trond Myklebust037fc982015-09-20 15:51:00 -0400495static int
496nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
498{
499 struct nfs_client *clp = server->nfs_client;
500 int ret;
501
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
505 goto out_retry;
506 }
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
511 goto out_retry;
512 }
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 ret = -EIO;
515 return ret;
516out_retry:
517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
520}
521
522static int
523nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
525{
526 struct nfs4_exception exception = {
527 .state = state,
528 };
529
530 if (task->tk_status >= 0)
531 return 0;
532 if (timeout)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
535 task->tk_status,
536 &exception);
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
539 if (exception.retry)
540 return -EAGAIN;
541 return 0;
542}
543
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400544/*
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
547 */
548static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
549{
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
551
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
554 return true;
555
556 return false;
557}
Trond Myklebust65de8722008-12-23 15:21:44 -0500558
Trond Myklebust452e9352010-07-31 14:29:06 -0400559static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
565}
566
Trond Myklebust452e9352010-07-31 14:29:06 -0400567static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
568{
Trond Myklebustbe824162015-07-05 14:50:46 -0400569 struct nfs_client *clp = server->nfs_client;
570
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400573}
574
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400575struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
579};
580
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800581void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400583{
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
587
588 res->sr_slot = NULL;
589}
590
591static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
592{
593 args->sa_privileged = 1;
594}
595
Peng Taocb04ad22014-06-11 05:24:15 +0800596int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400600{
Chuck Lever3bd23842013-08-09 12:49:19 -0400601 struct nfs4_slot *slot;
602
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
605 goto out_start;
606
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
609 goto out_sleep;
610
611 slot = nfs4_alloc_slot(tbl);
612 if (IS_ERR(slot)) {
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
615 goto out_sleep;
616 }
617 spin_unlock(&tbl->slot_tbl_lock);
618
Trond Myklebust0a014a42016-09-22 13:38:51 -0400619 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400620 args->sa_slot = slot;
621 res->sr_slot = slot;
622
623out_start:
624 rpc_call_start(task);
625 return 0;
626
627out_sleep:
628 if (args->sa_privileged)
629 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
630 NULL, RPC_PRIORITY_PRIVILEGED);
631 else
632 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
633 spin_unlock(&tbl->slot_tbl_lock);
634 return -EAGAIN;
635}
Peng Taocb04ad22014-06-11 05:24:15 +0800636EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400637
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400638static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400639{
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
642
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 tbl = slot->table;
644 spin_lock(&tbl->slot_tbl_lock);
645 if (!nfs41_wake_and_assign_slot(tbl, slot))
646 nfs4_free_slot(tbl, slot);
647 spin_unlock(&tbl->slot_tbl_lock);
648
649 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400650}
651
652static int nfs40_sequence_done(struct rpc_task *task,
653 struct nfs4_sequence_res *res)
654{
655 if (res->sr_slot != NULL)
656 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400657 return 1;
658}
659
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400660#if defined(CONFIG_NFS_V4_1)
661
Trond Myklebustd185a332010-06-16 09:52:25 -0400662static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400663{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500664 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400665 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500667 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400668
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500669 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500670 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500671
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400672 /* Bump the slot sequence number */
673 if (slot->seq_done)
674 slot->seq_nr++;
675 slot->seq_done = 0;
676
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500677 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500678 /* Be nice to the server: try to ensure that the last transmitted
679 * value for highest_user_slotid <= target_highest_slotid
680 */
681 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
682 send_new_highest_used_slotid = true;
683
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500684 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500685 send_new_highest_used_slotid = false;
686 goto out_unlock;
687 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500688 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500689
690 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
691 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500692out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500693 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500695 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400696 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400697 if (waitqueue_active(&tbl->slot_waitq))
698 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400699}
700
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400701static int nfs41_sequence_process(struct rpc_task *task,
702 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400706 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500707 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500708 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400709
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500710 if (slot == NULL)
711 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400712 /* don't increment the sequence number if the task wasn't sent */
713 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400714 goto out;
715
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500716 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500717
Trond Myklebustac20d162012-12-15 15:36:07 -0500718 if (slot->interrupted) {
719 slot->interrupted = 0;
720 interrupted = true;
721 }
722
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400723 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500724 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 switch (res->sr_status) {
726 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400727 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400728 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500729 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500730 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500731 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400732 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
733 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500734 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500736 case 1:
737 /*
738 * sr_status remains 1 if an RPC level error occurred.
739 * The server may or may not have processed the sequence
740 * operation..
741 * Mark the slot as having hosted an interrupted RPC call.
742 */
743 slot->interrupted = 1;
744 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400745 case -NFS4ERR_DELAY:
746 /* The server detected a resend of the RPC call and
747 * returned NFS4ERR_DELAY as per Section 2.10.6.2
748 * of RFC5661.
749 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500750 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400751 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500752 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500753 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400754 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500755 case -NFS4ERR_BADSLOT:
756 /*
757 * The slot id we used was probably retired. Try again
758 * using a different slot id.
759 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500760 goto retry_nowait;
761 case -NFS4ERR_SEQ_MISORDERED:
762 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 * Was the last operation on this sequence interrupted?
764 * If so, retry after bumping the sequence number.
765 */
766 if (interrupted) {
767 ++slot->seq_nr;
768 goto retry_nowait;
769 }
770 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500771 * Could this slot have been previously retired?
772 * If so, then the server may be expecting seq_nr = 1!
773 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500774 if (slot->seq_nr != 1) {
775 slot->seq_nr = 1;
776 goto retry_nowait;
777 }
778 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500779 case -NFS4ERR_SEQ_FALSE_RETRY:
780 ++slot->seq_nr;
781 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 default:
783 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400784 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400785 }
786out:
787 /* The session may be reset by one of the error handlers. */
788 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500789out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500790 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500791retry_nowait:
792 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 task->tk_status = 0;
795 ret = 0;
796 }
797 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400799 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 goto out;
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
802 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400804
805int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (!nfs41_sequence_process(task, res))
808 return 0;
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
811 return 1;
812
813}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500814EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400815
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400816static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817{
818 if (res->sr_slot == NULL)
819 return 1;
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
823}
824
825static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826{
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
830 else
831 nfs40_sequence_free_slot(res);
832 }
833}
834
Peng Tao2c4b1312014-06-11 05:24:16 +0800835int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400836{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500837 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842}
Peng Tao2c4b1312014-06-11 05:24:16 +0800843EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000845int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846 struct nfs4_sequence_args *args,
847 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848 struct rpc_task *task)
849{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400850 struct nfs4_slot *slot;
851 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400852
853 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400855 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400856 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400857
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400858 tbl = &session->fc_slot_table;
859
Trond Myklebust69d206b2012-11-22 13:21:02 -0500860 task->tk_timeout = 0;
861
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400862 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400863 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400864 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500865 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500866 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400867 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400868 }
869
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500870 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500871 if (IS_ERR(slot)) {
872 /* If out of memory, try again in 1/4 second */
873 if (slot == ERR_PTR(-ENOMEM))
874 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400875 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400876 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400877 }
878 spin_unlock(&tbl->slot_tbl_lock);
879
Trond Myklebust0a014a42016-09-22 13:38:51 -0400880 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500881 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400882
Chuck Levere8d92382013-08-09 12:47:51 -0400883 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500884 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400885
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500887 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400888 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 /*
890 * sr_status is only set in decode_sequence, and so will remain
891 * set to 1 if an rpc level failure occurs.
892 */
893 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400894 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400895out_success:
896 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400899 /* Privileged tasks are queued with top priority */
900 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400901 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
902 NULL, RPC_PRIORITY_PRIVILEGED);
903 else
904 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400905 spin_unlock(&tbl->slot_tbl_lock);
906 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000908EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400909
Chuck Lever5a580e02013-08-09 12:48:09 -0400910static int nfs4_setup_sequence(const struct nfs_server *server,
911 struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400914{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400915 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400916 int ret = 0;
917
Chuck Lever3bd23842013-08-09 12:49:19 -0400918 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
920 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400921
Chuck Levere8d92382013-08-09 12:47:51 -0400922 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400923 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400924 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400925
Trond Myklebust9d12b212012-01-17 22:04:25 -0500926 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400927
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 dprintk("<-- %s status=%d\n", __func__, ret);
929 return ret;
930}
931
Andy Adamsonce5039c2009-04-01 09:22:13 -0400932static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
933{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400934 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400935 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400936
Trond Myklebust035168ab2010-06-16 09:52:26 -0400937 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
938
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400939 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940}
941
Andy Adamson69ab40c2009-04-01 09:22:19 -0400942static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
943{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400944 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400945
Trond Myklebust14516c32010-07-31 14:29:06 -0400946 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400947}
948
Trond Myklebust17280172012-03-11 13:11:00 -0400949static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400951 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400952};
953
Chuck Lever3bd23842013-08-09 12:49:19 -0400954#else /* !CONFIG_NFS_V4_1 */
955
Chuck Lever5a580e02013-08-09 12:48:09 -0400956static int nfs4_setup_sequence(const struct nfs_server *server,
957 struct nfs4_sequence_args *args,
958 struct nfs4_sequence_res *res,
959 struct rpc_task *task)
960{
Peng Taocb04ad22014-06-11 05:24:15 +0800961 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
962 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400963}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400964
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400965static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
966{
967 return nfs40_sequence_done(task, res);
968}
969
970static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
971{
972 if (res->sr_slot != NULL)
973 nfs40_sequence_free_slot(res);
974}
975
Peng Tao2c4b1312014-06-11 05:24:16 +0800976int nfs4_sequence_done(struct rpc_task *task,
977 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400978{
Chuck Lever3bd23842013-08-09 12:49:19 -0400979 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400980}
Peng Tao2c4b1312014-06-11 05:24:16 +0800981EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400982
983#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400984
Chuck Lever9915ea72013-08-09 12:48:27 -0400985static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
986{
987 struct nfs4_call_sync_data *data = calldata;
988 nfs4_setup_sequence(data->seq_server,
989 data->seq_args, data->seq_res, task);
990}
991
992static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
993{
994 struct nfs4_call_sync_data *data = calldata;
995 nfs4_sequence_done(task, data->seq_res);
996}
997
998static const struct rpc_call_ops nfs40_call_sync_ops = {
999 .rpc_call_prepare = nfs40_call_sync_prepare,
1000 .rpc_call_done = nfs40_call_sync_done,
1001};
1002
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001003static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001004 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs4_sequence_args *args,
1007 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001008{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 int ret;
1010 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001011 struct nfs_client *clp = server->nfs_client;
1012 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001013 .seq_server = server,
1014 .seq_args = args,
1015 .seq_res = res,
1016 };
1017 struct rpc_task_setup task_setup = {
1018 .rpc_client = clnt,
1019 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001020 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001021 .callback_data = &data
1022 };
1023
1024 task = rpc_run_task(&task_setup);
1025 if (IS_ERR(task))
1026 ret = PTR_ERR(task);
1027 else {
1028 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001029 rpc_put_task(task);
1030 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001031 return ret;
1032}
1033
Bryan Schumaker7c513052011-03-24 17:12:24 +00001034int nfs4_call_sync(struct rpc_clnt *clnt,
1035 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001036 struct rpc_message *msg,
1037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 int cache_reply)
1040{
Chuck Levera9c92d62013-08-09 12:48:18 -04001041 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1046{
1047 struct nfs_inode *nfsi = NFS_I(dir);
1048
1049 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001051 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001053 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001054 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001055 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 spin_unlock(&dir->i_lock);
1057}
1058
1059struct nfs4_opendata {
1060 struct kref kref;
1061 struct nfs_openargs o_arg;
1062 struct nfs_openres o_res;
1063 struct nfs_open_confirmargs c_arg;
1064 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 struct nfs4_string owner_name;
1066 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001067 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001069 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001071 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct nfs4_state_owner *owner;
1073 struct nfs4_state *state;
1074 struct iattr attrs;
1075 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001076 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001077 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001078 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001079 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int cancelled;
1081};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001082
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001083static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1084 int err, struct nfs4_exception *exception)
1085{
1086 if (err != -EINVAL)
1087 return false;
1088 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1089 return false;
1090 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1091 exception->retry = 1;
1092 return true;
1093}
1094
Trond Myklebust6ae37332015-01-30 14:21:14 -05001095static u32
1096nfs4_map_atomic_open_share(struct nfs_server *server,
1097 fmode_t fmode, int openflags)
1098{
1099 u32 res = 0;
1100
1101 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1102 case FMODE_READ:
1103 res = NFS4_SHARE_ACCESS_READ;
1104 break;
1105 case FMODE_WRITE:
1106 res = NFS4_SHARE_ACCESS_WRITE;
1107 break;
1108 case FMODE_READ|FMODE_WRITE:
1109 res = NFS4_SHARE_ACCESS_BOTH;
1110 }
1111 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1112 goto out;
1113 /* Want no delegation if we're using O_DIRECT */
1114 if (openflags & O_DIRECT)
1115 res |= NFS4_SHARE_WANT_NO_DELEG;
1116out:
1117 return res;
1118}
1119
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001120static enum open_claim_type4
1121nfs4_map_atomic_open_claim(struct nfs_server *server,
1122 enum open_claim_type4 claim)
1123{
1124 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1125 return claim;
1126 switch (claim) {
1127 default:
1128 return claim;
1129 case NFS4_OPEN_CLAIM_FH:
1130 return NFS4_OPEN_CLAIM_NULL;
1131 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1132 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1133 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1134 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1135 }
1136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139{
1140 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001141 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001142 p->o_res.seqid = p->o_arg.seqid;
1143 p->c_res.seqid = p->c_arg.seqid;
1144 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001145 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001147 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001148}
1149
Al Viro82a2c1b2011-06-22 18:30:55 -04001150static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001151 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001152 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001153 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001154 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001155 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156{
Al Viro82a2c1b2011-06-22 18:30:55 -04001157 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001158 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001160 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 struct nfs4_opendata *p;
1162
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001163 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001164 if (p == NULL)
1165 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001166
1167 p->f_label = nfs4_label_alloc(server, gfp_mask);
1168 if (IS_ERR(p->f_label))
1169 goto err_free_p;
1170
Kinglong Meea49c2692015-07-27 15:31:38 +08001171 p->a_label = nfs4_label_alloc(server, gfp_mask);
1172 if (IS_ERR(p->a_label))
1173 goto err_free_f;
1174
Trond Myklebust63f5f792015-01-23 19:19:25 -05001175 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1176 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001177 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001178 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001179 nfs_sb_active(dentry->d_sb);
1180 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001181 p->dir = parent;
1182 p->owner = sp;
1183 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 p->o_arg.open_flags = flags;
1185 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001186 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1187 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001188 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1189 * will return permission denied for all bits until close */
1190 if (!(flags & O_EXCL)) {
1191 /* ask server to check for all possible rights as results
1192 * are cached */
1193 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1194 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001195 }
David Howells7539bba2006-08-22 20:06:09 -04001196 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001197 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1198 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001199 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001201 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001202 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001203 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001204 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1205 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001206 case NFS4_OPEN_CLAIM_NULL:
1207 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1208 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1209 p->o_arg.fh = NFS_FH(dir);
1210 break;
1211 case NFS4_OPEN_CLAIM_PREVIOUS:
1212 case NFS4_OPEN_CLAIM_FH:
1213 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1214 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001215 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001216 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001217 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001218 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001219
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 p->o_arg.u.attrs = &p->attrs;
1221 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001222
1223 verf[0] = jiffies;
1224 verf[1] = current->pid;
1225 memcpy(p->o_arg.u.verifier.data, verf,
1226 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001228 p->c_arg.fh = &p->o_res.fh;
1229 p->c_arg.stateid = &p->o_res.stateid;
1230 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001232 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001234
1235err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001236 nfs4_label_free(p->a_label);
1237err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001238 nfs4_label_free(p->f_label);
1239err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001240 kfree(p);
1241err:
1242 dput(parent);
1243 return NULL;
1244}
1245
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001246static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 struct nfs4_opendata *p = container_of(kref,
1249 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001250 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251
1252 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001253 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001254 if (p->state != NULL)
1255 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001256 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001257
Kinglong Meea49c2692015-07-27 15:31:38 +08001258 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001259 nfs4_label_free(p->f_label);
1260
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001261 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001262 dput(p->dentry);
1263 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001264 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001265 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001266 kfree(p);
1267}
1268
1269static void nfs4_opendata_put(struct nfs4_opendata *p)
1270{
1271 if (p != NULL)
1272 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001273}
1274
Trond Myklebust06f814a2006-01-03 09:55:07 +01001275static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1276{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001277 int ret;
1278
Trond Myklebust06f814a2006-01-03 09:55:07 +01001279 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001280 return ret;
1281}
1282
Trond Myklebust24311f82015-09-20 10:50:17 -04001283static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1284 fmode_t fmode)
1285{
1286 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1287 case FMODE_READ|FMODE_WRITE:
1288 return state->n_rdwr != 0;
1289 case FMODE_WRITE:
1290 return state->n_wronly != 0;
1291 case FMODE_READ:
1292 return state->n_rdonly != 0;
1293 }
1294 WARN_ON_ONCE(1);
1295 return false;
1296}
1297
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001299{
1300 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301
Trond Myklebust536e43d2012-01-17 22:04:26 -05001302 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001303 goto out;
1304 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001306 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1307 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308 break;
1309 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001310 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1311 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001312 break;
1313 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001314 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1315 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001318 return ret;
1319}
1320
Trond Myklebust2a606182015-08-19 22:30:00 -05001321static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1322 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001323{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001324 if (delegation == NULL)
1325 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001327 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001328 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1329 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001330 switch (claim) {
1331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_FH:
1333 break;
1334 case NFS4_OPEN_CLAIM_PREVIOUS:
1335 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1336 break;
1337 default:
1338 return 0;
1339 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001340 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 return 1;
1342}
1343
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001345{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001347 case FMODE_WRITE:
1348 state->n_wronly++;
1349 break;
1350 case FMODE_READ:
1351 state->n_rdonly++;
1352 break;
1353 case FMODE_READ|FMODE_WRITE:
1354 state->n_rdwr++;
1355 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001357}
1358
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001360{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 struct nfs_client *clp = state->owner->so_server->nfs_client;
1362 bool need_recover = false;
1363
1364 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1365 need_recover = true;
1366 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1367 need_recover = true;
1368 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1369 need_recover = true;
1370 if (need_recover)
1371 nfs4_state_mark_reclaim_nograce(clp, state);
1372}
1373
Trond Myklebuste999e802014-02-10 18:20:47 -05001374static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1375 nfs4_stateid *stateid)
1376{
1377 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1378 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001379 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1380 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001381 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001382 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001383 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1384 return true;
1385 return false;
1386}
1387
Trond Myklebustf95549c2015-01-23 18:06:09 -05001388static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1389{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001390 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1391 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001392 if (state->n_wronly)
1393 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 if (state->n_rdonly)
1395 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1396 if (state->n_rdwr)
1397 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001398 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001399}
1400
Trond Myklebust226056c2014-02-11 10:41:07 -05001401static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001402 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001403 nfs4_stateid *stateid, fmode_t fmode)
1404{
1405 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1406 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1407 case FMODE_WRITE:
1408 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1409 break;
1410 case FMODE_READ:
1411 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1412 break;
1413 case 0:
1414 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1415 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1416 clear_bit(NFS_OPEN_STATE, &state->flags);
1417 }
1418 if (stateid == NULL)
1419 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001420 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001421 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1422 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1423 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001424 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001425 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001426 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001428 nfs4_stateid_copy(&state->stateid, stateid);
1429 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001430}
1431
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001432static void nfs_clear_open_stateid(struct nfs4_state *state,
1433 nfs4_stateid *arg_stateid,
1434 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001435{
1436 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001437 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001438 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001439 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1440 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001441}
1442
Trond Myklebust003707c2007-07-05 18:07:55 -04001443static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1444{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001446 case FMODE_READ:
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1448 break;
1449 case FMODE_WRITE:
1450 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1451 break;
1452 case FMODE_READ|FMODE_WRITE:
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
1454 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001455 if (!nfs_need_update_open_stateid(state, stateid))
1456 return;
1457 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1458 nfs4_stateid_copy(&state->stateid, stateid);
1459 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001460}
1461
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001464 /*
1465 * Protect the call to nfs4_state_set_mode_locked and
1466 * serialise the stateid update
1467 */
Andrew Elble361cad32015-12-02 09:20:57 -05001468 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001469 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001470 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001471 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001472 set_bit(NFS_DELEGATED_STATE, &state->flags);
1473 }
1474 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001476 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001478 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001481static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001482{
1483 struct nfs_inode *nfsi = NFS_I(state->inode);
1484 struct nfs_delegation *deleg_cur;
1485 int ret = 0;
1486
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001488
1489 rcu_read_lock();
1490 deleg_cur = rcu_dereference(nfsi->delegation);
1491 if (deleg_cur == NULL)
1492 goto no_delegation;
1493
1494 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001495 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001496 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001497 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001498 goto no_delegation_unlock;
1499
1500 if (delegation == NULL)
1501 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001502 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001503 goto no_delegation_unlock;
1504
Trond Myklebustb7391f42008-12-23 15:21:52 -05001505 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001506 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508no_delegation_unlock:
1509 spin_unlock(&deleg_cur->lock);
1510no_delegation:
1511 rcu_read_unlock();
1512
1513 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001514 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001515 ret = 1;
1516 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001517 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1518 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001519
1520 return ret;
1521}
1522
Trond Myklebust39071e62015-01-24 15:07:56 -05001523static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1524 const nfs4_stateid *stateid)
1525{
1526 struct nfs4_state *state = lsp->ls_state;
1527 bool ret = false;
1528
1529 spin_lock(&state->state_lock);
1530 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1531 goto out_noupdate;
1532 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1533 goto out_noupdate;
1534 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1535 ret = true;
1536out_noupdate:
1537 spin_unlock(&state->state_lock);
1538 return ret;
1539}
Trond Myklebust34310432008-12-23 15:21:38 -05001540
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542{
1543 struct nfs_delegation *delegation;
1544
1545 rcu_read_lock();
1546 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 rcu_read_unlock();
1549 return;
1550 }
1551 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001552 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553}
1554
Trond Myklebust6ee41262007-07-08 14:11:36 -04001555static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001556{
1557 struct nfs4_state *state = opendata->state;
1558 struct nfs_inode *nfsi = NFS_I(state->inode);
1559 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001560 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001562 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 nfs4_stateid stateid;
1564 int ret = -EAGAIN;
1565
Trond Myklebustaac00a82007-07-05 19:02:21 -04001566 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001567 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001568 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001569 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001570 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001571 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001572 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001573 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001574 rcu_read_lock();
1575 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001576 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001577 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001578 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001579 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001580 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001581 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001582 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001583 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001584 if (!opendata->is_recover) {
1585 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1586 if (ret != 0)
1587 goto out;
1588 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001590
1591 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001592 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001593 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001594 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001595out:
1596 return ERR_PTR(ret);
1597out_return_state:
1598 atomic_inc(&state->count);
1599 return state;
1600}
1601
Andy Adamsone23008e2012-10-02 21:07:32 -04001602static void
1603nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1604{
1605 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1606 struct nfs_delegation *delegation;
1607 int delegation_flags = 0;
1608
1609 rcu_read_lock();
1610 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1611 if (delegation)
1612 delegation_flags = delegation->flags;
1613 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001614 switch (data->o_arg.claim) {
1615 default:
1616 break;
1617 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1618 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001619 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1620 "returning a delegation for "
1621 "OPEN(CLAIM_DELEGATE_CUR)\n",
1622 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001623 return;
1624 }
1625 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001626 nfs_inode_set_delegation(state->inode,
1627 data->owner->so_cred,
1628 &data->o_res);
1629 else
1630 nfs_inode_reclaim_delegation(state->inode,
1631 data->owner->so_cred,
1632 &data->o_res);
1633}
1634
1635/*
1636 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1637 * and update the nfs4_state.
1638 */
1639static struct nfs4_state *
1640_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1641{
1642 struct inode *inode = data->state->inode;
1643 struct nfs4_state *state = data->state;
1644 int ret;
1645
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001646 if (!data->rpc_done) {
1647 if (data->rpc_status) {
1648 ret = data->rpc_status;
1649 goto err;
1650 }
1651 /* cached opens have already been processed */
1652 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001653 }
1654
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 ret = nfs_refresh_inode(inode, &data->f_attr);
1656 if (ret)
1657 goto err;
1658
1659 if (data->o_res.delegation_type != 0)
1660 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001661update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001662 update_open_stateid(state, &data->o_res.stateid, NULL,
1663 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001664 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001665
1666 return state;
1667err:
1668 return ERR_PTR(ret);
1669
1670}
1671
1672static struct nfs4_state *
1673_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674{
1675 struct inode *inode;
1676 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001680 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001681 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682 goto out;
1683 }
1684
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001687 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001688 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001689 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001690 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001691 goto err;
1692 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 state = nfs4_get_open_state(inode, data->owner);
1694 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001695 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001696 if (data->o_res.delegation_type != 0)
1697 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001698 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001699 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001701out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001702 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001704err_put_inode:
1705 iput(inode);
1706err:
1707 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708}
1709
Andy Adamsone23008e2012-10-02 21:07:32 -04001710static struct nfs4_state *
1711nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1712{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001713 struct nfs4_state *ret;
1714
Andy Adamsone23008e2012-10-02 21:07:32 -04001715 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001716 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1717 else
1718 ret = _nfs4_opendata_to_nfs4_state(data);
1719 nfs4_sequence_free_slot(&data->o_res.seq_res);
1720 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001721}
1722
Trond Myklebust864472e2006-01-03 09:55:15 +01001723static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1724{
1725 struct nfs_inode *nfsi = NFS_I(state->inode);
1726 struct nfs_open_context *ctx;
1727
1728 spin_lock(&state->inode->i_lock);
1729 list_for_each_entry(ctx, &nfsi->open_files, list) {
1730 if (ctx->state != state)
1731 continue;
1732 get_nfs_open_context(ctx);
1733 spin_unlock(&state->inode->i_lock);
1734 return ctx;
1735 }
1736 spin_unlock(&state->inode->i_lock);
1737 return ERR_PTR(-ENOENT);
1738}
1739
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001740static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1741 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001742{
1743 struct nfs4_opendata *opendata;
1744
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001745 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001746 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001747 if (opendata == NULL)
1748 return ERR_PTR(-ENOMEM);
1749 opendata->state = state;
1750 atomic_inc(&state->count);
1751 return opendata;
1752}
1753
Trond Myklebust24311f82015-09-20 10:50:17 -04001754static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1755 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001756{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 int ret;
1759
Trond Myklebust24311f82015-09-20 10:50:17 -04001760 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001761 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001762 opendata->o_arg.open_flags = 0;
1763 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001764 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1765 NFS_SB(opendata->dentry->d_sb),
1766 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001767 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1768 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1769 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001770 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001771 if (ret != 0)
1772 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001773 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774 if (IS_ERR(newstate))
1775 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001776 if (newstate != opendata->state)
1777 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001778 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001779 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001780}
1781
1782static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1783{
Trond Myklebust864472e2006-01-03 09:55:15 +01001784 int ret;
1785
Trond Myklebust4f14c192014-02-12 19:15:06 -05001786 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1787 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1788 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1789 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001791 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001792 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001793 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001794 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1795 if (ret != 0)
1796 return ret;
1797 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1798 if (ret != 0)
1799 return ret;
1800 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1801 if (ret != 0)
1802 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 /*
1804 * We may have performed cached opens for all three recoveries.
1805 * Check if we need to update the current stateid.
1806 */
1807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001808 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001809 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001810 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001811 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001812 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 return 0;
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * OPEN_RECLAIM:
1819 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001821static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001823 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001824 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001825 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int status;
1827
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001828 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1829 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001830 if (IS_ERR(opendata))
1831 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001832 rcu_read_lock();
1833 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001834 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001835 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001836 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001837 opendata->o_arg.u.delegation_type = delegation_type;
1838 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001839 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return status;
1841}
1842
Trond Myklebust539cd032007-06-05 11:46:42 -04001843static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct nfs_server *server = NFS_SERVER(state->inode);
1846 struct nfs4_exception exception = { };
1847 int err;
1848 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001849 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001850 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001851 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1852 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001853 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001854 break;
1855 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 } while (exception.retry);
1857 return err;
1858}
1859
Trond Myklebust864472e2006-01-03 09:55:15 +01001860static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1861{
1862 struct nfs_open_context *ctx;
1863 int ret;
1864
1865 ctx = nfs4_state_find_open_context(state);
1866 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001867 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001868 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001869 put_nfs_open_context(ctx);
1870 return ret;
1871}
1872
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001873static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001875 switch (err) {
1876 default:
1877 printk(KERN_ERR "NFS: %s: unhandled error "
1878 "%d.\n", __func__, err);
1879 case 0:
1880 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001881 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001882 case -ESTALE:
1883 break;
1884 case -NFS4ERR_BADSESSION:
1885 case -NFS4ERR_BADSLOT:
1886 case -NFS4ERR_BAD_HIGH_SLOT:
1887 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1888 case -NFS4ERR_DEADSESSION:
1889 set_bit(NFS_DELEGATED_STATE, &state->flags);
1890 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1891 return -EAGAIN;
1892 case -NFS4ERR_STALE_CLIENTID:
1893 case -NFS4ERR_STALE_STATEID:
1894 set_bit(NFS_DELEGATED_STATE, &state->flags);
1895 case -NFS4ERR_EXPIRED:
1896 /* Don't recall a delegation if it was lost */
1897 nfs4_schedule_lease_recovery(server->nfs_client);
1898 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001899 case -NFS4ERR_MOVED:
1900 nfs4_schedule_migration_recovery(server);
1901 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001902 case -NFS4ERR_LEASE_MOVED:
1903 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1904 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001905 case -NFS4ERR_DELEG_REVOKED:
1906 case -NFS4ERR_ADMIN_REVOKED:
1907 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001908 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001909 nfs_inode_find_state_and_recover(state->inode,
1910 stateid);
1911 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001912 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001913 case -NFS4ERR_DELAY:
1914 case -NFS4ERR_GRACE:
1915 set_bit(NFS_DELEGATED_STATE, &state->flags);
1916 ssleep(1);
1917 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001918 case -ENOMEM:
1919 case -NFS4ERR_DENIED:
1920 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1921 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return err;
1924}
1925
Trond Myklebust24311f82015-09-20 10:50:17 -04001926int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1927 struct nfs4_state *state, const nfs4_stateid *stateid,
1928 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001929{
1930 struct nfs_server *server = NFS_SERVER(state->inode);
1931 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001933
1934 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1935 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1936 if (IS_ERR(opendata))
1937 return PTR_ERR(opendata);
1938 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001939 write_seqlock(&state->seqlock);
1940 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1941 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1943 switch (type & (FMODE_READ|FMODE_WRITE)) {
1944 case FMODE_READ|FMODE_WRITE:
1945 case FMODE_WRITE:
1946 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1947 if (err)
1948 break;
1949 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1950 if (err)
1951 break;
1952 case FMODE_READ:
1953 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1954 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001955 nfs4_opendata_put(opendata);
1956 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1957}
1958
Chuck Leverbe05c862013-08-09 12:49:47 -04001959static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1960{
1961 struct nfs4_opendata *data = calldata;
1962
Peng Taocb04ad22014-06-11 05:24:15 +08001963 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1964 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001965}
1966
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001967static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1968{
1969 struct nfs4_opendata *data = calldata;
1970
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001971 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001972
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001973 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001974 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001975 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001976 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001977 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001978 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001979 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001980}
1981
1982static void nfs4_open_confirm_release(void *calldata)
1983{
1984 struct nfs4_opendata *data = calldata;
1985 struct nfs4_state *state = NULL;
1986
1987 /* If this request hasn't been cancelled, do nothing */
1988 if (data->cancelled == 0)
1989 goto out_free;
1990 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001991 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001993 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001994 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001995 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001996out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001997 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001998}
1999
2000static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002001 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 .rpc_call_done = nfs4_open_confirm_done,
2003 .rpc_release = nfs4_open_confirm_release,
2004};
2005
2006/*
2007 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2008 */
2009static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2010{
David Howells2b0143b2015-03-17 22:25:59 +00002011 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002012 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002013 struct rpc_message msg = {
2014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2015 .rpc_argp = &data->c_arg,
2016 .rpc_resp = &data->c_res,
2017 .rpc_cred = data->owner->so_cred,
2018 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002019 struct rpc_task_setup task_setup_data = {
2020 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002021 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 .callback_ops = &nfs4_open_confirm_ops,
2023 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002024 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002025 .flags = RPC_TASK_ASYNC,
2026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int status;
2028
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002029 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002030 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002031 data->rpc_done = 0;
2032 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002033 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002034 if (data->is_recover)
2035 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002036 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002037 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002038 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002039 status = nfs4_wait_for_completion_rpc_task(task);
2040 if (status != 0) {
2041 data->cancelled = 1;
2042 smp_wmb();
2043 } else
2044 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002045 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return status;
2047}
2048
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002049static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002051 struct nfs4_opendata *data = calldata;
2052 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002053 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002054 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002055
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002056 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002057 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002058 /*
2059 * Check if we still need to send an OPEN call, or if we can use
2060 * a delegation instead.
2061 */
2062 if (data->state != NULL) {
2063 struct nfs_delegation *delegation;
2064
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002065 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002066 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002067 rcu_read_lock();
2068 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002069 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002070 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002071 rcu_read_unlock();
2072 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002073 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002074 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002075 switch (claim) {
2076 default:
2077 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002078 case NFS4_OPEN_CLAIM_PREVIOUS:
2079 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2080 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002081 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002082 case NFS4_OPEN_CLAIM_FH:
2083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002084 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2085 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002086 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002087 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002088 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002089 &data->o_res.seq_res,
2090 task) != 0)
2091 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002092
2093 /* Set the create mode (note dependency on the session type) */
2094 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2095 if (data->o_arg.open_flags & O_EXCL) {
2096 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2097 if (nfs4_has_persistent_session(clp))
2098 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2099 else if (clp->cl_mvops->minor_version > 0)
2100 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2101 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002102 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002103unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002104 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002105 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002106out_no_action:
2107 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002108out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002109 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002110}
2111
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112static void nfs4_open_done(struct rpc_task *task, void *calldata)
2113{
2114 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002117
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002118 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002119 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002120
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002122 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2123 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002124 case S_IFREG:
2125 break;
2126 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128 break;
2129 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002131 break;
2132 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002134 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002135 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002136 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002137 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2138 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002139 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002140 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002141}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002142
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143static void nfs4_open_release(void *calldata)
2144{
2145 struct nfs4_opendata *data = calldata;
2146 struct nfs4_state *state = NULL;
2147
2148 /* If this request hasn't been cancelled, do nothing */
2149 if (data->cancelled == 0)
2150 goto out_free;
2151 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002152 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002153 goto out_free;
2154 /* In case we need an open_confirm, no cleanup! */
2155 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2156 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002158 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002159 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002161 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002162}
2163
2164static const struct rpc_call_ops nfs4_open_ops = {
2165 .rpc_call_prepare = nfs4_open_prepare,
2166 .rpc_call_done = nfs4_open_done,
2167 .rpc_release = nfs4_open_release,
2168};
2169
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002170static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002171{
David Howells2b0143b2015-03-17 22:25:59 +00002172 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002173 struct nfs_server *server = NFS_SERVER(dir);
2174 struct nfs_openargs *o_arg = &data->o_arg;
2175 struct nfs_openres *o_res = &data->o_res;
2176 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2179 .rpc_argp = o_arg,
2180 .rpc_resp = o_res,
2181 .rpc_cred = data->owner->so_cred,
2182 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 struct rpc_task_setup task_setup_data = {
2184 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 .callback_ops = &nfs4_open_ops,
2187 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002188 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 .flags = RPC_TASK_ASYNC,
2190 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 int status;
2192
Chuck Levera9c92d62013-08-09 12:48:18 -04002193 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002194 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002195 data->rpc_done = 0;
2196 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002197 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002198 data->is_recover = 0;
2199 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002200 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002201 data->is_recover = 1;
2202 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002203 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002204 if (IS_ERR(task))
2205 return PTR_ERR(task);
2206 status = nfs4_wait_for_completion_rpc_task(task);
2207 if (status != 0) {
2208 data->cancelled = 1;
2209 smp_wmb();
2210 } else
2211 status = data->rpc_status;
2212 rpc_put_task(task);
2213
2214 return status;
2215}
2216
2217static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2218{
David Howells2b0143b2015-03-17 22:25:59 +00002219 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 struct nfs_openres *o_res = &data->o_res;
2221 int status;
2222
2223 status = nfs4_run_open_task(data, 1);
2224 if (status != 0 || !data->rpc_done)
2225 return status;
2226
Trond Myklebust6926afd2012-01-07 13:22:46 -05002227 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2228
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002229 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2230 status = _nfs4_proc_open_confirm(data);
2231 if (status != 0)
2232 return status;
2233 }
2234
2235 return status;
2236}
2237
Trond Myklebustf3792d62014-07-10 08:54:32 -04002238/*
2239 * Additional permission checks in order to distinguish between an
2240 * open for read, and an open for execute. This works around the
2241 * fact that NFSv4 OPEN treats read and execute permissions as being
2242 * the same.
2243 * Note that in the non-execute case, we want to turn off permission
2244 * checking if we just created a new file (POSIX open() semantics).
2245 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002246static int nfs4_opendata_access(struct rpc_cred *cred,
2247 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002248 struct nfs4_state *state, fmode_t fmode,
2249 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002250{
2251 struct nfs_access_entry cache;
2252 u32 mask;
2253
2254 /* access call failed or for some reason the server doesn't
2255 * support any access modes -- defer access call until later */
2256 if (opendata->o_res.access_supported == 0)
2257 return 0;
2258
2259 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002260 /*
2261 * Use openflags to check for exec, because fmode won't
2262 * always have FMODE_EXEC set when file open for exec.
2263 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002264 if (openflags & __FMODE_EXEC) {
2265 /* ONLY check for exec rights */
2266 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002267 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002268 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002269
2270 cache.cred = cred;
2271 cache.jiffies = jiffies;
2272 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2273 nfs_access_add_cache(state->inode, &cache);
2274
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002275 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002276 return 0;
2277
2278 /* even though OPEN succeeded, access is denied. Close the file */
2279 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002280 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002281}
2282
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002283/*
2284 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2285 */
2286static int _nfs4_proc_open(struct nfs4_opendata *data)
2287{
David Howells2b0143b2015-03-17 22:25:59 +00002288 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002289 struct nfs_server *server = NFS_SERVER(dir);
2290 struct nfs_openargs *o_arg = &data->o_arg;
2291 struct nfs_openres *o_res = &data->o_res;
2292 int status;
2293
2294 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002295 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002296 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002297 if (status != 0) {
2298 if (status == -NFS4ERR_BADNAME &&
2299 !(o_arg->open_flags & O_CREAT))
2300 return -ENOENT;
2301 return status;
2302 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303
Trond Myklebust6926afd2012-01-07 13:22:46 -05002304 nfs_fattr_map_and_free_names(server, &data->f_attr);
2305
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002306 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002307 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002308 if (o_arg->open_flags & O_EXCL)
2309 data->file_created = 1;
2310 else if (o_res->cinfo.before != o_res->cinfo.after)
2311 data->file_created = 1;
2312 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002313 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2314 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002321 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Andy Adamsond83217c2011-03-01 01:34:17 +00002325static int nfs4_recover_expired_lease(struct nfs_server *server)
2326{
2327 return nfs4_client_recover_expired_lease(server->nfs_client);
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/*
2331 * OPEN_EXPIRED:
2332 * reclaim state on the server after a network partition.
2333 * Assumes caller holds the appropriate lock
2334 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002335static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002337 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002338 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002340 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002341 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002342 if (IS_ERR(opendata))
2343 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002344 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002345 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002346 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002347 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002351static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002352{
Trond Myklebust539cd032007-06-05 11:46:42 -04002353 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002354 struct nfs4_exception exception = { };
2355 int err;
2356
2357 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002358 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002359 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2361 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002362 switch (err) {
2363 default:
2364 goto out;
2365 case -NFS4ERR_GRACE:
2366 case -NFS4ERR_DELAY:
2367 nfs4_handle_exception(server, err, &exception);
2368 err = 0;
2369 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002370 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002371out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002372 return err;
2373}
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Trond Myklebust864472e2006-01-03 09:55:15 +01002380 ctx = nfs4_state_find_open_context(state);
2381 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002382 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002383 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002384 put_nfs_open_context(ctx);
2385 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Trond Myklebust41020b62016-09-22 13:38:58 -04002388static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2389 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002390{
Trond Myklebust41020b62016-09-22 13:38:58 -04002391 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002392 write_seqlock(&state->seqlock);
2393 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2394 write_sequnlock(&state->seqlock);
2395 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2396}
2397
2398static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2399{
2400 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002401 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002402}
2403
2404static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2405{
2406 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2407 nfs40_clear_delegation_stateid(state);
2408 return nfs4_open_expired(sp, state);
2409}
2410
Trond Myklebust45870d62016-09-22 13:38:59 -04002411static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2412 nfs4_stateid *stateid,
2413 struct rpc_cred *cred)
2414{
2415 return -NFS4ERR_BAD_STATEID;
2416}
2417
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002418#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002419static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2420 nfs4_stateid *stateid,
2421 struct rpc_cred *cred)
2422{
2423 int status;
2424
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002425 switch (stateid->type) {
2426 default:
2427 break;
2428 case NFS4_INVALID_STATEID_TYPE:
2429 case NFS4_SPECIAL_STATEID_TYPE:
2430 return -NFS4ERR_BAD_STATEID;
2431 case NFS4_REVOKED_STATEID_TYPE:
2432 goto out_free;
2433 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002434
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002435 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002436 switch (status) {
2437 case -NFS4ERR_EXPIRED:
2438 case -NFS4ERR_ADMIN_REVOKED:
2439 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002440 break;
2441 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002442 return status;
2443 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002444out_free:
2445 /* Ack the revoked state to the server */
2446 nfs41_free_stateid(server, stateid, cred);
2447 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002448}
2449
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002450static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002451{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002452 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002453 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002454 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002455 struct rpc_cred *cred;
2456 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002457
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002458 /* Get the delegation credential for use by test/free_stateid */
2459 rcu_read_lock();
2460 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002461 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002462 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002463 return;
2464 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002465
2466 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002467 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2468 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002469 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002470 return;
2471 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002472
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002473 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2474 rcu_read_unlock();
2475 return;
2476 }
2477
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002478 cred = get_rpccred(delegation->cred);
2479 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002480 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002481 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002482 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002483 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002484
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002485 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002486}
2487
2488/**
2489 * nfs41_check_open_stateid - possibly free an open stateid
2490 *
2491 * @state: NFSv4 state for an inode
2492 *
2493 * Returns NFS_OK if recovery for this stateid is now finished.
2494 * Otherwise a negative NFS4ERR value is returned.
2495 */
2496static int nfs41_check_open_stateid(struct nfs4_state *state)
2497{
2498 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002499 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002500 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002501 int status;
2502
2503 /* If a state reset has been done, test_stateid is unneeded */
2504 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2505 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2506 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2507 return -NFS4ERR_BAD_STATEID;
2508
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002510 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002511 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002512 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2513 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2514 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002515 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002516 }
2517 return status;
2518}
2519
2520static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2521{
Chuck Levereb64cf92012-07-11 16:30:05 -04002522 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002523
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002524 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002525 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002526 if (status != NFS_OK)
2527 status = nfs4_open_expired(sp, state);
2528 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002529}
2530#endif
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002533 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2534 * fields corresponding to attributes that were used to store the verifier.
2535 * Make sure we clobber those fields in the later setattr call
2536 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002537static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2538 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002539{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002540 const u32 *attrset = opendata->o_res.attrset;
2541
2542 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002543 !(sattr->ia_valid & ATTR_ATIME_SET))
2544 sattr->ia_valid |= ATTR_ATIME;
2545
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002546 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002547 !(sattr->ia_valid & ATTR_MTIME_SET))
2548 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002549
2550 /* Except MODE, it seems harmless of setting twice. */
2551 if ((attrset[1] & FATTR4_WORD1_MODE))
2552 sattr->ia_valid &= ~ATTR_MODE;
2553
2554 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2555 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002556}
2557
Trond Myklebustc21443c2013-02-07 14:26:21 -05002558static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2559 fmode_t fmode,
2560 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002561 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002562{
2563 struct nfs4_state_owner *sp = opendata->owner;
2564 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002565 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002566 struct nfs4_state *state;
2567 unsigned int seq;
2568 int ret;
2569
2570 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2571
2572 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002573 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002574 goto out;
2575
2576 state = nfs4_opendata_to_nfs4_state(opendata);
2577 ret = PTR_ERR(state);
2578 if (IS_ERR(state))
2579 goto out;
2580 if (server->caps & NFS_CAP_POSIX_LOCK)
2581 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002582 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2583 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002584
Trond Myklebust275bb302013-05-29 13:11:28 -04002585 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002586 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002587 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002588 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002589 alias = d_exact_alias(dentry, state->inode);
2590 if (!alias)
2591 alias = d_splice_alias(igrab(state->inode), dentry);
2592 /* d_splice_alias() can't fail here - it's a non-directory */
2593 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002594 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002595 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002596 }
2597 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002598 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002599 }
2600
Trond Myklebustc21443c2013-02-07 14:26:21 -05002601 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2602 if (ret != 0)
2603 goto out;
2604
Trond Myklebust3efb9722013-05-29 13:17:04 -04002605 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002606 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002607 nfs_inode_attach_open_context(ctx);
2608 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2609 nfs4_schedule_stateid_recovery(server, state);
2610 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002611out:
2612 return ret;
2613}
2614
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002615/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002616 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 */
Andy Adamson82be4172012-05-23 05:02:35 -04002618static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002619 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002620 int flags,
2621 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002622 struct nfs4_label *label,
2623 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 struct nfs4_state_owner *sp;
2626 struct nfs4_state *state = NULL;
2627 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002628 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002629 struct dentry *dentry = ctx->dentry;
2630 struct rpc_cred *cred = ctx->cred;
2631 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2632 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002633 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002634 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002635 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002639 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2640 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2642 goto out_err;
2643 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002644 status = nfs4_recover_expired_lease(server);
2645 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002646 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002647 if (d_really_is_positive(dentry))
2648 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002649 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002650 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002651 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002652 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002653 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002654 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002655 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
David Quigley14c43f72013-05-22 12:50:43 -04002657 if (label) {
2658 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2659 if (IS_ERR(olabel)) {
2660 status = PTR_ERR(olabel);
2661 goto err_opendata_put;
2662 }
2663 }
2664
Trond Myklebuste911b812014-03-26 13:24:37 -07002665 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2666 if (!opendata->f_attr.mdsthreshold) {
2667 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2668 if (!opendata->f_attr.mdsthreshold)
2669 goto err_free_label;
2670 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002671 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002672 }
David Howells2b0143b2015-03-17 22:25:59 +00002673 if (d_really_is_positive(dentry))
2674 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002675
Trond Myklebust3efb9722013-05-29 13:17:04 -04002676 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002677 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002678 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002679 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002680
NeilBrownefcbc042015-07-30 13:00:56 +10002681 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002682 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002683 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002684 /*
2685 * send create attributes which was not set by open
2686 * with an extra setattr.
2687 */
2688 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2689 nfs_fattr_init(opendata->o_res.f_attr);
2690 status = nfs4_do_setattr(state->inode, cred,
2691 opendata->o_res.f_attr, sattr,
2692 state, label, olabel);
2693 if (status == 0) {
2694 nfs_setattr_update_inode(state->inode, sattr,
2695 opendata->o_res.f_attr);
2696 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2697 }
David Quigley1775fd32013-05-22 12:50:42 -04002698 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002699 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002700 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002701 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002702
Trond Myklebuste911b812014-03-26 13:24:37 -07002703 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002704 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002705 opendata->f_attr.mdsthreshold = NULL;
2706 }
Andy Adamson82be4172012-05-23 05:02:35 -04002707
David Quigley14c43f72013-05-22 12:50:43 -04002708 nfs4_label_free(olabel);
2709
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002710 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002713err_free_label:
2714 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002715err_opendata_put:
2716 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002717err_put_state_owner:
2718 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return status;
2721}
2722
2723
Andy Adamson82be4172012-05-23 05:02:35 -04002724static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002725 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002726 int flags,
2727 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002728 struct nfs4_label *label,
2729 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002731 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 struct nfs4_exception exception = { };
2733 struct nfs4_state *res;
2734 int status;
2735
2736 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002737 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002738 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002739 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (status == 0)
2741 break;
2742 /* NOTE: BAD_SEQID means the server and client disagree about the
2743 * book-keeping w.r.t. state-changing operations
2744 * (OPEN/CLOSE/LOCK/LOCKU...)
2745 * It is actually a sign of a bug on the client or on the server.
2746 *
2747 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002748 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 * have unhashed the old state_owner for us, and that we can
2750 * therefore safely retry using a new one. We should still warn
2751 * the user though...
2752 */
2753 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002754 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002755 " returned a bad sequence-id error!\n",
2756 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 exception.retry = 1;
2758 continue;
2759 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002760 /*
2761 * BAD_STATEID on OPEN means that the server cancelled our
2762 * state before it received the OPEN_CONFIRM.
2763 * Recover by retrying the request as per the discussion
2764 * on Page 181 of RFC3530.
2765 */
2766 if (status == -NFS4ERR_BAD_STATEID) {
2767 exception.retry = 1;
2768 continue;
2769 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002770 if (status == -EAGAIN) {
2771 /* We must have found a delegation */
2772 exception.retry = 1;
2773 continue;
2774 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002775 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2776 continue;
2777 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 status, &exception));
2779 } while (exception.retry);
2780 return res;
2781}
2782
Trond Myklebust8487c472016-06-26 08:44:35 -04002783static int _nfs4_do_setattr(struct inode *inode,
2784 struct nfs_setattrargs *arg,
2785 struct nfs_setattrres *res,
2786 struct rpc_cred *cred,
2787 struct nfs4_state *state)
2788{
2789 struct nfs_server *server = NFS_SERVER(inode);
2790 struct rpc_message msg = {
2791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2792 .rpc_argp = arg,
2793 .rpc_resp = res,
2794 .rpc_cred = cred,
2795 };
2796 struct rpc_cred *delegation_cred = NULL;
2797 unsigned long timestamp = jiffies;
2798 fmode_t fmode;
2799 bool truncate;
2800 int status;
2801
2802 nfs_fattr_init(res->fattr);
2803
2804 /* Servers should only apply open mode checks for file size changes */
2805 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2806 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2807
2808 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2809 /* Use that stateid */
2810 } else if (truncate && state != NULL) {
2811 struct nfs_lockowner lockowner = {
2812 .l_owner = current->files,
2813 .l_pid = current->tgid,
2814 };
2815 if (!nfs4_valid_open_stateid(state))
2816 return -EBADF;
2817 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2818 &arg->stateid, &delegation_cred) == -EIO)
2819 return -EBADF;
2820 } else
2821 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2822 if (delegation_cred)
2823 msg.rpc_cred = delegation_cred;
2824
2825 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2826
2827 put_rpccred(delegation_cred);
2828 if (status == 0 && state != NULL)
2829 renew_lease(server, timestamp);
2830 trace_nfs4_setattr(inode, &arg->stateid, status);
2831 return status;
2832}
2833
2834static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2835 struct nfs_fattr *fattr, struct iattr *sattr,
2836 struct nfs4_state *state, struct nfs4_label *ilabel,
2837 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002839 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002841 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 .iap = sattr,
2843 .server = server,
2844 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002845 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 };
2847 struct nfs_setattrres res = {
2848 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002849 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 .server = server,
2851 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002852 struct nfs4_exception exception = {
2853 .state = state,
2854 .inode = inode,
2855 .stateid = &arg.stateid,
2856 };
2857 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
David Quigleyaa9c2662013-05-22 12:50:44 -04002859 arg.bitmask = nfs4_bitmask(server, ilabel);
2860 if (ilabel)
2861 arg.bitmask = nfs4_bitmask(server, olabel);
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002864 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002865 switch (err) {
2866 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002867 if (!(sattr->ia_valid & ATTR_SIZE)) {
2868 pr_warn_once("NFSv4: server %s is incorrectly "
2869 "applying open mode checks to "
2870 "a SETATTR that is not "
2871 "changing file size.\n",
2872 server->nfs_client->cl_hostname);
2873 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002874 if (state && !(state->state & FMODE_WRITE)) {
2875 err = -EBADF;
2876 if (sattr->ia_valid & ATTR_OPEN)
2877 err = -EACCES;
2878 goto out;
2879 }
2880 }
2881 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002883out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return err;
2885}
2886
Peng Tao500d7012015-09-22 11:35:22 +08002887static bool
2888nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2889{
2890 if (inode == NULL || !nfs_have_layout(inode))
2891 return false;
2892
2893 return pnfs_wait_on_layoutreturn(inode, task);
2894}
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896struct nfs4_closedata {
2897 struct inode *inode;
2898 struct nfs4_state *state;
2899 struct nfs_closeargs arg;
2900 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002901 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002902 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002903 bool roc;
2904 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905};
2906
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002907static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002908{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002909 struct nfs4_closedata *calldata = data;
2910 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002911 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002912
Fred Isamanf7e89172011-01-06 11:36:32 +00002913 if (calldata->roc)
2914 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002915 nfs4_put_open_state(calldata->state);
2916 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002917 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002918 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002919 kfree(calldata);
2920}
2921
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002922static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002924 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002927 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Chuck Levera3ca5652012-03-01 17:00:40 -05002929 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002930 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2931 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002932 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 /* hmm. we are done with the inode, and in the process of freeing
2934 * the state_owner. we keep this around to process errors
2935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 switch (task->tk_status) {
2937 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002938 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002939 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002940 pnfs_roc_set_barrier(state->inode,
2941 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002942 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002943 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002944 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002946 case -NFS4ERR_OLD_STATEID:
2947 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002949 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002950 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002951 rpc_restart_call_prepare(task);
2952 goto out_release;
2953 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002954 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002957 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002958 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002959 goto out_release;
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002962 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2963 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002964out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002965 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002966 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002967 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002970static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002972 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002973 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002974 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002975 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002976 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002977
Chuck Levera3ca5652012-03-01 17:00:40 -05002978 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002979 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002980 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002981
Trond Myklebust88069f72009-12-08 08:33:16 -05002982 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002983 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002984 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2985 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2986 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002987 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002988 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002989 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002990 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002991 if (state->n_rdonly == 0)
2992 call_close |= is_rdonly;
2993 else if (is_rdonly)
2994 calldata->arg.fmode |= FMODE_READ;
2995 if (state->n_wronly == 0)
2996 call_close |= is_wronly;
2997 else if (is_wronly)
2998 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002999 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3000 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003001 } else if (is_rdwr)
3002 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3003
Trond Myklebust5d422302013-03-14 16:57:48 -04003004 if (!nfs4_valid_open_stateid(state))
3005 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003006 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003007
3008 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003009 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003010 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003011 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003012
Peng Tao500d7012015-09-22 11:35:22 +08003013 if (nfs4_wait_on_layoutreturn(inode, task)) {
3014 nfs_release_seqid(calldata->arg.seqid);
3015 goto out_wait;
3016 }
3017
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003018 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003019 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003020 if (calldata->roc)
3021 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3022
Trond Myklebust6ae37332015-01-30 14:21:14 -05003023 calldata->arg.share_access =
3024 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3025 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003026
Trond Myklebust516a6af2005-10-27 22:12:41 -04003027 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003028 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003029 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003030 &calldata->arg.seq_args,
3031 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003032 task) != 0)
3033 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003034 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003035 return;
3036out_no_action:
3037 task->tk_action = NULL;
3038out_wait:
3039 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003042static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003043 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003044 .rpc_call_done = nfs4_close_done,
3045 .rpc_release = nfs4_free_closedata,
3046};
3047
Peng Taofe08c542014-07-03 13:05:01 +08003048static bool nfs4_roc(struct inode *inode)
3049{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003050 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003051 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003052 return pnfs_roc(inode);
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055/*
3056 * It is possible for data to be read/written from a mem-mapped file
3057 * after the sys_close call (which hits the vfs layer as a flush).
3058 * This means that we can't safely call nfsv4 close on a file until
3059 * the inode is cleared. This in turn means that we are not good
3060 * NFSv4 citizens - we do not indicate to the server to update the file's
3061 * share state even when we are done with one of the three share
3062 * stateid's in the inode.
3063 *
3064 * NOTE: Caller must be holding the sp->so_owner semaphore!
3065 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003066int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003068 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003069 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003071 struct nfs4_state_owner *sp = state->owner;
3072 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003073 struct rpc_message msg = {
3074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3075 .rpc_cred = state->owner->so_cred,
3076 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003077 struct rpc_task_setup task_setup_data = {
3078 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003079 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003080 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003081 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003082 .flags = RPC_TASK_ASYNC,
3083 };
Trond Myklebust95121352005-10-18 14:20:12 -07003084 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003086 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3087 &task_setup_data.rpc_client, &msg);
3088
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003089 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003091 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003092 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003093 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003095 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003097 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3098 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003099 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003100 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003101 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003102 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003103 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003104 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003105 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003106 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003107 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003108
Trond Myklebust1174dd12010-12-21 10:52:24 -05003109 msg.rpc_argp = &calldata->arg;
3110 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003111 task_setup_data.callback_data = calldata;
3112 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003113 if (IS_ERR(task))
3114 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003115 status = 0;
3116 if (wait)
3117 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003118 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003119 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003120out_free_calldata:
3121 kfree(calldata);
3122out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003123 nfs4_put_open_state(state);
3124 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003125 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Trond Myklebust2b484292010-09-17 10:56:51 -04003128static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003129nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3130 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003133 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3134
3135 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Trond Myklebust565277f2007-10-15 18:17:53 -04003137 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003138 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003139
3140 nfs4_label_release_security(label);
3141
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003142 if (IS_ERR(state))
3143 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003144 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
Trond Myklebust1185a552009-12-03 15:54:02 -05003147static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003148{
3149 if (ctx->state == NULL)
3150 return;
3151 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003152 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003153 else
Al Viro643168c2011-06-22 18:20:23 -04003154 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Trond Myklebustb944dba2013-11-04 15:20:20 -05003157#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3158#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003159#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3162{
Kinglong Mee8c612822015-08-26 21:12:58 +08003163 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003164 struct nfs4_server_caps_arg args = {
3165 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003166 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003167 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct nfs4_server_caps_res res = {};
3169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003171 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 .rpc_resp = &res,
3173 };
3174 int status;
3175
Kinglong Mee8c612822015-08-26 21:12:58 +08003176 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3177 FATTR4_WORD0_FH_EXPIRE_TYPE |
3178 FATTR4_WORD0_LINK_SUPPORT |
3179 FATTR4_WORD0_SYMLINK_SUPPORT |
3180 FATTR4_WORD0_ACLSUPPORT;
3181 if (minorversion)
3182 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3183
Bryan Schumaker7c513052011-03-24 17:12:24 +00003184 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003186 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003187 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003188 case 0:
3189 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3190 res.attr_bitmask[2] = 0;
3191 break;
3192 case 1:
3193 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3194 break;
3195 case 2:
3196 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003199 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3200 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3201 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3202 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003203 NFS_CAP_CTIME|NFS_CAP_MTIME|
3204 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003205 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3206 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 server->caps |= NFS_CAP_ACLS;
3208 if (res.has_links != 0)
3209 server->caps |= NFS_CAP_HARDLINKS;
3210 if (res.has_symlinks != 0)
3211 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003212 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3213 server->caps |= NFS_CAP_FILEID;
3214 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3215 server->caps |= NFS_CAP_MODE;
3216 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3217 server->caps |= NFS_CAP_NLINK;
3218 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3219 server->caps |= NFS_CAP_OWNER;
3220 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3221 server->caps |= NFS_CAP_OWNER_GROUP;
3222 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3223 server->caps |= NFS_CAP_ATIME;
3224 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3225 server->caps |= NFS_CAP_CTIME;
3226 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3227 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003228#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3229 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3230 server->caps |= NFS_CAP_SECURITY_LABEL;
3231#endif
3232 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3233 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003234 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003235
Trond Myklebusta65318b2009-03-11 14:10:28 -04003236 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3237 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3238 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003239 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003240 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3241 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003243 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return status;
3247}
3248
Trond Myklebust55a97592006-06-09 09:34:19 -04003249int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 struct nfs4_exception exception = { };
3252 int err;
3253 do {
3254 err = nfs4_handle_exception(server,
3255 _nfs4_server_capabilities(server, fhandle),
3256 &exception);
3257 } while (exception.retry);
3258 return err;
3259}
3260
3261static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3262 struct nfs_fsinfo *info)
3263{
David Quigleyaa9c2662013-05-22 12:50:44 -04003264 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003266 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 };
3268 struct nfs4_lookup_res res = {
3269 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003270 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 .fh = fhandle,
3272 };
3273 struct rpc_message msg = {
3274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3275 .rpc_argp = &args,
3276 .rpc_resp = &res,
3277 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003278
David Quigleyaa9c2662013-05-22 12:50:44 -04003279 bitmask[0] = nfs4_fattr_bitmap[0];
3280 bitmask[1] = nfs4_fattr_bitmap[1];
3281 /*
3282 * Process the label in the upcoming getfattr
3283 */
3284 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3285
Trond Myklebust0e574af2005-10-27 22:12:38 -04003286 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003287 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
3289
3290static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3291 struct nfs_fsinfo *info)
3292{
3293 struct nfs4_exception exception = { };
3294 int err;
3295 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003296 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003297 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003298 switch (err) {
3299 case 0:
3300 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003301 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003302 default:
3303 err = nfs4_handle_exception(server, err, &exception);
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003306out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return err;
3308}
3309
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003310static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3311 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3312{
Trond Myklebustc2190662013-08-26 19:23:04 -04003313 struct rpc_auth_create_args auth_args = {
3314 .pseudoflavor = flavor,
3315 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003316 struct rpc_auth *auth;
3317 int ret;
3318
Trond Myklebustc2190662013-08-26 19:23:04 -04003319 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003320 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003321 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003322 goto out;
3323 }
3324 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003325out:
3326 return ret;
3327}
3328
Chuck Lever9a744ba2013-03-16 15:56:02 -04003329/*
3330 * Retry pseudoroot lookup with various security flavors. We do this when:
3331 *
3332 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3333 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3334 *
3335 * Returns zero on success, or a negative NFS4ERR value, or a
3336 * negative errno value.
3337 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003338static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003339 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003341 /* Per 3530bis 15.33.5 */
3342 static const rpc_authflavor_t flav_array[] = {
3343 RPC_AUTH_GSS_KRB5P,
3344 RPC_AUTH_GSS_KRB5I,
3345 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003346 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003347 RPC_AUTH_NULL,
3348 };
3349 int status = -EPERM;
3350 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003352 if (server->auth_info.flavor_len > 0) {
3353 /* try each flavor specified by user */
3354 for (i = 0; i < server->auth_info.flavor_len; i++) {
3355 status = nfs4_lookup_root_sec(server, fhandle, info,
3356 server->auth_info.flavors[i]);
3357 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3358 continue;
3359 break;
3360 }
3361 } else {
3362 /* no flavors specified by user, try default list */
3363 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3364 status = nfs4_lookup_root_sec(server, fhandle, info,
3365 flav_array[i]);
3366 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3367 continue;
3368 break;
3369 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003370 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003371
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003372 /*
3373 * -EACCESS could mean that the user doesn't have correct permissions
3374 * to access the mount. It could also mean that we tried to mount
3375 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3376 * existing mount programs don't handle -EACCES very well so it should
3377 * be mapped to -EPERM instead.
3378 */
3379 if (status == -EACCES)
3380 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003381 return status;
3382}
3383
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003384/**
3385 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3386 * @server: initialized nfs_server handle
3387 * @fhandle: we fill in the pseudo-fs root file handle
3388 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003389 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003390 *
3391 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003392 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003393int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003394 struct nfs_fsinfo *info,
3395 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003396{
Andre Przywarac7757072015-04-23 17:17:40 +01003397 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003398
Andre Przywarac7757072015-04-23 17:17:40 +01003399 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003400 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003401
3402 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003403 status = server->nfs_client->cl_mvops->find_root_sec(server,
3404 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (status == 0)
3407 status = nfs4_server_capabilities(server, fhandle);
3408 if (status == 0)
3409 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003410
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003411 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Bryan Schumakerbae36242012-05-10 15:07:31 -04003414static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3415 struct nfs_fsinfo *info)
3416{
3417 int error;
3418 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003419 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003420
3421 error = nfs4_server_capabilities(server, mntfh);
3422 if (error < 0) {
3423 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3424 return error;
3425 }
3426
David Quigley14c43f72013-05-22 12:50:43 -04003427 label = nfs4_label_alloc(server, GFP_KERNEL);
3428 if (IS_ERR(label))
3429 return PTR_ERR(label);
3430
David Quigley1775fd32013-05-22 12:50:42 -04003431 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003432 if (error < 0) {
3433 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003434 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003435 }
3436
3437 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3438 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3439 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3440
David Quigley14c43f72013-05-22 12:50:43 -04003441err_free_label:
3442 nfs4_label_free(label);
3443
Bryan Schumakerbae36242012-05-10 15:07:31 -04003444 return error;
3445}
3446
Manoj Naik6b97fd32006-06-09 09:34:29 -04003447/*
3448 * Get locations and (maybe) other attributes of a referral.
3449 * Note that we'll actually follow the referral later when
3450 * we detect fsid mismatch in inode revalidation
3451 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003452static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3453 const struct qstr *name, struct nfs_fattr *fattr,
3454 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003455{
3456 int status = -ENOMEM;
3457 struct page *page = NULL;
3458 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003459
3460 page = alloc_page(GFP_KERNEL);
3461 if (page == NULL)
3462 goto out;
3463 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3464 if (locations == NULL)
3465 goto out;
3466
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003467 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003468 if (status != 0)
3469 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003470
3471 /*
3472 * If the fsid didn't change, this is a migration event, not a
3473 * referral. Cause us to drop into the exception handler, which
3474 * will kick off migration recovery.
3475 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003476 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003477 dprintk("%s: server did not return a different fsid for"
3478 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003479 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003480 goto out;
3481 }
Andy Adamson533eb462011-06-13 18:25:56 -04003482 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3483 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003484
Andy Adamson533eb462011-06-13 18:25:56 -04003485 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003486 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003487 memset(fhandle, 0, sizeof(struct nfs_fh));
3488out:
3489 if (page)
3490 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003491 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003492 return status;
3493}
3494
David Quigley1775fd32013-05-22 12:50:42 -04003495static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3496 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 struct nfs4_getattr_arg args = {
3499 .fh = fhandle,
3500 .bitmask = server->attr_bitmask,
3501 };
3502 struct nfs4_getattr_res res = {
3503 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003504 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 .server = server,
3506 };
3507 struct rpc_message msg = {
3508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3509 .rpc_argp = &args,
3510 .rpc_resp = &res,
3511 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003512
3513 args.bitmask = nfs4_bitmask(server, label);
3514
Trond Myklebust0e574af2005-10-27 22:12:38 -04003515 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003516 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
David Quigley1775fd32013-05-22 12:50:42 -04003519static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3520 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 struct nfs4_exception exception = { };
3523 int err;
3524 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003525 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3526 trace_nfs4_getattr(server, fhandle, fattr, err);
3527 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 &exception);
3529 } while (exception.retry);
3530 return err;
3531}
3532
3533/*
3534 * The file is not closed if it is opened due to the a request to change
3535 * the size of the file. The open call will not be needed once the
3536 * VFS layer lookup-intents are implemented.
3537 *
3538 * Close is called when the inode is destroyed.
3539 * If we haven't opened the file for O_WRONLY, we
3540 * need to in the size_change case to obtain a stateid.
3541 *
3542 * Got race?
3543 * Because OPEN is always done by name in nfsv4, it is
3544 * possible that we opened a different file by the same
3545 * name. We can recognize this race condition, but we
3546 * can't do anything about it besides returning an error.
3547 *
3548 * This will be fixed with VFS changes (lookup-intent).
3549 */
3550static int
3551nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3552 struct iattr *sattr)
3553{
David Howells2b0143b2015-03-17 22:25:59 +00003554 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003555 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003556 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003557 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 int status;
3559
Peng Tao88ac8152014-09-12 11:04:10 +08003560 if (pnfs_ld_layoutret_on_setattr(inode) &&
3561 sattr->ia_valid & ATTR_SIZE &&
3562 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003563 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003564
Trond Myklebust0e574af2005-10-27 22:12:38 -04003565 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Andy Adamson26699402012-05-30 16:12:24 -04003567 /* Deal with open(O_TRUNC) */
3568 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003569 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003570
3571 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003572 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003573 return 0;
3574
Trond Myklebustd5308382005-11-04 15:33:38 -05003575 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003576 if (sattr->ia_valid & ATTR_FILE) {
3577 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003578
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003579 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003580 if (ctx) {
3581 cred = ctx->cred;
3582 state = ctx->state;
3583 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003584 }
3585
David Quigley14c43f72013-05-22 12:50:43 -04003586 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3587 if (IS_ERR(label))
3588 return PTR_ERR(label);
3589
3590 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003591 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003592 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003593 nfs_setsecurity(inode, fattr, label);
3594 }
David Quigley14c43f72013-05-22 12:50:43 -04003595 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return status;
3597}
3598
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003599static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3600 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003601 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003602{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003603 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003604 int status;
3605 struct nfs4_lookup_arg args = {
3606 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003607 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003608 .name = name,
3609 };
3610 struct nfs4_lookup_res res = {
3611 .server = server,
3612 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003613 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003614 .fh = fhandle,
3615 };
3616 struct rpc_message msg = {
3617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3618 .rpc_argp = &args,
3619 .rpc_resp = &res,
3620 };
3621
David Quigleyaa9c2662013-05-22 12:50:44 -04003622 args.bitmask = nfs4_bitmask(server, label);
3623
David Howells2b3de442006-08-22 20:06:09 -04003624 nfs_fattr_init(fattr);
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003627 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 dprintk("NFS reply lookup: %d\n", status);
3629 return status;
3630}
3631
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003632static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003633{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003634 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003635 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003636 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3637 fattr->nlink = 2;
3638}
3639
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003640static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003641 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003642 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003643{
3644 struct nfs4_exception exception = { };
3645 struct rpc_clnt *client = *clnt;
3646 int err;
3647 do {
David Quigley1775fd32013-05-22 12:50:42 -04003648 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003649 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003650 switch (err) {
3651 case -NFS4ERR_BADNAME:
3652 err = -ENOENT;
3653 goto out;
3654 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003655 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003656 if (err == -NFS4ERR_MOVED)
3657 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003658 goto out;
3659 case -NFS4ERR_WRONGSEC:
3660 err = -EPERM;
3661 if (client != *clnt)
3662 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003663 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003664 if (IS_ERR(client))
3665 return PTR_ERR(client);
3666
3667 exception.retry = 1;
3668 break;
3669 default:
3670 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3671 }
3672 } while (exception.retry);
3673
3674out:
3675 if (err == 0)
3676 *clnt = client;
3677 else if (client != *clnt)
3678 rpc_shutdown_client(client);
3679
3680 return err;
3681}
3682
Al Virobeffb8f2016-07-20 16:34:42 -04003683static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003684 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3685 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003687 int status;
3688 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003689
David Quigley1775fd32013-05-22 12:50:42 -04003690 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003691 if (client != NFS_CLIENT(dir)) {
3692 rpc_shutdown_client(client);
3693 nfs_fixup_secinfo_attributes(fattr);
3694 }
3695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003698struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003699nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003700 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3701{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003702 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003703 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003704
David Quigley1775fd32013-05-22 12:50:42 -04003705 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003706 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003707 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003708 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003709}
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3712{
Trond Myklebust76b32992007-08-10 17:45:11 -04003713 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 struct nfs4_accessargs args = {
3715 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003716 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003718 struct nfs4_accessres res = {
3719 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003720 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 struct rpc_message msg = {
3722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3723 .rpc_argp = &args,
3724 .rpc_resp = &res,
3725 .rpc_cred = entry->cred,
3726 };
3727 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003728 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 /*
3731 * Determine which access bits we want to ask for...
3732 */
3733 if (mode & MAY_READ)
3734 args.access |= NFS4_ACCESS_READ;
3735 if (S_ISDIR(inode->i_mode)) {
3736 if (mode & MAY_WRITE)
3737 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3738 if (mode & MAY_EXEC)
3739 args.access |= NFS4_ACCESS_LOOKUP;
3740 } else {
3741 if (mode & MAY_WRITE)
3742 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3743 if (mode & MAY_EXEC)
3744 args.access |= NFS4_ACCESS_EXECUTE;
3745 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003746
3747 res.fattr = nfs_alloc_fattr();
3748 if (res.fattr == NULL)
3749 return -ENOMEM;
3750
Bryan Schumaker7c513052011-03-24 17:12:24 +00003751 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003753 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003754 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003756 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return status;
3758}
3759
3760static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3761{
3762 struct nfs4_exception exception = { };
3763 int err;
3764 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003765 err = _nfs4_proc_access(inode, entry);
3766 trace_nfs4_access(inode, err);
3767 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 &exception);
3769 } while (exception.retry);
3770 return err;
3771}
3772
3773/*
3774 * TODO: For the time being, we don't try to get any attributes
3775 * along with any of the zero-copy operations READ, READDIR,
3776 * READLINK, WRITE.
3777 *
3778 * In the case of the first three, we want to put the GETATTR
3779 * after the read-type operation -- this is because it is hard
3780 * to predict the length of a GETATTR response in v4, and thus
3781 * align the READ data correctly. This means that the GETATTR
3782 * may end up partially falling into the page cache, and we should
3783 * shift it into the 'tail' of the xdr_buf before processing.
3784 * To do this efficiently, we need to know the total length
3785 * of data received, which doesn't seem to be available outside
3786 * of the RPC layer.
3787 *
3788 * In the case of WRITE, we also want to put the GETATTR after
3789 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003790 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 *
3792 * Both of these changes to the XDR layer would in fact be quite
3793 * minor, but I decided to leave them for a subsequent patch.
3794 */
3795static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3796 unsigned int pgbase, unsigned int pglen)
3797{
3798 struct nfs4_readlink args = {
3799 .fh = NFS_FH(inode),
3800 .pgbase = pgbase,
3801 .pglen = pglen,
3802 .pages = &page,
3803 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003804 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 struct rpc_message msg = {
3806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3807 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003808 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 };
3810
Bryan Schumaker7c513052011-03-24 17:12:24 +00003811 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 -07003812}
3813
3814static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3815 unsigned int pgbase, unsigned int pglen)
3816{
3817 struct nfs4_exception exception = { };
3818 int err;
3819 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003820 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3821 trace_nfs4_readlink(inode, err);
3822 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 &exception);
3824 } while (exception.retry);
3825 return err;
3826}
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003829 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831static int
3832nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003833 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
David Quigleyaa9c2662013-05-22 12:50:44 -04003835 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003836 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 int status = 0;
3839
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003840 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3841 if (IS_ERR(ctx))
3842 return PTR_ERR(ctx);
3843
David Quigleyaa9c2662013-05-22 12:50:44 -04003844 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3845
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003846 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003847 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 if (IS_ERR(state)) {
3849 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003853 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003854 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 return status;
3856}
3857
Al Virobeffb8f2016-07-20 16:34:42 -04003858static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
Trond Myklebust16e42952005-10-27 22:12:44 -04003860 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003861 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003863 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003865 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003866 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003867 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3870 .rpc_argp = &args,
3871 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003873 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003874
Bryan Schumaker7c513052011-03-24 17:12:24 +00003875 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003876 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003877 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 return status;
3879}
3880
Al Virobeffb8f2016-07-20 16:34:42 -04003881static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
3883 struct nfs4_exception exception = { };
3884 int err;
3885 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003886 err = _nfs4_proc_remove(dir, name);
3887 trace_nfs4_remove(dir, name, err);
3888 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 &exception);
3890 } while (exception.retry);
3891 return err;
3892}
3893
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003894static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003896 struct nfs_server *server = NFS_SERVER(dir);
3897 struct nfs_removeargs *args = msg->rpc_argp;
3898 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003900 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003902 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003903
3904 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
3906
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003907static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3908{
Al Viro884be172016-04-28 23:56:31 -04003909 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003910 &data->args.seq_args,
3911 &data->res.seq_res,
3912 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003915static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003917 struct nfs_unlinkdata *data = task->tk_calldata;
3918 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003919
Trond Myklebust14516c32010-07-31 14:29:06 -04003920 if (!nfs4_sequence_done(task, &res->seq_res))
3921 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003922 if (nfs4_async_handle_error(task, res->server, NULL,
3923 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003924 return 0;
3925 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003926 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927}
3928
Jeff Laytond3d41522010-09-17 17:31:57 -04003929static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3930{
3931 struct nfs_server *server = NFS_SERVER(dir);
3932 struct nfs_renameargs *arg = msg->rpc_argp;
3933 struct nfs_renameres *res = msg->rpc_resp;
3934
3935 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003936 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003937 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003938}
3939
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003940static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3941{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003942 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3943 &data->args.seq_args,
3944 &data->res.seq_res,
3945 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003946}
3947
3948static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3949 struct inode *new_dir)
3950{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003951 struct nfs_renamedata *data = task->tk_calldata;
3952 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003953
3954 if (!nfs4_sequence_done(task, &res->seq_res))
3955 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003956 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003957 return 0;
3958
3959 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003960 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003961 return 1;
3962}
3963
Al Virobeffb8f2016-07-20 16:34:42 -04003964static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003966 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 struct nfs4_link_arg arg = {
3968 .fh = NFS_FH(inode),
3969 .dir_fh = NFS_FH(dir),
3970 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003971 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003973 struct nfs4_link_res res = {
3974 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003975 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003976 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 struct rpc_message msg = {
3978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3979 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003980 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003982 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Trond Myklebust136f2622010-04-16 16:22:49 -04003984 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003985 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003986 goto out;
3987
David Quigley14c43f72013-05-22 12:50:43 -04003988 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3989 if (IS_ERR(res.label)) {
3990 status = PTR_ERR(res.label);
3991 goto out;
3992 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003993 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003994
Bryan Schumaker7c513052011-03-24 17:12:24 +00003995 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003996 if (!status) {
3997 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003998 status = nfs_post_op_update_inode(inode, res.fattr);
3999 if (!status)
4000 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004001 }
David Quigley14c43f72013-05-22 12:50:43 -04004002
4003
4004 nfs4_label_free(res.label);
4005
Trond Myklebust136f2622010-04-16 16:22:49 -04004006out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004007 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return status;
4009}
4010
Al Virobeffb8f2016-07-20 16:34:42 -04004011static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
4013 struct nfs4_exception exception = { };
4014 int err;
4015 do {
4016 err = nfs4_handle_exception(NFS_SERVER(inode),
4017 _nfs4_proc_link(inode, dir, name),
4018 &exception);
4019 } while (exception.retry);
4020 return err;
4021}
4022
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004023struct nfs4_createdata {
4024 struct rpc_message msg;
4025 struct nfs4_create_arg arg;
4026 struct nfs4_create_res res;
4027 struct nfs_fh fh;
4028 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004029 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004030};
4031
4032static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004033 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004034{
4035 struct nfs4_createdata *data;
4036
4037 data = kzalloc(sizeof(*data), GFP_KERNEL);
4038 if (data != NULL) {
4039 struct nfs_server *server = NFS_SERVER(dir);
4040
David Quigley14c43f72013-05-22 12:50:43 -04004041 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4042 if (IS_ERR(data->label))
4043 goto out_free;
4044
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004045 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4046 data->msg.rpc_argp = &data->arg;
4047 data->msg.rpc_resp = &data->res;
4048 data->arg.dir_fh = NFS_FH(dir);
4049 data->arg.server = server;
4050 data->arg.name = name;
4051 data->arg.attrs = sattr;
4052 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004053 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004054 data->res.server = server;
4055 data->res.fh = &data->fh;
4056 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004057 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004058 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004059 }
4060 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004061out_free:
4062 kfree(data);
4063 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004064}
4065
4066static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4067{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004068 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004069 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004070 if (status == 0) {
4071 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004072 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004073 }
4074 return status;
4075}
4076
4077static void nfs4_free_createdata(struct nfs4_createdata *data)
4078{
David Quigley14c43f72013-05-22 12:50:43 -04004079 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004080 kfree(data);
4081}
4082
Chuck Lever4f390c12006-08-22 20:06:22 -04004083static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004084 struct page *page, unsigned int len, struct iattr *sattr,
4085 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004087 struct nfs4_createdata *data;
4088 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Chuck Lever94a6d752006-08-22 20:06:23 -04004090 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004091 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004092
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004093 status = -ENOMEM;
4094 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4095 if (data == NULL)
4096 goto out;
4097
4098 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4099 data->arg.u.symlink.pages = &page;
4100 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004101 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004103 status = nfs4_do_create(dir, dentry, data);
4104
4105 nfs4_free_createdata(data);
4106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 return status;
4108}
4109
Chuck Lever4f390c12006-08-22 20:06:22 -04004110static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004111 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004114 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004116
4117 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004120 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4121 trace_nfs4_symlink(dir, &dentry->d_name, err);
4122 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 &exception);
4124 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004125
4126 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return err;
4128}
4129
4130static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004131 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004133 struct nfs4_createdata *data;
4134 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004136 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4137 if (data == NULL)
4138 goto out;
4139
David Quigley1775fd32013-05-22 12:50:42 -04004140 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004141 status = nfs4_do_create(dir, dentry, data);
4142
4143 nfs4_free_createdata(data);
4144out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return status;
4146}
4147
4148static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4149 struct iattr *sattr)
4150{
4151 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004152 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004154
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4156
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004157 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004159 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4160 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4161 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 &exception);
4163 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004164 nfs4_label_release_security(label);
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return err;
4167}
4168
4169static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004170 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
David Howells2b0143b2015-03-17 22:25:59 +00004172 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 struct nfs4_readdir_arg args = {
4174 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004175 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 .pgbase = 0,
4177 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004178 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004179 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 };
4181 struct nfs4_readdir_res res;
4182 struct rpc_message msg = {
4183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4184 .rpc_argp = &args,
4185 .rpc_resp = &res,
4186 .rpc_cred = cred,
4187 };
4188 int status;
4189
Al Viro6de14722013-09-16 10:53:17 -04004190 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4191 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004192 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004193 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004195 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 -05004196 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004197 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004198 status += args.pgbase;
4199 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004200
4201 nfs_invalidate_atime(dir);
4202
Harvey Harrison3110ff82008-05-02 13:42:44 -07004203 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 return status;
4205}
4206
4207static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004208 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
4210 struct nfs4_exception exception = { };
4211 int err;
4212 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004213 err = _nfs4_proc_readdir(dentry, cred, cookie,
4214 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004215 trace_nfs4_readdir(d_inode(dentry), err);
4216 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 &exception);
4218 } while (exception.retry);
4219 return err;
4220}
4221
4222static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004223 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004225 struct nfs4_createdata *data;
4226 int mode = sattr->ia_mode;
4227 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004229 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4230 if (data == NULL)
4231 goto out;
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236 data->arg.ftype = NF4BLK;
4237 data->arg.u.device.specdata1 = MAJOR(rdev);
4238 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
4240 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 data->arg.ftype = NF4CHR;
4242 data->arg.u.device.specdata1 = MAJOR(rdev);
4243 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004244 } else if (!S_ISSOCK(mode)) {
4245 status = -EINVAL;
4246 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
David Quigley1775fd32013-05-22 12:50:42 -04004248
David Quigleyaa9c2662013-05-22 12:50:44 -04004249 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004250 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004251out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 nfs4_free_createdata(data);
4253out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return status;
4255}
4256
4257static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4258 struct iattr *sattr, dev_t rdev)
4259{
4260 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004261 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004263
David Quigleyaa9c2662013-05-22 12:50:44 -04004264 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4265
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004266 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004268 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4269 trace_nfs4_mknod(dir, &dentry->d_name, err);
4270 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 &exception);
4272 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004273
4274 nfs4_label_release_security(label);
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return err;
4277}
4278
4279static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4280 struct nfs_fsstat *fsstat)
4281{
4282 struct nfs4_statfs_arg args = {
4283 .fh = fhandle,
4284 .bitmask = server->attr_bitmask,
4285 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004286 struct nfs4_statfs_res res = {
4287 .fsstat = fsstat,
4288 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4291 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004292 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 };
4294
Trond Myklebust0e574af2005-10-27 22:12:38 -04004295 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004296 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
4299static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4300{
4301 struct nfs4_exception exception = { };
4302 int err;
4303 do {
4304 err = nfs4_handle_exception(server,
4305 _nfs4_proc_statfs(server, fhandle, fsstat),
4306 &exception);
4307 } while (exception.retry);
4308 return err;
4309}
4310
4311static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4312 struct nfs_fsinfo *fsinfo)
4313{
4314 struct nfs4_fsinfo_arg args = {
4315 .fh = fhandle,
4316 .bitmask = server->attr_bitmask,
4317 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004318 struct nfs4_fsinfo_res res = {
4319 .fsinfo = fsinfo,
4320 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 struct rpc_message msg = {
4322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4323 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004324 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 };
4326
Bryan Schumaker7c513052011-03-24 17:12:24 +00004327 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328}
4329
4330static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4331{
4332 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004333 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 int err;
4335
4336 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004337 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004338 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004339 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004340 nfs4_set_lease_period(server->nfs_client,
4341 fsinfo->lease_time * HZ,
4342 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004343 break;
4344 }
4345 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 } while (exception.retry);
4347 return err;
4348}
4349
4350static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4351{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004352 int error;
4353
Trond Myklebust0e574af2005-10-27 22:12:38 -04004354 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004355 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004356 if (error == 0) {
4357 /* block layout checks this! */
4358 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004359 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004360 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004361
4362 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363}
4364
4365static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4366 struct nfs_pathconf *pathconf)
4367{
4368 struct nfs4_pathconf_arg args = {
4369 .fh = fhandle,
4370 .bitmask = server->attr_bitmask,
4371 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004372 struct nfs4_pathconf_res res = {
4373 .pathconf = pathconf,
4374 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 struct rpc_message msg = {
4376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4377 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004378 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 };
4380
4381 /* None of the pathconf attributes are mandatory to implement */
4382 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4383 memset(pathconf, 0, sizeof(*pathconf));
4384 return 0;
4385 }
4386
Trond Myklebust0e574af2005-10-27 22:12:38 -04004387 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004388 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389}
4390
4391static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4392 struct nfs_pathconf *pathconf)
4393{
4394 struct nfs4_exception exception = { };
4395 int err;
4396
4397 do {
4398 err = nfs4_handle_exception(server,
4399 _nfs4_proc_pathconf(server, fhandle, pathconf),
4400 &exception);
4401 } while (exception.retry);
4402 return err;
4403}
4404
Trond Myklebust5521abf2013-03-16 20:54:34 -04004405int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004406 const struct nfs_open_context *ctx,
4407 const struct nfs_lock_context *l_ctx,
4408 fmode_t fmode)
4409{
4410 const struct nfs_lockowner *lockowner = NULL;
4411
4412 if (l_ctx != NULL)
4413 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004414 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004415}
4416EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4417
Trond Myklebust5521abf2013-03-16 20:54:34 -04004418static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4419 const struct nfs_open_context *ctx,
4420 const struct nfs_lock_context *l_ctx,
4421 fmode_t fmode)
4422{
4423 nfs4_stateid current_stateid;
4424
Trond Myklebuste1253be2014-03-05 08:44:23 -05004425 /* If the current stateid represents a lost lock, then exit */
4426 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4427 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004428 return nfs4_stateid_match(stateid, &current_stateid);
4429}
4430
4431static bool nfs4_error_stateid_expired(int err)
4432{
4433 switch (err) {
4434 case -NFS4ERR_DELEG_REVOKED:
4435 case -NFS4ERR_ADMIN_REVOKED:
4436 case -NFS4ERR_BAD_STATEID:
4437 case -NFS4ERR_STALE_STATEID:
4438 case -NFS4ERR_OLD_STATEID:
4439 case -NFS4ERR_OPENMODE:
4440 case -NFS4ERR_EXPIRED:
4441 return true;
4442 }
4443 return false;
4444}
4445
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004446void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004447{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004448 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004449}
4450
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004451static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004453 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004455 trace_nfs4_read(hdr, task->tk_status);
4456 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004457 hdr->args.context->state,
4458 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004459 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004460 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004462
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004463 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467}
4468
Trond Myklebust5521abf2013-03-16 20:54:34 -04004469static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004470 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004471{
4472
4473 if (!nfs4_error_stateid_expired(task->tk_status) ||
4474 nfs4_stateid_is_current(&args->stateid,
4475 args->context,
4476 args->lock_context,
4477 FMODE_READ))
4478 return false;
4479 rpc_restart_call_prepare(task);
4480 return true;
4481}
4482
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004483static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004484{
4485
4486 dprintk("--> %s\n", __func__);
4487
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004488 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004489 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004490 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004491 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004492 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4493 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004494}
4495
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004496static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4497 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004499 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004500 if (!hdr->pgio_done_cb)
4501 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004502 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004503 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004506static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4507 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004508{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004509 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4510 &hdr->args.seq_args,
4511 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004512 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004513 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004514 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4515 hdr->args.lock_context,
4516 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004517 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004518 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004519 return -EIO;
4520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521}
4522
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004523static int nfs4_write_done_cb(struct rpc_task *task,
4524 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004526 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004527
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004528 trace_nfs4_write(hdr, task->tk_status);
4529 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004530 hdr->args.context->state,
4531 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004532 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004533 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004535 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004536 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004537 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004538 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540}
4541
Trond Myklebust5521abf2013-03-16 20:54:34 -04004542static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004543 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004544{
4545
4546 if (!nfs4_error_stateid_expired(task->tk_status) ||
4547 nfs4_stateid_is_current(&args->stateid,
4548 args->context,
4549 args->lock_context,
4550 FMODE_WRITE))
4551 return false;
4552 rpc_restart_call_prepare(task);
4553 return true;
4554}
4555
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004556static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004557{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004558 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004559 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004560 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004561 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004562 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4563 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004564}
4565
Trond Myklebust5a37f852012-04-28 14:55:16 -04004566static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004567bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004568{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004569 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004570 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004571 return false;
4572 /* Otherwise, request attributes if and only if we don't hold
4573 * a delegation
4574 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004575 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004576}
Fred Isamana69aef12011-03-03 15:13:47 +00004577
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004578static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4579 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004581 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004582
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004583 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4584 hdr->args.bitmask = NULL;
4585 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004586 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004587 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004588
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004589 if (!hdr->pgio_done_cb)
4590 hdr->pgio_done_cb = nfs4_write_done_cb;
4591 hdr->res.server = server;
4592 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004594 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004595 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
Fred Isaman0b7c0152012-04-20 14:47:39 -04004598static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4599{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004600 nfs4_setup_sequence(NFS_SERVER(data->inode),
4601 &data->args.seq_args,
4602 &data->res.seq_res,
4603 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Fred Isaman0b7c0152012-04-20 14:47:39 -04004606static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004609
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004610 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004611 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4612 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004613 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004614 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
Fred Isaman0b7c0152012-04-20 14:47:39 -04004619static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004620{
4621 if (!nfs4_sequence_done(task, &data->res.seq_res))
4622 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004623 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004624}
4625
Fred Isaman0b7c0152012-04-20 14:47:39 -04004626static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004628 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004629
Fred Isaman0b7c0152012-04-20 14:47:39 -04004630 if (data->commit_done_cb == NULL)
4631 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004632 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004633 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004634 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004637struct nfs4_renewdata {
4638 struct nfs_client *client;
4639 unsigned long timestamp;
4640};
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642/*
4643 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4644 * standalone procedure for queueing an asynchronous RENEW.
4645 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004646static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004647{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004648 struct nfs4_renewdata *data = calldata;
4649 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004650
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004651 if (atomic_read(&clp->cl_count) > 1)
4652 nfs4_schedule_state_renewal(clp);
4653 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004654 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004655}
4656
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004657static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004659 struct nfs4_renewdata *data = calldata;
4660 struct nfs_client *clp = data->client;
4661 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004663 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004664 switch (task->tk_status) {
4665 case 0:
4666 break;
4667 case -NFS4ERR_LEASE_MOVED:
4668 nfs4_schedule_lease_moved_recovery(clp);
4669 break;
4670 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004671 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004672 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4673 return;
4674 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004675 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004676 return;
4677 }
4678 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004680 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004683static const struct rpc_call_ops nfs4_renew_ops = {
4684 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004685 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004686};
4687
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004688static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
4690 struct rpc_message msg = {
4691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4692 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004693 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004695 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004697 if (renew_flags == 0)
4698 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004699 if (!atomic_inc_not_zero(&clp->cl_count))
4700 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004701 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004702 if (data == NULL)
4703 return -ENOMEM;
4704 data->client = clp;
4705 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004706 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004707 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004710static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
4712 struct rpc_message msg = {
4713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4714 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004715 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 };
4717 unsigned long now = jiffies;
4718 int status;
4719
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004720 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (status < 0)
4722 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004723 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 return 0;
4725}
4726
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004727static inline int nfs4_server_supports_acls(struct nfs_server *server)
4728{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004729 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004730}
4731
Trond Myklebust21f498c2012-08-24 10:59:25 -04004732/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4733 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004734 * the stack.
4735 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004736#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004737
Neil Hormane9e3d722011-03-04 19:26:03 -05004738static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004739 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004740{
4741 struct page *newpage, **spages;
4742 int rc = 0;
4743 size_t len;
4744 spages = pages;
4745
4746 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004747 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004748 newpage = alloc_page(GFP_KERNEL);
4749
4750 if (newpage == NULL)
4751 goto unwind;
4752 memcpy(page_address(newpage), buf, len);
4753 buf += len;
4754 buflen -= len;
4755 *pages++ = newpage;
4756 rc++;
4757 } while (buflen != 0);
4758
4759 return rc;
4760
4761unwind:
4762 for(; rc > 0; rc--)
4763 __free_page(spages[rc-1]);
4764 return -ENOMEM;
4765}
4766
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004767struct nfs4_cached_acl {
4768 int cached;
4769 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004770 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004771};
4772
4773static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004774{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004775 struct nfs_inode *nfsi = NFS_I(inode);
4776
4777 spin_lock(&inode->i_lock);
4778 kfree(nfsi->nfs4_acl);
4779 nfsi->nfs4_acl = acl;
4780 spin_unlock(&inode->i_lock);
4781}
4782
4783static void nfs4_zap_acl_attr(struct inode *inode)
4784{
4785 nfs4_set_cached_acl(inode, NULL);
4786}
4787
4788static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4789{
4790 struct nfs_inode *nfsi = NFS_I(inode);
4791 struct nfs4_cached_acl *acl;
4792 int ret = -ENOENT;
4793
4794 spin_lock(&inode->i_lock);
4795 acl = nfsi->nfs4_acl;
4796 if (acl == NULL)
4797 goto out;
4798 if (buf == NULL) /* user is just asking for length */
4799 goto out_len;
4800 if (acl->cached == 0)
4801 goto out;
4802 ret = -ERANGE; /* see getxattr(2) man page */
4803 if (acl->len > buflen)
4804 goto out;
4805 memcpy(buf, acl->data, acl->len);
4806out_len:
4807 ret = acl->len;
4808out:
4809 spin_unlock(&inode->i_lock);
4810 return ret;
4811}
4812
Sachin Prabhu5794d212012-04-17 14:36:40 +01004813static 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 +00004814{
4815 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004816 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004817
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004818 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004819 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004820 if (acl == NULL)
4821 goto out;
4822 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004823 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004824 } else {
4825 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4826 if (acl == NULL)
4827 goto out;
4828 acl->cached = 0;
4829 }
4830 acl->len = acl_len;
4831out:
4832 nfs4_set_cached_acl(inode, acl);
4833}
4834
Andy Adamsonbf118a32011-12-07 11:55:27 -05004835/*
4836 * The getxattr API returns the required buffer length when called with a
4837 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4838 * the required buf. On a NULL buf, we send a page of data to the server
4839 * guessing that the ACL request can be serviced by a page. If so, we cache
4840 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4841 * the cache. If not so, we throw away the page, and cache the required
4842 * length. The next getxattr call will then produce another round trip to
4843 * the server, this time with the input buf of the required size.
4844 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004845static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004846{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004847 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004848 struct nfs_getaclargs args = {
4849 .fh = NFS_FH(inode),
4850 .acl_pages = pages,
4851 .acl_len = buflen,
4852 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004853 struct nfs_getaclres res = {
4854 .acl_len = buflen,
4855 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004856 struct rpc_message msg = {
4857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4858 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004859 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004860 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004861 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4862 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004863
Andy Adamsonbf118a32011-12-07 11:55:27 -05004864 /* As long as we're doing a round trip to the server anyway,
4865 * let's be prepared for a page of acl data. */
4866 if (npages == 0)
4867 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004868 if (npages > ARRAY_SIZE(pages))
4869 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004870
Andy Adamsonbf118a32011-12-07 11:55:27 -05004871 for (i = 0; i < npages; i++) {
4872 pages[i] = alloc_page(GFP_KERNEL);
4873 if (!pages[i])
4874 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004875 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004876
4877 /* for decoding across pages */
4878 res.acl_scratch = alloc_page(GFP_KERNEL);
4879 if (!res.acl_scratch)
4880 goto out_free;
4881
Andy Adamsonbf118a32011-12-07 11:55:27 -05004882 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004883
Peng Taode040be2012-01-10 22:42:47 +08004884 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004885 __func__, buf, buflen, npages, args.acl_len);
4886 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4887 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004888 if (ret)
4889 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004890
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004891 /* Handle the case where the passed-in buffer is too short */
4892 if (res.acl_flags & NFS4_ACL_TRUNC) {
4893 /* Did the user only issue a request for the acl length? */
4894 if (buf == NULL)
4895 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004896 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004897 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004898 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004899 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004900 if (buf) {
4901 if (res.acl_len > buflen) {
4902 ret = -ERANGE;
4903 goto out_free;
4904 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004905 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004906 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004907out_ok:
4908 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004909out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004910 for (i = 0; i < npages; i++)
4911 if (pages[i])
4912 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004913 if (res.acl_scratch)
4914 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004915 return ret;
4916}
4917
Trond Myklebust16b42892006-08-24 12:27:15 -04004918static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4919{
4920 struct nfs4_exception exception = { };
4921 ssize_t ret;
4922 do {
4923 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004924 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004925 if (ret >= 0)
4926 break;
4927 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4928 } while (exception.retry);
4929 return ret;
4930}
4931
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004932static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4933{
4934 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004935 int ret;
4936
4937 if (!nfs4_server_supports_acls(server))
4938 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004939 ret = nfs_revalidate_inode(server, inode);
4940 if (ret < 0)
4941 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004942 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4943 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004944 ret = nfs4_read_cached_acl(inode, buf, buflen);
4945 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004946 /* -ENOENT is returned if there is no ACL or if there is an ACL
4947 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004948 return ret;
4949 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004950}
4951
Trond Myklebust16b42892006-08-24 12:27:15 -04004952static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004953{
4954 struct nfs_server *server = NFS_SERVER(inode);
4955 struct page *pages[NFS4ACL_MAXPAGES];
4956 struct nfs_setaclargs arg = {
4957 .fh = NFS_FH(inode),
4958 .acl_pages = pages,
4959 .acl_len = buflen,
4960 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004961 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004962 struct rpc_message msg = {
4963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4964 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004965 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004966 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004967 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004968 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004969
4970 if (!nfs4_server_supports_acls(server))
4971 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004972 if (npages > ARRAY_SIZE(pages))
4973 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004974 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004975 if (i < 0)
4976 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004977 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004978 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004979
4980 /*
4981 * Free each page after tx, so the only ref left is
4982 * held by the network stack
4983 */
4984 for (; i > 0; i--)
4985 put_page(pages[i-1]);
4986
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004987 /*
4988 * Acl update can result in inode attribute update.
4989 * so mark the attribute cache invalid.
4990 */
4991 spin_lock(&inode->i_lock);
4992 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4993 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004994 nfs_access_zap_cache(inode);
4995 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004996 return ret;
4997}
4998
Trond Myklebust16b42892006-08-24 12:27:15 -04004999static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5000{
5001 struct nfs4_exception exception = { };
5002 int err;
5003 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005004 err = __nfs4_proc_set_acl(inode, buf, buflen);
5005 trace_nfs4_set_acl(inode, err);
5006 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005007 &exception);
5008 } while (exception.retry);
5009 return err;
5010}
5011
David Quigleyaa9c2662013-05-22 12:50:44 -04005012#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5013static int _nfs4_get_security_label(struct inode *inode, void *buf,
5014 size_t buflen)
5015{
5016 struct nfs_server *server = NFS_SERVER(inode);
5017 struct nfs_fattr fattr;
5018 struct nfs4_label label = {0, 0, buflen, buf};
5019
5020 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005021 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005022 .fh = NFS_FH(inode),
5023 .bitmask = bitmask,
5024 };
5025 struct nfs4_getattr_res res = {
5026 .fattr = &fattr,
5027 .label = &label,
5028 .server = server,
5029 };
5030 struct rpc_message msg = {
5031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005032 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005033 .rpc_resp = &res,
5034 };
5035 int ret;
5036
5037 nfs_fattr_init(&fattr);
5038
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005039 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005040 if (ret)
5041 return ret;
5042 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5043 return -ENOENT;
5044 if (buflen < label.len)
5045 return -ERANGE;
5046 return 0;
5047}
5048
5049static int nfs4_get_security_label(struct inode *inode, void *buf,
5050 size_t buflen)
5051{
5052 struct nfs4_exception exception = { };
5053 int err;
5054
5055 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5056 return -EOPNOTSUPP;
5057
5058 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005059 err = _nfs4_get_security_label(inode, buf, buflen);
5060 trace_nfs4_get_security_label(inode, err);
5061 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005062 &exception);
5063 } while (exception.retry);
5064 return err;
5065}
5066
5067static int _nfs4_do_set_security_label(struct inode *inode,
5068 struct nfs4_label *ilabel,
5069 struct nfs_fattr *fattr,
5070 struct nfs4_label *olabel)
5071{
5072
5073 struct iattr sattr = {0};
5074 struct nfs_server *server = NFS_SERVER(inode);
5075 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005076 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005077 .fh = NFS_FH(inode),
5078 .iap = &sattr,
5079 .server = server,
5080 .bitmask = bitmask,
5081 .label = ilabel,
5082 };
5083 struct nfs_setattrres res = {
5084 .fattr = fattr,
5085 .label = olabel,
5086 .server = server,
5087 };
5088 struct rpc_message msg = {
5089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005090 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005091 .rpc_resp = &res,
5092 };
5093 int status;
5094
Jeff Layton12207f62013-11-01 10:49:32 -04005095 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005096
Jeff Layton12207f62013-11-01 10:49:32 -04005097 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005098 if (status)
5099 dprintk("%s failed: %d\n", __func__, status);
5100
5101 return status;
5102}
5103
5104static int nfs4_do_set_security_label(struct inode *inode,
5105 struct nfs4_label *ilabel,
5106 struct nfs_fattr *fattr,
5107 struct nfs4_label *olabel)
5108{
5109 struct nfs4_exception exception = { };
5110 int err;
5111
5112 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005113 err = _nfs4_do_set_security_label(inode, ilabel,
5114 fattr, olabel);
5115 trace_nfs4_set_security_label(inode, err);
5116 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005117 &exception);
5118 } while (exception.retry);
5119 return err;
5120}
5121
5122static int
Al Viro59301222016-05-27 10:19:30 -04005123nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005124{
5125 struct nfs4_label ilabel, *olabel = NULL;
5126 struct nfs_fattr fattr;
5127 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005128 int status;
5129
5130 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5131 return -EOPNOTSUPP;
5132
5133 nfs_fattr_init(&fattr);
5134
5135 ilabel.pi = 0;
5136 ilabel.lfs = 0;
5137 ilabel.label = (char *)buf;
5138 ilabel.len = buflen;
5139
5140 cred = rpc_lookup_cred();
5141 if (IS_ERR(cred))
5142 return PTR_ERR(cred);
5143
5144 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5145 if (IS_ERR(olabel)) {
5146 status = -PTR_ERR(olabel);
5147 goto out;
5148 }
5149
5150 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5151 if (status == 0)
5152 nfs_setsecurity(inode, &fattr, olabel);
5153
5154 nfs4_label_free(olabel);
5155out:
5156 put_rpccred(cred);
5157 return status;
5158}
5159#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5160
5161
Chuck Leverf0920752012-05-21 22:45:41 -04005162static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5163 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005164{
5165 __be32 verf[2];
5166
Chuck Lever2c820d92012-05-21 22:45:33 -04005167 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5168 /* An impossible timestamp guarantees this value
5169 * will never match a generated boot time. */
5170 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005171 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005172 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005173 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005174 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5175 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005176 }
Chuck Levercd937102012-03-02 17:14:31 -05005177 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5178}
5179
Jeff Laytona3192682015-06-09 19:43:59 -04005180static int
5181nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005182{
Jeff Laytona3192682015-06-09 19:43:59 -04005183 size_t len;
5184 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005185
Trond Myklebustceb3a162015-01-03 15:16:04 -05005186 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005187 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005188
Jeff Laytona3192682015-06-09 19:43:59 -04005189 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005190 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005191 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5192 1 +
5193 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5194 1;
5195 rcu_read_unlock();
5196
5197 if (len > NFS4_OPAQUE_LIMIT + 1)
5198 return -EINVAL;
5199
5200 /*
5201 * Since this string is allocated at mount time, and held until the
5202 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5203 * about a memory-reclaim deadlock.
5204 */
5205 str = kmalloc(len, GFP_KERNEL);
5206 if (!str)
5207 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005208
Chuck Levere984a552012-09-14 17:24:21 -04005209 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005210 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005211 clp->cl_ipaddr,
5212 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5213 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005214 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005215
Jeff Laytona3192682015-06-09 19:43:59 -04005216 clp->cl_owner_id = str;
5217 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005218}
5219
Jeff Layton873e3852015-06-09 19:44:00 -04005220static int
5221nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005222{
Jeff Layton873e3852015-06-09 19:44:00 -04005223 size_t len;
5224 char *str;
5225
5226 len = 10 + 10 + 1 + 10 + 1 +
5227 strlen(nfs4_client_id_uniquifier) + 1 +
5228 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5229
5230 if (len > NFS4_OPAQUE_LIMIT + 1)
5231 return -EINVAL;
5232
5233 /*
5234 * Since this string is allocated at mount time, and held until the
5235 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5236 * about a memory-reclaim deadlock.
5237 */
5238 str = kmalloc(len, GFP_KERNEL);
5239 if (!str)
5240 return -ENOMEM;
5241
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005242 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005243 clp->rpc_ops->version, clp->cl_minorversion,
5244 nfs4_client_id_uniquifier,
5245 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005246 clp->cl_owner_id = str;
5247 return 0;
5248}
5249
5250static int
5251nfs4_init_uniform_client_string(struct nfs_client *clp)
5252{
Jeff Layton873e3852015-06-09 19:44:00 -04005253 size_t len;
5254 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005255
5256 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005257 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005258
5259 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005260 return nfs4_init_uniquifier_client_string(clp);
5261
5262 len = 10 + 10 + 1 + 10 + 1 +
5263 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5264
5265 if (len > NFS4_OPAQUE_LIMIT + 1)
5266 return -EINVAL;
5267
5268 /*
5269 * Since this string is allocated at mount time, and held until the
5270 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5271 * about a memory-reclaim deadlock.
5272 */
5273 str = kmalloc(len, GFP_KERNEL);
5274 if (!str)
5275 return -ENOMEM;
5276
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005277 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005278 clp->rpc_ops->version, clp->cl_minorversion,
5279 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005280 clp->cl_owner_id = str;
5281 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005282}
5283
Chuck Lever706cb8d2014-03-12 12:51:47 -04005284/*
5285 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5286 * services. Advertise one based on the address family of the
5287 * clientaddr.
5288 */
5289static unsigned int
5290nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5291{
5292 if (strchr(clp->cl_ipaddr, ':') != NULL)
5293 return scnprintf(buf, len, "tcp6");
5294 else
5295 return scnprintf(buf, len, "tcp");
5296}
5297
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005298static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5299{
5300 struct nfs4_setclientid *sc = calldata;
5301
5302 if (task->tk_status == 0)
5303 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5304}
5305
5306static const struct rpc_call_ops nfs4_setclientid_ops = {
5307 .rpc_call_done = nfs4_setclientid_done,
5308};
5309
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005310/**
5311 * nfs4_proc_setclientid - Negotiate client ID
5312 * @clp: state data structure
5313 * @program: RPC program for NFSv4 callback service
5314 * @port: IP port number for NFS4 callback service
5315 * @cred: RPC credential to use for this call
5316 * @res: where to place the result
5317 *
5318 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5319 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005320int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5321 unsigned short port, struct rpc_cred *cred,
5322 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
5324 nfs4_verifier sc_verifier;
5325 struct nfs4_setclientid setclientid = {
5326 .sc_verifier = &sc_verifier,
5327 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005328 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 };
5330 struct rpc_message msg = {
5331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5332 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005333 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005334 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005336 struct rpc_task *task;
5337 struct rpc_task_setup task_setup_data = {
5338 .rpc_client = clp->cl_rpcclient,
5339 .rpc_message = &msg,
5340 .callback_ops = &nfs4_setclientid_ops,
5341 .callback_data = &setclientid,
5342 .flags = RPC_TASK_TIMEOUT,
5343 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005344 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
Chuck Leverde734832012-07-11 16:30:50 -04005346 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005347 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005348
5349 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5350 status = nfs4_init_uniform_client_string(clp);
5351 else
Jeff Laytona3192682015-06-09 19:43:59 -04005352 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005353
5354 if (status)
5355 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005356
Chuck Leverde734832012-07-11 16:30:50 -04005357 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005358 setclientid.sc_netid_len =
5359 nfs4_init_callback_netid(clp,
5360 setclientid.sc_netid,
5361 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005362 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005363 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 clp->cl_ipaddr, port >> 8, port & 255);
5365
Jeff Layton3a6bb732015-06-09 19:43:57 -04005366 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005367 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005368 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005369 task = rpc_run_task(&task_setup_data);
5370 if (IS_ERR(task)) {
5371 status = PTR_ERR(task);
5372 goto out;
5373 }
5374 status = task->tk_status;
5375 if (setclientid.sc_cred) {
5376 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5377 put_rpccred(setclientid.sc_cred);
5378 }
5379 rpc_put_task(task);
5380out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005381 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005382 dprintk("NFS reply setclientid: %d\n", status);
5383 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005386/**
5387 * nfs4_proc_setclientid_confirm - Confirm client ID
5388 * @clp: state data structure
5389 * @res: result of a previous SETCLIENTID
5390 * @cred: RPC credential to use for this call
5391 *
5392 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5393 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005394int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005395 struct nfs4_setclientid_res *arg,
5396 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 struct rpc_message msg = {
5399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005400 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005401 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 int status;
5404
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005405 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5406 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5407 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005408 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005409 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005410 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 return status;
5412}
5413
Trond Myklebustfe650402006-01-03 09:55:18 +01005414struct nfs4_delegreturndata {
5415 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005416 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005417 struct nfs_fh fh;
5418 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005419 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005420 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005421 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005422 struct inode *inode;
5423 bool roc;
5424 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005425};
5426
Trond Myklebustfe650402006-01-03 09:55:18 +01005427static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5428{
5429 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005430
Trond Myklebust14516c32010-07-31 14:29:06 -04005431 if (!nfs4_sequence_done(task, &data->res.seq_res))
5432 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005433
Trond Myklebustca8acf82013-08-13 10:36:56 -04005434 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005435 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005436 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005437 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005438 case -NFS4ERR_ADMIN_REVOKED:
5439 case -NFS4ERR_DELEG_REVOKED:
5440 case -NFS4ERR_BAD_STATEID:
5441 case -NFS4ERR_OLD_STATEID:
5442 case -NFS4ERR_STALE_STATEID:
5443 case -NFS4ERR_EXPIRED:
5444 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005445 if (data->roc)
5446 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005447 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005448 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005449 if (nfs4_async_handle_error(task, data->res.server,
5450 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005451 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005452 return;
5453 }
5454 }
5455 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005456}
5457
5458static void nfs4_delegreturn_release(void *calldata)
5459{
Peng Tao039b7562014-07-03 13:05:02 +08005460 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005461 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005462
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005463 if (inode) {
5464 if (data->roc)
5465 pnfs_roc_release(inode);
5466 nfs_iput_and_deactive(inode);
5467 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005468 kfree(calldata);
5469}
5470
Andy Adamson938e1012009-04-01 09:22:28 -04005471static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5472{
5473 struct nfs4_delegreturndata *d_data;
5474
5475 d_data = (struct nfs4_delegreturndata *)data;
5476
Peng Tao500d7012015-09-22 11:35:22 +08005477 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5478 return;
5479
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005480 if (d_data->roc)
5481 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005482
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005483 nfs4_setup_sequence(d_data->res.server,
5484 &d_data->args.seq_args,
5485 &d_data->res.seq_res,
5486 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005487}
Andy Adamson938e1012009-04-01 09:22:28 -04005488
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005489static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005490 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005491 .rpc_call_done = nfs4_delegreturn_done,
5492 .rpc_release = nfs4_delegreturn_release,
5493};
5494
Trond Myklebuste6f81072008-01-24 18:14:34 -05005495static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005496{
5497 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005498 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005499 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005500 struct rpc_message msg = {
5501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5502 .rpc_cred = cred,
5503 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005504 struct rpc_task_setup task_setup_data = {
5505 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005506 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005507 .callback_ops = &nfs4_delegreturn_ops,
5508 .flags = RPC_TASK_ASYNC,
5509 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005510 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005511
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005512 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005513 if (data == NULL)
5514 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005515 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005516
5517 nfs4_state_protect(server->nfs_client,
5518 NFS_SP4_MACH_CRED_CLEANUP,
5519 &task_setup_data.rpc_client, &msg);
5520
Trond Myklebustfe650402006-01-03 09:55:18 +01005521 data->args.fhandle = &data->fh;
5522 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005523 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005524 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005525 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005526 data->res.fattr = &data->fattr;
5527 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005528 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005529 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005530 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005531 data->inode = nfs_igrab_and_active(inode);
5532 if (data->inode)
5533 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005534
Trond Myklebustc970aa82007-07-14 15:39:59 -04005535 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005536 msg.rpc_argp = &data->args;
5537 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005538 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005539 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005540 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005541 if (!issync)
5542 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005543 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005544 if (status != 0)
5545 goto out;
5546 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005547 if (status == 0)
5548 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5549 else
5550 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005551out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005552 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005553 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554}
5555
Trond Myklebuste6f81072008-01-24 18:14:34 -05005556int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557{
5558 struct nfs_server *server = NFS_SERVER(inode);
5559 struct nfs4_exception exception = { };
5560 int err;
5561 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005562 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005563 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 switch (err) {
5565 case -NFS4ERR_STALE_STATEID:
5566 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 case 0:
5568 return 0;
5569 }
5570 err = nfs4_handle_exception(server, err, &exception);
5571 } while (exception.retry);
5572 return err;
5573}
5574
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5576{
5577 struct inode *inode = state->inode;
5578 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005579 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005580 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005582 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005584 struct nfs_lockt_res res = {
5585 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 };
5587 struct rpc_message msg = {
5588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5589 .rpc_argp = &arg,
5590 .rpc_resp = &res,
5591 .rpc_cred = state->owner->so_cred,
5592 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 struct nfs4_lock_state *lsp;
5594 int status;
5595
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005596 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005597 status = nfs4_set_lock_state(state, request);
5598 if (status != 0)
5599 goto out;
5600 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005601 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005602 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005603 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005604 switch (status) {
5605 case 0:
5606 request->fl_type = F_UNLCK;
5607 break;
5608 case -NFS4ERR_DENIED:
5609 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005611 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005612 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005613out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 return status;
5615}
5616
5617static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5618{
5619 struct nfs4_exception exception = { };
5620 int err;
5621
5622 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005623 err = _nfs4_proc_getlk(state, cmd, request);
5624 trace_nfs4_get_lock(request, state, cmd, err);
5625 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 &exception);
5627 } while (exception.retry);
5628 return err;
5629}
5630
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005631struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005632 struct nfs_locku_args arg;
5633 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005634 struct nfs4_lock_state *lsp;
5635 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005636 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005637 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005638 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005639};
5640
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005641static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5642 struct nfs_open_context *ctx,
5643 struct nfs4_lock_state *lsp,
5644 struct nfs_seqid *seqid)
5645{
5646 struct nfs4_unlockdata *p;
5647 struct inode *inode = lsp->ls_state->inode;
5648
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005649 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005650 if (p == NULL)
5651 return NULL;
5652 p->arg.fh = NFS_FH(inode);
5653 p->arg.fl = &p->fl;
5654 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005655 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005656 p->lsp = lsp;
5657 atomic_inc(&lsp->ls_count);
5658 /* Ensure we don't close file until we're done freeing locks! */
5659 p->ctx = get_nfs_open_context(ctx);
5660 memcpy(&p->fl, fl, sizeof(p->fl));
5661 p->server = NFS_SERVER(inode);
5662 return p;
5663}
5664
Trond Myklebust06f814a2006-01-03 09:55:07 +01005665static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005666{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005667 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005668 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005669 nfs4_put_lock_state(calldata->lsp);
5670 put_nfs_open_context(calldata->ctx);
5671 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005672}
5673
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005674static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005675{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005676 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005677
Trond Myklebust14516c32010-07-31 14:29:06 -04005678 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5679 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005680 switch (task->tk_status) {
5681 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005682 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005683 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005684 if (nfs4_update_lock_stateid(calldata->lsp,
5685 &calldata->res.stateid))
5686 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005687 case -NFS4ERR_BAD_STATEID:
5688 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005689 case -NFS4ERR_STALE_STATEID:
5690 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005691 if (!nfs4_stateid_match(&calldata->arg.stateid,
5692 &calldata->lsp->ls_stateid))
5693 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005694 break;
5695 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005696 if (nfs4_async_handle_error(task, calldata->server,
5697 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005698 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005699 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005700 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005701}
5702
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005703static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005704{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005705 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005707 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005708 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005709 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005710 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005711 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005712 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005713 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005714 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005715 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005716 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005717 &calldata->res.seq_res,
5718 task) != 0)
5719 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005720 return;
5721out_no_action:
5722 task->tk_action = NULL;
5723out_wait:
5724 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725}
5726
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005727static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005728 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005729 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005730 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005731};
5732
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005733static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5734 struct nfs_open_context *ctx,
5735 struct nfs4_lock_state *lsp,
5736 struct nfs_seqid *seqid)
5737{
5738 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005739 struct rpc_message msg = {
5740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5741 .rpc_cred = ctx->cred,
5742 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005743 struct rpc_task_setup task_setup_data = {
5744 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005745 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005746 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005747 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005748 .flags = RPC_TASK_ASYNC,
5749 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005750
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005751 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5752 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5753
Frank Filz137d6ac2007-07-09 15:32:29 -07005754 /* Ensure this is an unlock - when canceling a lock, the
5755 * canceled lock is passed in, and it won't be an unlock.
5756 */
5757 fl->fl_type = F_UNLCK;
5758
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5760 if (data == NULL) {
5761 nfs_free_seqid(seqid);
5762 return ERR_PTR(-ENOMEM);
5763 }
5764
Chuck Levera9c92d62013-08-09 12:48:18 -04005765 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005766 msg.rpc_argp = &data->arg;
5767 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005768 task_setup_data.callback_data = data;
5769 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005770}
5771
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5773{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005774 struct inode *inode = state->inode;
5775 struct nfs4_state_owner *sp = state->owner;
5776 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005777 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005778 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005779 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005780 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005781 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005782 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
Trond Myklebust9b073572006-06-29 16:38:34 -04005784 status = nfs4_set_lock_state(state, request);
5785 /* Unlock _before_ we do the RPC call */
5786 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005787 /* Exclude nfs_delegation_claim_locks() */
5788 mutex_lock(&sp->so_delegreturn_mutex);
5789 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005790 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005791 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005792 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005793 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005794 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005795 }
5796 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005797 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005798 if (status != 0)
5799 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005800 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005801 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005802 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5803 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005804 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5805 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005806 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005807 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005808 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005809 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005810 status = PTR_ERR(task);
5811 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005812 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005813 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005814 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005815out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005816 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005817 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005818 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005821struct nfs4_lockdata {
5822 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005823 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005824 struct nfs4_lock_state *lsp;
5825 struct nfs_open_context *ctx;
5826 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005827 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005828 int rpc_status;
5829 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005830 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005831};
5832
5833static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005834 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5835 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005836{
5837 struct nfs4_lockdata *p;
5838 struct inode *inode = lsp->ls_state->inode;
5839 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005840 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005841
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005842 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005843 if (p == NULL)
5844 return NULL;
5845
5846 p->arg.fh = NFS_FH(inode);
5847 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005848 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005849 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005850 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005851 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5852 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005853 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005854 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005855 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005856 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005857 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005858 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005859 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005860 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005861 atomic_inc(&lsp->ls_count);
5862 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005863 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005864 memcpy(&p->fl, fl, sizeof(p->fl));
5865 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005866out_free_seqid:
5867 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005868out_free:
5869 kfree(p);
5870 return NULL;
5871}
5872
5873static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5874{
5875 struct nfs4_lockdata *data = calldata;
5876 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
Harvey Harrison3110ff82008-05-02 13:42:44 -07005878 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005879 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005880 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005882 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005883 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005884 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005885 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005886 nfs4_stateid_copy(&data->arg.open_stateid,
5887 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005888 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005889 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005890 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005891 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005892 nfs4_stateid_copy(&data->arg.lock_stateid,
5893 &data->lsp->ls_stateid);
5894 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005895 if (!nfs4_valid_open_stateid(state)) {
5896 data->rpc_status = -EBADF;
5897 task->tk_action = NULL;
5898 goto out_release_open_seqid;
5899 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005900 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005901 if (nfs4_setup_sequence(data->server,
5902 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005903 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005904 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005905 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005906out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005907 nfs_release_seqid(data->arg.open_seqid);
5908out_release_lock_seqid:
5909 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005910out_wait:
5911 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005912 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005913}
5914
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005915static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5916{
5917 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005918 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005919
Harvey Harrison3110ff82008-05-02 13:42:44 -07005920 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005921
Trond Myklebust14516c32010-07-31 14:29:06 -04005922 if (!nfs4_sequence_done(task, &data->res.seq_res))
5923 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005924
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005925 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005926 switch (task->tk_status) {
5927 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005928 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005929 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005930 if (data->arg.new_lock) {
5931 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04005932 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005933 rpc_restart_call_prepare(task);
5934 break;
5935 }
5936 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005937 if (data->arg.new_lock_owner != 0) {
5938 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5939 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5940 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5941 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5942 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005943 break;
5944 case -NFS4ERR_BAD_STATEID:
5945 case -NFS4ERR_OLD_STATEID:
5946 case -NFS4ERR_STALE_STATEID:
5947 case -NFS4ERR_EXPIRED:
5948 if (data->arg.new_lock_owner != 0) {
5949 if (!nfs4_stateid_match(&data->arg.open_stateid,
5950 &lsp->ls_state->open_stateid))
5951 rpc_restart_call_prepare(task);
5952 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5953 &lsp->ls_stateid))
5954 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005955 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005956 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005957}
5958
5959static void nfs4_lock_release(void *calldata)
5960{
5961 struct nfs4_lockdata *data = calldata;
5962
Harvey Harrison3110ff82008-05-02 13:42:44 -07005963 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005964 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005965 if (data->cancelled != 0) {
5966 struct rpc_task *task;
5967 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5968 data->arg.lock_seqid);
5969 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005970 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005971 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005972 } else
5973 nfs_free_seqid(data->arg.lock_seqid);
5974 nfs4_put_lock_state(data->lsp);
5975 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005976 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005978 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979}
5980
5981static const struct rpc_call_ops nfs4_lock_ops = {
5982 .rpc_call_prepare = nfs4_lock_prepare,
5983 .rpc_call_done = nfs4_lock_done,
5984 .rpc_release = nfs4_lock_release,
5985};
5986
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005987static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5988{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005989 switch (error) {
5990 case -NFS4ERR_ADMIN_REVOKED:
5991 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005992 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005993 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005994 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005995 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005996 break;
5997 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005998 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005999 case -NFS4ERR_EXPIRED:
6000 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006001 };
6002}
6003
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006004static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006005{
6006 struct nfs4_lockdata *data;
6007 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006008 struct rpc_message msg = {
6009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6010 .rpc_cred = state->owner->so_cred,
6011 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006012 struct rpc_task_setup task_setup_data = {
6013 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006014 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006015 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006016 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006017 .flags = RPC_TASK_ASYNC,
6018 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 int ret;
6020
Harvey Harrison3110ff82008-05-02 13:42:44 -07006021 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006022 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006023 fl->fl_u.nfs4_fl.owner,
6024 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 if (data == NULL)
6026 return -ENOMEM;
6027 if (IS_SETLKW(cmd))
6028 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006029 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006030 msg.rpc_argp = &data->arg;
6031 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006032 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006033 if (recovery_type > NFS_LOCK_NEW) {
6034 if (recovery_type == NFS_LOCK_RECLAIM)
6035 data->arg.reclaim = NFS_LOCK_RECLAIM;
6036 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006037 } else
6038 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006039 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006040 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006041 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006042 ret = nfs4_wait_for_completion_rpc_task(task);
6043 if (ret == 0) {
6044 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006045 if (ret)
6046 nfs4_handle_setlk_error(data->server, data->lsp,
6047 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006048 } else
6049 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006050 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006051 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006052 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054}
6055
6056static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6057{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006058 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006059 struct nfs4_exception exception = {
6060 .inode = state->inode,
6061 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006062 int err;
6063
6064 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006065 /* Cache the lock if possible... */
6066 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6067 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006068 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006069 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006070 break;
6071 nfs4_handle_exception(server, err, &exception);
6072 } while (exception.retry);
6073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074}
6075
6076static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6077{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006078 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006079 struct nfs4_exception exception = {
6080 .inode = state->inode,
6081 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006082 int err;
6083
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006084 err = nfs4_set_lock_state(state, request);
6085 if (err != 0)
6086 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006087 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006088 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6089 return 0;
6090 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006091 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006092 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6093 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006094 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006095 switch (err) {
6096 default:
6097 goto out;
6098 case -NFS4ERR_GRACE:
6099 case -NFS4ERR_DELAY:
6100 nfs4_handle_exception(server, err, &exception);
6101 err = 0;
6102 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006103 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006104out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106}
6107
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006108#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006109/**
6110 * nfs41_check_expired_locks - possibly free a lock stateid
6111 *
6112 * @state: NFSv4 state for an inode
6113 *
6114 * Returns NFS_OK if recovery for this stateid is now finished.
6115 * Otherwise a negative NFS4ERR value is returned.
6116 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006117static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006118{
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04006119 int status, ret = NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006120 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006121 struct nfs_server *server = NFS_SERVER(state->inode);
6122
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006123 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006124 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006125 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6126
Trond Myklebust4586f6e2016-09-22 13:38:57 -04006127 status = nfs41_test_and_free_expired_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006128 &lsp->ls_stateid,
6129 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006130 trace_nfs4_test_lock_stateid(state, lsp, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04006131 if (status == -NFS4ERR_EXPIRED ||
6132 status == -NFS4ERR_BAD_STATEID)
Trond Myklebust795a88c2012-09-10 13:26:49 -04006133 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04006134 else if (status != NFS_OK) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006135 ret = status;
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04006136 break;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006137 }
6138 }
6139 };
6140
6141 return ret;
6142}
6143
6144static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6145{
6146 int status = NFS_OK;
6147
6148 if (test_bit(LK_STATE_IN_USE, &state->flags))
6149 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006150 if (status != NFS_OK)
6151 status = nfs4_lock_expired(state, request);
6152 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006153}
6154#endif
6155
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6157{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006158 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006159 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006160 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006161 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Trond Myklebust01c3b862006-06-29 16:38:39 -04006163 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006164 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006165 if (status < 0)
6166 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006167 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006168 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006169 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006170 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006171 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006172 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006173 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006174 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006175 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006176 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006177 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006178 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006179 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006180 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006181out:
6182 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 return status;
6184}
6185
6186static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6187{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006188 struct nfs4_exception exception = {
6189 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006190 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006191 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 int err;
6193
6194 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006195 err = _nfs4_proc_setlk(state, cmd, request);
6196 if (err == -NFS4ERR_DENIED)
6197 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006199 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 } while (exception.retry);
6201 return err;
6202}
6203
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006204#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6205#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6206
6207static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006208nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6209 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006210{
6211 int status = -ERESTARTSYS;
6212 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6213
6214 while(!signalled()) {
6215 status = nfs4_proc_setlk(state, cmd, request);
6216 if ((status != -EAGAIN) || IS_SETLK(cmd))
6217 break;
6218 freezable_schedule_timeout_interruptible(timeout);
6219 timeout *= 2;
6220 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6221 status = -ERESTARTSYS;
6222 }
6223 return status;
6224}
6225
Jeff Laytona1d617d82016-09-17 18:17:39 -04006226#ifdef CONFIG_NFS_V4_1
6227struct nfs4_lock_waiter {
6228 struct task_struct *task;
6229 struct inode *inode;
6230 struct nfs_lowner *owner;
6231 bool notified;
6232};
6233
6234static int
6235nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6236{
6237 int ret;
6238 struct cb_notify_lock_args *cbnl = key;
6239 struct nfs4_lock_waiter *waiter = wait->private;
6240 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6241 *wowner = waiter->owner;
6242
6243 /* Only wake if the callback was for the same owner */
6244 if (lowner->clientid != wowner->clientid ||
6245 lowner->id != wowner->id ||
6246 lowner->s_dev != wowner->s_dev)
6247 return 0;
6248
6249 /* Make sure it's for the right inode */
6250 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6251 return 0;
6252
6253 waiter->notified = true;
6254
6255 /* override "private" so we can use default_wake_function */
6256 wait->private = waiter->task;
6257 ret = autoremove_wake_function(wait, mode, flags, key);
6258 wait->private = waiter;
6259 return ret;
6260}
6261
6262static int
6263nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6264{
6265 int status = -ERESTARTSYS;
6266 unsigned long flags;
6267 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6268 struct nfs_server *server = NFS_SERVER(state->inode);
6269 struct nfs_client *clp = server->nfs_client;
6270 wait_queue_head_t *q = &clp->cl_lock_waitq;
6271 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6272 .id = lsp->ls_seqid.owner_id,
6273 .s_dev = server->s_dev };
6274 struct nfs4_lock_waiter waiter = { .task = current,
6275 .inode = state->inode,
6276 .owner = &owner,
6277 .notified = false };
6278 wait_queue_t wait;
6279
6280 /* Don't bother with waitqueue if we don't expect a callback */
6281 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6282 return nfs4_retry_setlk_simple(state, cmd, request);
6283
6284 init_wait(&wait);
6285 wait.private = &waiter;
6286 wait.func = nfs4_wake_lock_waiter;
6287 add_wait_queue(q, &wait);
6288
6289 while(!signalled()) {
6290 status = nfs4_proc_setlk(state, cmd, request);
6291 if ((status != -EAGAIN) || IS_SETLK(cmd))
6292 break;
6293
6294 status = -ERESTARTSYS;
6295 spin_lock_irqsave(&q->lock, flags);
6296 if (waiter.notified) {
6297 spin_unlock_irqrestore(&q->lock, flags);
6298 continue;
6299 }
6300 set_current_state(TASK_INTERRUPTIBLE);
6301 spin_unlock_irqrestore(&q->lock, flags);
6302
6303 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6304 }
6305
6306 finish_wait(q, &wait);
6307 return status;
6308}
6309#else /* !CONFIG_NFS_V4_1 */
6310static inline int
6311nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6312{
6313 return nfs4_retry_setlk_simple(state, cmd, request);
6314}
6315#endif
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317static int
6318nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6319{
6320 struct nfs_open_context *ctx;
6321 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 int status;
6323
6324 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006325 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 state = ctx->state;
6327
6328 if (request->fl_start < 0 || request->fl_end < 0)
6329 return -EINVAL;
6330
Trond Myklebustd9531262009-07-21 19:22:38 -04006331 if (IS_GETLK(cmd)) {
6332 if (state != NULL)
6333 return nfs4_proc_getlk(state, F_GETLK, request);
6334 return 0;
6335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
6337 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6338 return -EINVAL;
6339
Trond Myklebustd9531262009-07-21 19:22:38 -04006340 if (request->fl_type == F_UNLCK) {
6341 if (state != NULL)
6342 return nfs4_proc_unlck(state, cmd, request);
6343 return 0;
6344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
Trond Myklebustd9531262009-07-21 19:22:38 -04006346 if (state == NULL)
6347 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006348
6349 if ((request->fl_flags & FL_POSIX) &&
6350 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6351 return -ENOLCK;
6352
Trond Myklebust55725512012-04-18 12:48:35 -04006353 /*
6354 * Don't rely on the VFS having checked the file open mode,
6355 * since it won't do this for flock() locks.
6356 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006357 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006358 case F_RDLCK:
6359 if (!(filp->f_mode & FMODE_READ))
6360 return -EBADF;
6361 break;
6362 case F_WRLCK:
6363 if (!(filp->f_mode & FMODE_WRITE))
6364 return -EBADF;
6365 }
6366
Jeff Layton1ea67db2016-09-17 18:17:37 -04006367 status = nfs4_set_lock_state(state, request);
6368 if (status != 0)
6369 return status;
6370
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006371 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372}
6373
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006374int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006375{
6376 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006377 int err;
6378
6379 err = nfs4_set_lock_state(state, fl);
6380 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006381 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006382 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006383 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006384}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006385
Trond Myklebustcf470c32012-03-07 13:49:12 -05006386struct nfs_release_lockowner_data {
6387 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006388 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006389 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006390 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006391 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006392};
6393
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006394static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6395{
6396 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006397 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006398 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6399 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006400 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006401 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006402}
6403
6404static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6405{
6406 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006407 struct nfs_server *server = data->server;
6408
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006409 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006410
6411 switch (task->tk_status) {
6412 case 0:
6413 renew_lease(server, data->timestamp);
6414 break;
6415 case -NFS4ERR_STALE_CLIENTID:
6416 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006417 nfs4_schedule_lease_recovery(server->nfs_client);
6418 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006419 case -NFS4ERR_LEASE_MOVED:
6420 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006421 if (nfs4_async_handle_error(task, server,
6422 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006423 rpc_restart_call_prepare(task);
6424 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006425}
6426
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006427static void nfs4_release_lockowner_release(void *calldata)
6428{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006429 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006430 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006431 kfree(calldata);
6432}
6433
Trond Myklebust17280172012-03-11 13:11:00 -04006434static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006435 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6436 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006437 .rpc_release = nfs4_release_lockowner_release,
6438};
6439
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006440static void
6441nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006442{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006443 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006444 struct rpc_message msg = {
6445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6446 };
6447
6448 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006449 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006450
Trond Myklebustcf470c32012-03-07 13:49:12 -05006451 data = kmalloc(sizeof(*data), GFP_NOFS);
6452 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006453 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006454 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006455 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006456 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6457 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6458 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006459
Trond Myklebustcf470c32012-03-07 13:49:12 -05006460 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006461 msg.rpc_resp = &data->res;
6462 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006463 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006464}
6465
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006466#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6467
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006468static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006469 struct dentry *unused, struct inode *inode,
6470 const char *key, const void *buf,
6471 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006472{
Al Viro59301222016-05-27 10:19:30 -04006473 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006474}
6475
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006476static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006477 struct dentry *unused, struct inode *inode,
6478 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006479{
Al Virob2968212016-04-10 20:48:24 -04006480 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006481}
6482
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006483static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006484{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006485 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006486}
6487
David Quigleyc9bccef2013-05-22 12:50:45 -04006488#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006489
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006490static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006491 struct dentry *unused, struct inode *inode,
6492 const char *key, const void *buf,
6493 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006494{
6495 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006496 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006497
6498 return -EOPNOTSUPP;
6499}
6500
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006501static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006502 struct dentry *unused, struct inode *inode,
6503 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006504{
6505 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006506 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006507 return -EOPNOTSUPP;
6508}
6509
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006510static ssize_t
6511nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006512{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006513 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006514
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006515 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6516 len = security_inode_listsecurity(inode, list, list_len);
6517 if (list_len && len > list_len)
6518 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006519 }
6520 return len;
6521}
6522
6523static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6524 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006525 .get = nfs4_xattr_get_nfs4_label,
6526 .set = nfs4_xattr_set_nfs4_label,
6527};
David Quigleyc9bccef2013-05-22 12:50:45 -04006528
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006529#else
6530
6531static ssize_t
6532nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6533{
6534 return 0;
6535}
6536
6537#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006538
Andy Adamson533eb462011-06-13 18:25:56 -04006539/*
6540 * nfs_fhget will use either the mounted_on_fileid or the fileid
6541 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006542static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6543{
Andy Adamson533eb462011-06-13 18:25:56 -04006544 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6545 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6546 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006547 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006548 return;
6549
6550 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006551 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006552 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6553 fattr->nlink = 2;
6554}
6555
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006556static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6557 const struct qstr *name,
6558 struct nfs4_fs_locations *fs_locations,
6559 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006560{
6561 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006562 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006563 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006564 };
6565 struct nfs4_fs_locations_arg args = {
6566 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006567 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006568 .page = page,
6569 .bitmask = bitmask,
6570 };
Benny Halevy22958462009-04-01 09:22:02 -04006571 struct nfs4_fs_locations_res res = {
6572 .fs_locations = fs_locations,
6573 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6576 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006577 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006578 };
6579 int status;
6580
Harvey Harrison3110ff82008-05-02 13:42:44 -07006581 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006582
6583 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6584 * is not supported */
6585 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6586 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6587 else
6588 bitmask[0] |= FATTR4_WORD0_FILEID;
6589
Trond Myklebustc228fd32007-01-13 02:28:11 -05006590 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006591 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006592 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006593 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006594 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006595 return status;
6596}
6597
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006598int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6599 const struct qstr *name,
6600 struct nfs4_fs_locations *fs_locations,
6601 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006602{
6603 struct nfs4_exception exception = { };
6604 int err;
6605 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006606 err = _nfs4_proc_fs_locations(client, dir, name,
6607 fs_locations, page);
6608 trace_nfs4_get_fs_locations(dir, name, err);
6609 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006610 &exception);
6611 } while (exception.retry);
6612 return err;
6613}
6614
Chuck Leverb03d7352013-10-17 14:12:50 -04006615/*
6616 * This operation also signals the server that this client is
6617 * performing migration recovery. The server can stop returning
6618 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6619 * appended to this compound to identify the client ID which is
6620 * performing recovery.
6621 */
6622static int _nfs40_proc_get_locations(struct inode *inode,
6623 struct nfs4_fs_locations *locations,
6624 struct page *page, struct rpc_cred *cred)
6625{
6626 struct nfs_server *server = NFS_SERVER(inode);
6627 struct rpc_clnt *clnt = server->client;
6628 u32 bitmask[2] = {
6629 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6630 };
6631 struct nfs4_fs_locations_arg args = {
6632 .clientid = server->nfs_client->cl_clientid,
6633 .fh = NFS_FH(inode),
6634 .page = page,
6635 .bitmask = bitmask,
6636 .migration = 1, /* skip LOOKUP */
6637 .renew = 1, /* append RENEW */
6638 };
6639 struct nfs4_fs_locations_res res = {
6640 .fs_locations = locations,
6641 .migration = 1,
6642 .renew = 1,
6643 };
6644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6646 .rpc_argp = &args,
6647 .rpc_resp = &res,
6648 .rpc_cred = cred,
6649 };
6650 unsigned long now = jiffies;
6651 int status;
6652
6653 nfs_fattr_init(&locations->fattr);
6654 locations->server = server;
6655 locations->nlocations = 0;
6656
6657 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6658 nfs4_set_sequence_privileged(&args.seq_args);
6659 status = nfs4_call_sync_sequence(clnt, server, &msg,
6660 &args.seq_args, &res.seq_res);
6661 if (status)
6662 return status;
6663
6664 renew_lease(server, now);
6665 return 0;
6666}
6667
6668#ifdef CONFIG_NFS_V4_1
6669
6670/*
6671 * This operation also signals the server that this client is
6672 * performing migration recovery. The server can stop asserting
6673 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6674 * performing this operation is identified in the SEQUENCE
6675 * operation in this compound.
6676 *
6677 * When the client supports GETATTR(fs_locations_info), it can
6678 * be plumbed in here.
6679 */
6680static int _nfs41_proc_get_locations(struct inode *inode,
6681 struct nfs4_fs_locations *locations,
6682 struct page *page, struct rpc_cred *cred)
6683{
6684 struct nfs_server *server = NFS_SERVER(inode);
6685 struct rpc_clnt *clnt = server->client;
6686 u32 bitmask[2] = {
6687 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6688 };
6689 struct nfs4_fs_locations_arg args = {
6690 .fh = NFS_FH(inode),
6691 .page = page,
6692 .bitmask = bitmask,
6693 .migration = 1, /* skip LOOKUP */
6694 };
6695 struct nfs4_fs_locations_res res = {
6696 .fs_locations = locations,
6697 .migration = 1,
6698 };
6699 struct rpc_message msg = {
6700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6701 .rpc_argp = &args,
6702 .rpc_resp = &res,
6703 .rpc_cred = cred,
6704 };
6705 int status;
6706
6707 nfs_fattr_init(&locations->fattr);
6708 locations->server = server;
6709 locations->nlocations = 0;
6710
6711 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6712 nfs4_set_sequence_privileged(&args.seq_args);
6713 status = nfs4_call_sync_sequence(clnt, server, &msg,
6714 &args.seq_args, &res.seq_res);
6715 if (status == NFS4_OK &&
6716 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6717 status = -NFS4ERR_LEASE_MOVED;
6718 return status;
6719}
6720
6721#endif /* CONFIG_NFS_V4_1 */
6722
6723/**
6724 * nfs4_proc_get_locations - discover locations for a migrated FSID
6725 * @inode: inode on FSID that is migrating
6726 * @locations: result of query
6727 * @page: buffer
6728 * @cred: credential to use for this operation
6729 *
6730 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6731 * operation failed, or a negative errno if a local error occurred.
6732 *
6733 * On success, "locations" is filled in, but if the server has
6734 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6735 * asserted.
6736 *
6737 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6738 * from this client that require migration recovery.
6739 */
6740int nfs4_proc_get_locations(struct inode *inode,
6741 struct nfs4_fs_locations *locations,
6742 struct page *page, struct rpc_cred *cred)
6743{
6744 struct nfs_server *server = NFS_SERVER(inode);
6745 struct nfs_client *clp = server->nfs_client;
6746 const struct nfs4_mig_recovery_ops *ops =
6747 clp->cl_mvops->mig_recovery_ops;
6748 struct nfs4_exception exception = { };
6749 int status;
6750
6751 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6752 (unsigned long long)server->fsid.major,
6753 (unsigned long long)server->fsid.minor,
6754 clp->cl_hostname);
6755 nfs_display_fhandle(NFS_FH(inode), __func__);
6756
6757 do {
6758 status = ops->get_locations(inode, locations, page, cred);
6759 if (status != -NFS4ERR_DELAY)
6760 break;
6761 nfs4_handle_exception(server, status, &exception);
6762 } while (exception.retry);
6763 return status;
6764}
6765
Chuck Lever44c99932013-10-17 14:13:30 -04006766/*
6767 * This operation also signals the server that this client is
6768 * performing "lease moved" recovery. The server can stop
6769 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6770 * is appended to this compound to identify the client ID which is
6771 * performing recovery.
6772 */
6773static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6774{
6775 struct nfs_server *server = NFS_SERVER(inode);
6776 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6777 struct rpc_clnt *clnt = server->client;
6778 struct nfs4_fsid_present_arg args = {
6779 .fh = NFS_FH(inode),
6780 .clientid = clp->cl_clientid,
6781 .renew = 1, /* append RENEW */
6782 };
6783 struct nfs4_fsid_present_res res = {
6784 .renew = 1,
6785 };
6786 struct rpc_message msg = {
6787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6788 .rpc_argp = &args,
6789 .rpc_resp = &res,
6790 .rpc_cred = cred,
6791 };
6792 unsigned long now = jiffies;
6793 int status;
6794
6795 res.fh = nfs_alloc_fhandle();
6796 if (res.fh == NULL)
6797 return -ENOMEM;
6798
6799 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6800 nfs4_set_sequence_privileged(&args.seq_args);
6801 status = nfs4_call_sync_sequence(clnt, server, &msg,
6802 &args.seq_args, &res.seq_res);
6803 nfs_free_fhandle(res.fh);
6804 if (status)
6805 return status;
6806
6807 do_renew_lease(clp, now);
6808 return 0;
6809}
6810
6811#ifdef CONFIG_NFS_V4_1
6812
6813/*
6814 * This operation also signals the server that this client is
6815 * performing "lease moved" recovery. The server can stop asserting
6816 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6817 * this operation is identified in the SEQUENCE operation in this
6818 * compound.
6819 */
6820static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6821{
6822 struct nfs_server *server = NFS_SERVER(inode);
6823 struct rpc_clnt *clnt = server->client;
6824 struct nfs4_fsid_present_arg args = {
6825 .fh = NFS_FH(inode),
6826 };
6827 struct nfs4_fsid_present_res res = {
6828 };
6829 struct rpc_message msg = {
6830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6831 .rpc_argp = &args,
6832 .rpc_resp = &res,
6833 .rpc_cred = cred,
6834 };
6835 int status;
6836
6837 res.fh = nfs_alloc_fhandle();
6838 if (res.fh == NULL)
6839 return -ENOMEM;
6840
6841 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6842 nfs4_set_sequence_privileged(&args.seq_args);
6843 status = nfs4_call_sync_sequence(clnt, server, &msg,
6844 &args.seq_args, &res.seq_res);
6845 nfs_free_fhandle(res.fh);
6846 if (status == NFS4_OK &&
6847 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6848 status = -NFS4ERR_LEASE_MOVED;
6849 return status;
6850}
6851
6852#endif /* CONFIG_NFS_V4_1 */
6853
6854/**
6855 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6856 * @inode: inode on FSID to check
6857 * @cred: credential to use for this operation
6858 *
6859 * Server indicates whether the FSID is present, moved, or not
6860 * recognized. This operation is necessary to clear a LEASE_MOVED
6861 * condition for this client ID.
6862 *
6863 * Returns NFS4_OK if the FSID is present on this server,
6864 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6865 * NFS4ERR code if some error occurred on the server, or a
6866 * negative errno if a local failure occurred.
6867 */
6868int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6869{
6870 struct nfs_server *server = NFS_SERVER(inode);
6871 struct nfs_client *clp = server->nfs_client;
6872 const struct nfs4_mig_recovery_ops *ops =
6873 clp->cl_mvops->mig_recovery_ops;
6874 struct nfs4_exception exception = { };
6875 int status;
6876
6877 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6878 (unsigned long long)server->fsid.major,
6879 (unsigned long long)server->fsid.minor,
6880 clp->cl_hostname);
6881 nfs_display_fhandle(NFS_FH(inode), __func__);
6882
6883 do {
6884 status = ops->fsid_present(inode, cred);
6885 if (status != -NFS4ERR_DELAY)
6886 break;
6887 nfs4_handle_exception(server, status, &exception);
6888 } while (exception.retry);
6889 return status;
6890}
6891
Andy Adamson5ec16a82013-08-08 10:57:55 -04006892/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006893 * If 'use_integrity' is true and the state managment nfs_client
6894 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6895 * and the machine credential as per RFC3530bis and RFC5661 Security
6896 * Considerations sections. Otherwise, just use the user cred with the
6897 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006898 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006899static 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 +00006900{
6901 int status;
6902 struct nfs4_secinfo_arg args = {
6903 .dir_fh = NFS_FH(dir),
6904 .name = name,
6905 };
6906 struct nfs4_secinfo_res res = {
6907 .flavors = flavors,
6908 };
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6911 .rpc_argp = &args,
6912 .rpc_resp = &res,
6913 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006914 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006915 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006916
6917 if (use_integrity) {
6918 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006919 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6920 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006921 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006922
6923 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006924
6925 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6926 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6927
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006928 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6929 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006930 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006931
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006932 if (cred)
6933 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006934
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006935 return status;
6936}
6937
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006938int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6939 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006940{
6941 struct nfs4_exception exception = { };
6942 int err;
6943 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006944 err = -NFS4ERR_WRONGSEC;
6945
6946 /* try to use integrity protection with machine cred */
6947 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6948 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6949
6950 /*
6951 * if unable to use integrity protection, or SECINFO with
6952 * integrity protection returns NFS4ERR_WRONGSEC (which is
6953 * disallowed by spec, but exists in deployed servers) use
6954 * the current filesystem's rpc_client and the user cred.
6955 */
6956 if (err == -NFS4ERR_WRONGSEC)
6957 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6958
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006959 trace_nfs4_secinfo(dir, name, err);
6960 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006961 &exception);
6962 } while (exception.retry);
6963 return err;
6964}
6965
Andy Adamson557134a2009-04-01 09:21:53 -04006966#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006967/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006968 * Check the exchange flags returned by the server for invalid flags, having
6969 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6970 * DS flags set.
6971 */
6972static int nfs4_check_cl_exchange_flags(u32 flags)
6973{
6974 if (flags & ~EXCHGID4_FLAG_MASK_R)
6975 goto out_inval;
6976 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6977 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6978 goto out_inval;
6979 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6980 goto out_inval;
6981 return NFS_OK;
6982out_inval:
6983 return -NFS4ERR_INVAL;
6984}
6985
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006986static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006987nfs41_same_server_scope(struct nfs41_server_scope *a,
6988 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006989{
6990 if (a->server_scope_sz == b->server_scope_sz &&
6991 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6992 return true;
6993
6994 return false;
6995}
6996
Andy Adamson02a95de2016-02-05 16:08:37 -05006997static void
6998nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6999{
7000}
7001
7002static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7003 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7004};
7005
Andy Adamson357f54d2010-12-14 10:11:57 -05007006/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007007 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007008 *
7009 * The 4.1 client currently uses the same TCP connection for the
7010 * fore and backchannel.
7011 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007012static
7013int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7014 struct rpc_xprt *xprt,
7015 struct nfs_client *clp,
7016 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007017{
7018 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007019 struct nfs41_bind_conn_to_session_args args = {
7020 .client = clp,
7021 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7022 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007023 struct nfs41_bind_conn_to_session_res res;
7024 struct rpc_message msg = {
7025 .rpc_proc =
7026 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007027 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007028 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007029 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007030 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007031 struct rpc_task_setup task_setup_data = {
7032 .rpc_client = clnt,
7033 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007034 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007035 .rpc_message = &msg,
7036 .flags = RPC_TASK_TIMEOUT,
7037 };
7038 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007039
7040 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007041
Trond Myklebust71a097c2015-02-18 09:27:18 -08007042 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7043 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7044 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007045
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007046 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7047 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7048 args.dir = NFS4_CDFC4_FORE;
7049
7050 task = rpc_run_task(&task_setup_data);
7051 if (!IS_ERR(task)) {
7052 status = task->tk_status;
7053 rpc_put_task(task);
7054 } else
7055 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007056 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007057 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007058 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007059 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7060 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7061 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007062 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007063 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007064 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007065 dprintk("NFS: %s: Unexpected direction from server\n",
7066 __func__);
7067 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007068 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007069 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007070 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007071 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7072 __func__);
7073 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007074 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007075 }
7076 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007077out:
7078 dprintk("<-- %s status= %d\n", __func__, status);
7079 return status;
7080}
7081
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007082struct rpc_bind_conn_calldata {
7083 struct nfs_client *clp;
7084 struct rpc_cred *cred;
7085};
7086
7087static int
7088nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7089 struct rpc_xprt *xprt,
7090 void *calldata)
7091{
7092 struct rpc_bind_conn_calldata *p = calldata;
7093
7094 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7095}
7096
7097int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7098{
7099 struct rpc_bind_conn_calldata data = {
7100 .clp = clp,
7101 .cred = cred,
7102 };
7103 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7104 nfs4_proc_bind_conn_to_session_callback, &data);
7105}
7106
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007107/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007108 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7109 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007110 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007111static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7112 .how = SP4_MACH_CRED,
7113 .enforce.u.words = {
7114 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7115 1 << (OP_EXCHANGE_ID - 32) |
7116 1 << (OP_CREATE_SESSION - 32) |
7117 1 << (OP_DESTROY_SESSION - 32) |
7118 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007119 },
7120 .allow.u.words = {
7121 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007122 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007123 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007124 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007125 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007126 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007127 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007128 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007129 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007130 1 << (OP_FREE_STATEID - 32) |
7131 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007132 }
7133};
7134
7135/*
7136 * Select the state protection mode for client `clp' given the server results
7137 * from exchange_id in `sp'.
7138 *
7139 * Returns 0 on success, negative errno otherwise.
7140 */
7141static int nfs4_sp4_select_mode(struct nfs_client *clp,
7142 struct nfs41_state_protection *sp)
7143{
7144 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7145 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7146 1 << (OP_EXCHANGE_ID - 32) |
7147 1 << (OP_CREATE_SESSION - 32) |
7148 1 << (OP_DESTROY_SESSION - 32) |
7149 1 << (OP_DESTROY_CLIENTID - 32)
7150 };
7151 unsigned int i;
7152
7153 if (sp->how == SP4_MACH_CRED) {
7154 /* Print state protect result */
7155 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7156 for (i = 0; i <= LAST_NFS4_OP; i++) {
7157 if (test_bit(i, sp->enforce.u.longs))
7158 dfprintk(MOUNT, " enforce op %d\n", i);
7159 if (test_bit(i, sp->allow.u.longs))
7160 dfprintk(MOUNT, " allow op %d\n", i);
7161 }
7162
7163 /* make sure nothing is on enforce list that isn't supported */
7164 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7165 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7166 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7167 return -EINVAL;
7168 }
7169 }
7170
7171 /*
7172 * Minimal mode - state operations are allowed to use machine
7173 * credential. Note this already happens by default, so the
7174 * client doesn't have to do anything more than the negotiation.
7175 *
7176 * NOTE: we don't care if EXCHANGE_ID is in the list -
7177 * we're already using the machine cred for exchange_id
7178 * and will never use a different cred.
7179 */
7180 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7181 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7182 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7183 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7184 dfprintk(MOUNT, "sp4_mach_cred:\n");
7185 dfprintk(MOUNT, " minimal mode enabled\n");
7186 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7187 } else {
7188 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7189 return -EINVAL;
7190 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007191
7192 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007193 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7194 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007195 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7196 dfprintk(MOUNT, " cleanup mode enabled\n");
7197 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7198 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007199
Andrew Elble99ade3c2015-12-02 09:39:51 -05007200 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7201 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7202 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7203 &clp->cl_sp4_flags);
7204 }
7205
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007206 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7207 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7208 dfprintk(MOUNT, " secinfo mode enabled\n");
7209 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7210 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007211
7212 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7213 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7214 dfprintk(MOUNT, " stateid mode enabled\n");
7215 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7216 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007217
7218 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7219 dfprintk(MOUNT, " write mode enabled\n");
7220 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7221 }
7222
7223 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7224 dfprintk(MOUNT, " commit mode enabled\n");
7225 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7226 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007227 }
7228
7229 return 0;
7230}
7231
Andy Adamson8d89bd72016-09-09 09:22:18 -04007232struct nfs41_exchange_id_data {
7233 struct nfs41_exchange_id_res res;
7234 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007235 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007236 int rpc_status;
7237};
7238
7239static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7240{
7241 struct nfs41_exchange_id_data *cdata =
7242 (struct nfs41_exchange_id_data *)data;
7243 struct nfs_client *clp = cdata->args.client;
7244 int status = task->tk_status;
7245
7246 trace_nfs4_exchange_id(clp, status);
7247
7248 if (status == 0)
7249 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007250
7251 if (cdata->xprt && status == 0) {
7252 status = nfs4_detect_session_trunking(clp, &cdata->res,
7253 cdata->xprt);
7254 goto out;
7255 }
7256
Andy Adamson8d89bd72016-09-09 09:22:18 -04007257 if (status == 0)
7258 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7259
7260 if (status == 0) {
7261 clp->cl_clientid = cdata->res.clientid;
7262 clp->cl_exchange_flags = cdata->res.flags;
7263 /* Client ID is not confirmed */
7264 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7265 clear_bit(NFS4_SESSION_ESTABLISHED,
7266 &clp->cl_session->session_state);
7267 clp->cl_seqid = cdata->res.seqid;
7268 }
7269
7270 kfree(clp->cl_serverowner);
7271 clp->cl_serverowner = cdata->res.server_owner;
7272 cdata->res.server_owner = NULL;
7273
7274 /* use the most recent implementation id */
7275 kfree(clp->cl_implid);
7276 clp->cl_implid = cdata->res.impl_id;
7277 cdata->res.impl_id = NULL;
7278
7279 if (clp->cl_serverscope != NULL &&
7280 !nfs41_same_server_scope(clp->cl_serverscope,
7281 cdata->res.server_scope)) {
7282 dprintk("%s: server_scope mismatch detected\n",
7283 __func__);
7284 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7285 kfree(clp->cl_serverscope);
7286 clp->cl_serverscope = NULL;
7287 }
7288
7289 if (clp->cl_serverscope == NULL) {
7290 clp->cl_serverscope = cdata->res.server_scope;
7291 cdata->res.server_scope = NULL;
7292 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007293 /* Save the EXCHANGE_ID verifier session trunk tests */
7294 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7295 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007296 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007297out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007298 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007299 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007300}
7301
7302static void nfs4_exchange_id_release(void *data)
7303{
7304 struct nfs41_exchange_id_data *cdata =
7305 (struct nfs41_exchange_id_data *)data;
7306
7307 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007308 if (cdata->xprt) {
7309 xprt_put(cdata->xprt);
7310 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7311 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007312 kfree(cdata->res.impl_id);
7313 kfree(cdata->res.server_scope);
7314 kfree(cdata->res.server_owner);
7315 kfree(cdata);
7316}
7317
7318static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7319 .rpc_call_done = nfs4_exchange_id_done,
7320 .rpc_release = nfs4_exchange_id_release,
7321};
7322
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007323/*
7324 * _nfs4_proc_exchange_id()
7325 *
7326 * Wrapper for EXCHANGE_ID operation.
7327 */
7328static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007329 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007330{
7331 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007332 struct rpc_message msg = {
7333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007334 .rpc_cred = cred,
7335 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007336 struct rpc_task_setup task_setup_data = {
7337 .rpc_client = clp->cl_rpcclient,
7338 .callback_ops = &nfs4_exchange_id_call_ops,
7339 .rpc_message = &msg,
7340 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7341 };
7342 struct nfs41_exchange_id_data *calldata;
7343 struct rpc_task *task;
7344 int status = -EIO;
7345
7346 if (!atomic_inc_not_zero(&clp->cl_count))
7347 goto out;
7348
7349 status = -ENOMEM;
7350 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7351 if (!calldata)
7352 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007353
Andy Adamsonad0849a2016-09-09 09:22:28 -04007354 if (!xprt)
7355 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007356
7357 status = nfs4_init_uniform_client_string(clp);
7358 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007359 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007360
7361 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007362 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007363 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007364
Andy Adamson8d89bd72016-09-09 09:22:18 -04007365 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7366 GFP_NOFS);
7367 status = -ENOMEM;
7368 if (unlikely(calldata->res.server_owner == NULL))
7369 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007370
Andy Adamson8d89bd72016-09-09 09:22:18 -04007371 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007372 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007373 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007374 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007375
Andy Adamson8d89bd72016-09-09 09:22:18 -04007376 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7377 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007378 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007379
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007380 switch (sp4_how) {
7381 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007382 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007383 break;
7384
7385 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007386 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007387 break;
7388
7389 default:
7390 /* unsupported! */
7391 WARN_ON_ONCE(1);
7392 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007393 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007394 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007395 if (xprt) {
7396 calldata->xprt = xprt;
7397 task_setup_data.rpc_xprt = xprt;
7398 task_setup_data.flags =
7399 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7400 calldata->args.verifier = &clp->cl_confirm;
7401 } else {
7402 calldata->args.verifier = &verifier;
7403 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007404 calldata->args.client = clp;
7405#ifdef CONFIG_NFS_V4_1_MIGRATION
7406 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7407 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7408 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7409#else
7410 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7411 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7412#endif
7413 msg.rpc_argp = &calldata->args;
7414 msg.rpc_resp = &calldata->res;
7415 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007416
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417 task = rpc_run_task(&task_setup_data);
7418 if (IS_ERR(task)) {
7419 status = PTR_ERR(task);
7420 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007421 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007422
Andy Adamsonad0849a2016-09-09 09:22:28 -04007423 if (!xprt) {
7424 status = rpc_wait_for_completion_task(task);
7425 if (!status)
7426 status = calldata->rpc_status;
7427 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007429
Andy Adamson8d89bd72016-09-09 09:22:18 -04007430 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007431out:
Chuck Lever177313f2012-05-21 22:44:58 -04007432 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007433 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007434 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007435 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007436 clp->cl_implid->date.seconds,
7437 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007438 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007439 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007440
7441out_impl_id:
7442 kfree(calldata->res.impl_id);
7443out_server_scope:
7444 kfree(calldata->res.server_scope);
7445out_server_owner:
7446 kfree(calldata->res.server_owner);
7447out_calldata:
7448 kfree(calldata);
7449 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007450}
7451
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007452/*
7453 * nfs4_proc_exchange_id()
7454 *
7455 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7456 *
7457 * Since the clientid has expired, all compounds using sessions
7458 * associated with the stale clientid will be returning
7459 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7460 * be in some phase of session reset.
7461 *
7462 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7463 */
7464int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7465{
7466 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7467 int status;
7468
7469 /* try SP4_MACH_CRED if krb5i/p */
7470 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7471 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007472 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007473 if (!status)
7474 return 0;
7475 }
7476
7477 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007478 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007479}
7480
Andy Adamson04fa2c62016-09-09 09:22:29 -04007481/**
7482 * nfs4_test_session_trunk
7483 *
7484 * This is an add_xprt_test() test function called from
7485 * rpc_clnt_setup_test_and_add_xprt.
7486 *
7487 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7488 * and is dereferrenced in nfs4_exchange_id_release
7489 *
7490 * Upon success, add the new transport to the rpc_clnt
7491 *
7492 * @clnt: struct rpc_clnt to get new transport
7493 * @xprt: the rpc_xprt to test
7494 * @data: call data for _nfs4_proc_exchange_id.
7495 */
7496int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7497 void *data)
7498{
7499 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7500 u32 sp4_how;
7501
7502 dprintk("--> %s try %s\n", __func__,
7503 xprt->address_strings[RPC_DISPLAY_ADDR]);
7504
7505 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7506
7507 /* Test connection for session trunking. Async exchange_id call */
7508 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7509}
7510EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7511
Trond Myklebust66245532012-05-25 17:18:09 -04007512static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7513 struct rpc_cred *cred)
7514{
7515 struct rpc_message msg = {
7516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7517 .rpc_argp = clp,
7518 .rpc_cred = cred,
7519 };
7520 int status;
7521
7522 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007523 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007524 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007525 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007526 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7527 return status;
7528}
7529
7530static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7531 struct rpc_cred *cred)
7532{
7533 unsigned int loop;
7534 int ret;
7535
7536 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7537 ret = _nfs4_proc_destroy_clientid(clp, cred);
7538 switch (ret) {
7539 case -NFS4ERR_DELAY:
7540 case -NFS4ERR_CLIENTID_BUSY:
7541 ssleep(1);
7542 break;
7543 default:
7544 return ret;
7545 }
7546 }
7547 return 0;
7548}
7549
7550int nfs4_destroy_clientid(struct nfs_client *clp)
7551{
7552 struct rpc_cred *cred;
7553 int ret = 0;
7554
7555 if (clp->cl_mvops->minor_version < 1)
7556 goto out;
7557 if (clp->cl_exchange_flags == 0)
7558 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007559 if (clp->cl_preserve_clid)
7560 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007561 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007562 ret = nfs4_proc_destroy_clientid(clp, cred);
7563 if (cred)
7564 put_rpccred(cred);
7565 switch (ret) {
7566 case 0:
7567 case -NFS4ERR_STALE_CLIENTID:
7568 clp->cl_exchange_flags = 0;
7569 }
7570out:
7571 return ret;
7572}
7573
Andy Adamson2050f0c2009-04-01 09:22:30 -04007574struct nfs4_get_lease_time_data {
7575 struct nfs4_get_lease_time_args *args;
7576 struct nfs4_get_lease_time_res *res;
7577 struct nfs_client *clp;
7578};
7579
7580static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7581 void *calldata)
7582{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007583 struct nfs4_get_lease_time_data *data =
7584 (struct nfs4_get_lease_time_data *)calldata;
7585
7586 dprintk("--> %s\n", __func__);
7587 /* just setup sequence, do not trigger session recovery
7588 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007589 nfs41_setup_sequence(data->clp->cl_session,
7590 &data->args->la_seq_args,
7591 &data->res->lr_seq_res,
7592 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007593 dprintk("<-- %s\n", __func__);
7594}
7595
7596/*
7597 * Called from nfs4_state_manager thread for session setup, so don't recover
7598 * from sequence operation or clientid errors.
7599 */
7600static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7601{
7602 struct nfs4_get_lease_time_data *data =
7603 (struct nfs4_get_lease_time_data *)calldata;
7604
7605 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007606 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7607 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007608 switch (task->tk_status) {
7609 case -NFS4ERR_DELAY:
7610 case -NFS4ERR_GRACE:
7611 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7612 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7613 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007614 /* fall through */
7615 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007616 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007617 return;
7618 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007619 dprintk("<-- %s\n", __func__);
7620}
7621
Trond Myklebust17280172012-03-11 13:11:00 -04007622static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007623 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7624 .rpc_call_done = nfs4_get_lease_time_done,
7625};
7626
7627int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7628{
7629 struct rpc_task *task;
7630 struct nfs4_get_lease_time_args args;
7631 struct nfs4_get_lease_time_res res = {
7632 .lr_fsinfo = fsinfo,
7633 };
7634 struct nfs4_get_lease_time_data data = {
7635 .args = &args,
7636 .res = &res,
7637 .clp = clp,
7638 };
7639 struct rpc_message msg = {
7640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7641 .rpc_argp = &args,
7642 .rpc_resp = &res,
7643 };
7644 struct rpc_task_setup task_setup = {
7645 .rpc_client = clp->cl_rpcclient,
7646 .rpc_message = &msg,
7647 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007648 .callback_data = &data,
7649 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007650 };
7651 int status;
7652
Chuck Levera9c92d62013-08-09 12:48:18 -04007653 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007654 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007655 dprintk("--> %s\n", __func__);
7656 task = rpc_run_task(&task_setup);
7657
7658 if (IS_ERR(task))
7659 status = PTR_ERR(task);
7660 else {
7661 status = task->tk_status;
7662 rpc_put_task(task);
7663 }
7664 dprintk("<-- %s return %d\n", __func__, status);
7665
7666 return status;
7667}
7668
Andy Adamsonfc931582009-04-01 09:22:31 -04007669/*
7670 * Initialize the values to be used by the client in CREATE_SESSION
7671 * If nfs4_init_session set the fore channel request and response sizes,
7672 * use them.
7673 *
7674 * Set the back channel max_resp_sz_cached to zero to force the client to
7675 * always set csa_cachethis to FALSE because the current implementation
7676 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7677 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007678static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7679 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007680{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007681 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007682 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007683
Andy Adamson18aad3d2013-06-26 12:21:49 -04007684 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7685 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7686
Andy Adamsonfc931582009-04-01 09:22:31 -04007687 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007688 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7689 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007690 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007691 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007692
7693 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007694 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007695 __func__,
7696 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007697 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007698
7699 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007700 args->bc_attrs.max_rqst_sz = max_bc_payload;
7701 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007702 args->bc_attrs.max_resp_sz_cached = 0;
7703 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007704 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007705
7706 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7707 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7708 __func__,
7709 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7710 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7711 args->bc_attrs.max_reqs);
7712}
7713
Trond Myklebust79969dd2015-02-18 11:30:18 -08007714static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7715 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007716{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007717 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007718 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007719
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007720 if (rcvd->max_resp_sz > sent->max_resp_sz)
7721 return -EINVAL;
7722 /*
7723 * Our requested max_ops is the minimum we need; we're not
7724 * prepared to break up compounds into smaller pieces than that.
7725 * So, no point even trying to continue if the server won't
7726 * cooperate:
7727 */
7728 if (rcvd->max_ops < sent->max_ops)
7729 return -EINVAL;
7730 if (rcvd->max_reqs == 0)
7731 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007732 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7733 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007734 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007735}
7736
Trond Myklebust79969dd2015-02-18 11:30:18 -08007737static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7738 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007739{
7740 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007741 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007742
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007743 if (!(res->flags & SESSION4_BACK_CHAN))
7744 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007745 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7746 return -EINVAL;
7747 if (rcvd->max_resp_sz < sent->max_resp_sz)
7748 return -EINVAL;
7749 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7750 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007751 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007752 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007753 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007754 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007755out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007756 return 0;
7757}
Andy Adamson8d353012009-04-01 09:22:32 -04007758
Andy Adamson8d353012009-04-01 09:22:32 -04007759static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007760 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007761{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007762 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007763
Trond Myklebust79969dd2015-02-18 11:30:18 -08007764 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007765 if (ret)
7766 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007767 return nfs4_verify_back_channel_attrs(args, res);
7768}
7769
7770static void nfs4_update_session(struct nfs4_session *session,
7771 struct nfs41_create_session_res *res)
7772{
7773 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007774 /* Mark client id and session as being confirmed */
7775 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7776 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007777 session->flags = res->flags;
7778 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007779 if (res->flags & SESSION4_BACK_CHAN)
7780 memcpy(&session->bc_attrs, &res->bc_attrs,
7781 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007782}
7783
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007784static int _nfs4_proc_create_session(struct nfs_client *clp,
7785 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007786{
7787 struct nfs4_session *session = clp->cl_session;
7788 struct nfs41_create_session_args args = {
7789 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007790 .clientid = clp->cl_clientid,
7791 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007792 .cb_program = NFS4_CALLBACK,
7793 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007794 struct nfs41_create_session_res res;
7795
Andy Adamsonfc931582009-04-01 09:22:31 -04007796 struct rpc_message msg = {
7797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7798 .rpc_argp = &args,
7799 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007800 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007801 };
7802 int status;
7803
Chuck Lever6b26cc82016-05-02 14:40:40 -04007804 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007805 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007806
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007807 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007808 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007809
Trond Myklebustb519d402016-09-11 14:50:01 -04007810 switch (status) {
7811 case -NFS4ERR_STALE_CLIENTID:
7812 case -NFS4ERR_DELAY:
7813 case -ETIMEDOUT:
7814 case -EACCES:
7815 case -EAGAIN:
7816 goto out;
7817 };
7818
7819 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007820 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007821 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007822 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007823 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007824 if (status)
7825 goto out;
7826 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007827 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007828out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007829 return status;
7830}
7831
7832/*
7833 * Issues a CREATE_SESSION operation to the server.
7834 * It is the responsibility of the caller to verify the session is
7835 * expired before calling this routine.
7836 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007837int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007838{
7839 int status;
7840 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007841 struct nfs4_session *session = clp->cl_session;
7842
7843 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7844
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007845 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007846 if (status)
7847 goto out;
7848
Andy Adamsonaacd5532011-11-09 13:58:21 -05007849 /* Init or reset the session slot tables */
7850 status = nfs4_setup_session_slot_tables(session);
7851 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007852 if (status)
7853 goto out;
7854
7855 ptr = (unsigned *)&session->sess_id.data[0];
7856 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7857 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007858out:
7859 dprintk("<-- %s\n", __func__);
7860 return status;
7861}
7862
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007863/*
7864 * Issue the over-the-wire RPC DESTROY_SESSION.
7865 * The caller must serialize access to this routine.
7866 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007867int nfs4_proc_destroy_session(struct nfs4_session *session,
7868 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007869{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007870 struct rpc_message msg = {
7871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7872 .rpc_argp = session,
7873 .rpc_cred = cred,
7874 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007875 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007876
7877 dprintk("--> nfs4_proc_destroy_session\n");
7878
7879 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007880 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7881 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007882
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007883 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007884 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007885
7886 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007887 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007888 "Session has been destroyed regardless...\n", status);
7889
7890 dprintk("<-- nfs4_proc_destroy_session\n");
7891 return status;
7892}
7893
Trond Myklebust7b38c362012-05-23 13:23:31 -04007894/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007895 * Renew the cl_session lease.
7896 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007897struct nfs4_sequence_data {
7898 struct nfs_client *clp;
7899 struct nfs4_sequence_args args;
7900 struct nfs4_sequence_res res;
7901};
7902
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007903static void nfs41_sequence_release(void *data)
7904{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007905 struct nfs4_sequence_data *calldata = data;
7906 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007907
Alexandros Batsakis71358402010-02-05 03:45:05 -08007908 if (atomic_read(&clp->cl_count) > 1)
7909 nfs4_schedule_state_renewal(clp);
7910 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007911 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007912}
7913
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007914static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7915{
7916 switch(task->tk_status) {
7917 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007918 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7919 return -EAGAIN;
7920 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007921 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007922 }
7923 return 0;
7924}
7925
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007926static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007927{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007928 struct nfs4_sequence_data *calldata = data;
7929 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007930
Trond Myklebust14516c32010-07-31 14:29:06 -04007931 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7932 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007933
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007934 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007935 if (task->tk_status < 0) {
7936 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007937 if (atomic_read(&clp->cl_count) == 1)
7938 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007939
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007940 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7941 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007942 return;
7943 }
7944 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007945 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007946out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007947 dprintk("<-- %s\n", __func__);
7948}
7949
7950static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7951{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007952 struct nfs4_sequence_data *calldata = data;
7953 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007954 struct nfs4_sequence_args *args;
7955 struct nfs4_sequence_res *res;
7956
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007957 args = task->tk_msg.rpc_argp;
7958 res = task->tk_msg.rpc_resp;
7959
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007960 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007961}
7962
7963static const struct rpc_call_ops nfs41_sequence_ops = {
7964 .rpc_call_done = nfs41_sequence_call_done,
7965 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007966 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007967};
7968
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007969static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7970 struct rpc_cred *cred,
7971 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007972{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007973 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007974 struct rpc_message msg = {
7975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7976 .rpc_cred = cred,
7977 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007978 struct rpc_task_setup task_setup_data = {
7979 .rpc_client = clp->cl_rpcclient,
7980 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007981 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007982 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007983 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007984
Alexandros Batsakis71358402010-02-05 03:45:05 -08007985 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007986 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007987 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007988 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007989 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007990 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007991 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007992 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007993 if (is_privileged)
7994 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007995 msg.rpc_argp = &calldata->args;
7996 msg.rpc_resp = &calldata->res;
7997 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007998 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007999
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008000 return rpc_run_task(&task_setup_data);
8001}
8002
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008003static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008004{
8005 struct rpc_task *task;
8006 int ret = 0;
8007
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008008 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008009 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008010 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008011 if (IS_ERR(task))
8012 ret = PTR_ERR(task);
8013 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008014 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008015 dprintk("<-- %s status=%d\n", __func__, ret);
8016 return ret;
8017}
8018
8019static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8020{
8021 struct rpc_task *task;
8022 int ret;
8023
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008024 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008025 if (IS_ERR(task)) {
8026 ret = PTR_ERR(task);
8027 goto out;
8028 }
8029 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008030 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008031 ret = task->tk_status;
8032 rpc_put_task(task);
8033out:
8034 dprintk("<-- %s status=%d\n", __func__, ret);
8035 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008036}
8037
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008038struct nfs4_reclaim_complete_data {
8039 struct nfs_client *clp;
8040 struct nfs41_reclaim_complete_args arg;
8041 struct nfs41_reclaim_complete_res res;
8042};
8043
8044static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8045{
8046 struct nfs4_reclaim_complete_data *calldata = data;
8047
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008048 nfs41_setup_sequence(calldata->clp->cl_session,
8049 &calldata->arg.seq_args,
8050 &calldata->res.seq_res,
8051 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008052}
8053
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008054static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8055{
8056 switch(task->tk_status) {
8057 case 0:
8058 case -NFS4ERR_COMPLETE_ALREADY:
8059 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8060 break;
8061 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008062 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008063 /* fall through */
8064 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008065 return -EAGAIN;
8066 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008067 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008068 }
8069 return 0;
8070}
8071
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008072static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8073{
8074 struct nfs4_reclaim_complete_data *calldata = data;
8075 struct nfs_client *clp = calldata->clp;
8076 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8077
8078 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008079 if (!nfs41_sequence_done(task, res))
8080 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008081
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008082 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008083 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8084 rpc_restart_call_prepare(task);
8085 return;
8086 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008087 dprintk("<-- %s\n", __func__);
8088}
8089
8090static void nfs4_free_reclaim_complete_data(void *data)
8091{
8092 struct nfs4_reclaim_complete_data *calldata = data;
8093
8094 kfree(calldata);
8095}
8096
8097static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8098 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8099 .rpc_call_done = nfs4_reclaim_complete_done,
8100 .rpc_release = nfs4_free_reclaim_complete_data,
8101};
8102
8103/*
8104 * Issue a global reclaim complete.
8105 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008106static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8107 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008108{
8109 struct nfs4_reclaim_complete_data *calldata;
8110 struct rpc_task *task;
8111 struct rpc_message msg = {
8112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008113 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008114 };
8115 struct rpc_task_setup task_setup_data = {
8116 .rpc_client = clp->cl_rpcclient,
8117 .rpc_message = &msg,
8118 .callback_ops = &nfs4_reclaim_complete_call_ops,
8119 .flags = RPC_TASK_ASYNC,
8120 };
8121 int status = -ENOMEM;
8122
8123 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008124 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008125 if (calldata == NULL)
8126 goto out;
8127 calldata->clp = clp;
8128 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008129
Chuck Levera9c92d62013-08-09 12:48:18 -04008130 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008131 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008132 msg.rpc_argp = &calldata->arg;
8133 msg.rpc_resp = &calldata->res;
8134 task_setup_data.callback_data = calldata;
8135 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008136 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008137 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008138 goto out;
8139 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008140 status = nfs4_wait_for_completion_rpc_task(task);
8141 if (status == 0)
8142 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008143 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008144 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008145out:
8146 dprintk("<-- %s status=%d\n", __func__, status);
8147 return status;
8148}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008149
8150static void
8151nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8152{
8153 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008154 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008155 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008156
8157 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008158 nfs41_setup_sequence(session, &lgp->args.seq_args,
8159 &lgp->res.seq_res, task);
8160 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008161}
8162
8163static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8164{
8165 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008166
8167 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008168 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008169 dprintk("<-- %s\n", __func__);
8170}
8171
8172static int
8173nfs4_layoutget_handle_exception(struct rpc_task *task,
8174 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8175{
Trond Myklebustee314c22012-10-01 17:25:48 -07008176 struct inode *inode = lgp->args.inode;
8177 struct nfs_server *server = NFS_SERVER(inode);
8178 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008179 int nfs4err = task->tk_status;
8180 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008181 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008182
Boaz Harroshed7e5422014-01-22 20:34:54 +02008183 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008184
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008185 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008186 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008187 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008188
8189 /*
8190 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8191 * on the file. set tk_status to -ENODATA to tell upper layer to
8192 * retry go inband.
8193 */
8194 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008195 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008196 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008197 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008198 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8199 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8200 */
8201 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008202 status = -EOVERFLOW;
8203 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008204 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008205 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008206 * (or clients) writing to the same RAID stripe except when
8207 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008208 *
8209 * Treat it like we would RECALLCONFLICT -- we retry for a little
8210 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008211 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008212 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008213 if (lgp->args.minlength == 0) {
8214 status = -EOVERFLOW;
8215 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008216 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008217 status = -EBUSY;
8218 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008219 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008220 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008221 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07008222 case -NFS4ERR_EXPIRED:
8223 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008224 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008225 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008226 lo = NFS_I(inode)->layout;
8227 /* If the open stateid was bad, then recover it. */
8228 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8229 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008230 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008231 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008232 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04008233 break;
8234 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008235
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008236 /*
8237 * Mark the bad layout state as invalid, then retry
8238 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008239 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008240 spin_unlock(&inode->i_lock);
8241 pnfs_free_lseg_list(&head);
8242 status = -EAGAIN;
8243 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008244 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008245
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008246 err = nfs4_handle_exception(server, nfs4err, exception);
8247 if (!status) {
8248 if (exception->retry)
8249 status = -EAGAIN;
8250 else
8251 status = err;
8252 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008253out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008254 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008255 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008256}
8257
Idan Kedar85541162012-08-02 11:47:10 +03008258static size_t max_response_pages(struct nfs_server *server)
8259{
8260 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8261 return nfs_page_array_len(0, max_resp_sz);
8262}
8263
8264static void nfs4_free_pages(struct page **pages, size_t size)
8265{
8266 int i;
8267
8268 if (!pages)
8269 return;
8270
8271 for (i = 0; i < size; i++) {
8272 if (!pages[i])
8273 break;
8274 __free_page(pages[i]);
8275 }
8276 kfree(pages);
8277}
8278
8279static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8280{
8281 struct page **pages;
8282 int i;
8283
8284 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8285 if (!pages) {
8286 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8287 return NULL;
8288 }
8289
8290 for (i = 0; i < size; i++) {
8291 pages[i] = alloc_page(gfp_flags);
8292 if (!pages[i]) {
8293 dprintk("%s: failed to allocate page\n", __func__);
8294 nfs4_free_pages(pages, size);
8295 return NULL;
8296 }
8297 }
8298
8299 return pages;
8300}
8301
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008302static void nfs4_layoutget_release(void *calldata)
8303{
8304 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008305 struct inode *inode = lgp->args.inode;
8306 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008307 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008308
8309 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008310 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008311 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008312 put_nfs_open_context(lgp->args.ctx);
8313 kfree(calldata);
8314 dprintk("<-- %s\n", __func__);
8315}
8316
8317static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8318 .rpc_call_prepare = nfs4_layoutget_prepare,
8319 .rpc_call_done = nfs4_layoutget_done,
8320 .rpc_release = nfs4_layoutget_release,
8321};
8322
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008323struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008324nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008325{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008326 struct inode *inode = lgp->args.inode;
8327 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008328 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008329 struct rpc_task *task;
8330 struct rpc_message msg = {
8331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8332 .rpc_argp = &lgp->args,
8333 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008334 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008335 };
8336 struct rpc_task_setup task_setup_data = {
8337 .rpc_client = server->client,
8338 .rpc_message = &msg,
8339 .callback_ops = &nfs4_layoutget_call_ops,
8340 .callback_data = lgp,
8341 .flags = RPC_TASK_ASYNC,
8342 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008343 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008344 struct nfs4_exception exception = {
8345 .inode = inode,
8346 .timeout = *timeout,
8347 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008348 int status = 0;
8349
8350 dprintk("--> %s\n", __func__);
8351
Peng Tao4bd5a982014-11-17 11:05:17 +08008352 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8353 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8354
Idan Kedar85541162012-08-02 11:47:10 +03008355 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8356 if (!lgp->args.layout.pages) {
8357 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008358 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008359 }
8360 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8361
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008362 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008363 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008364 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008365
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008366 task = rpc_run_task(&task_setup_data);
8367 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008368 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008369 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008370 if (status == 0) {
8371 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8372 *timeout = exception.timeout;
8373 }
8374
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008375 trace_nfs4_layoutget(lgp->args.ctx,
8376 &lgp->args.range,
8377 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008378 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008379 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008380
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008381 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8382 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008383 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008384 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008385 rpc_put_task(task);
8386 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008387 if (status)
8388 return ERR_PTR(status);
8389 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008390}
8391
Benny Halevycbe82602011-05-22 19:52:37 +03008392static void
8393nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8394{
8395 struct nfs4_layoutreturn *lrp = calldata;
8396
8397 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008398 nfs41_setup_sequence(lrp->clp->cl_session,
8399 &lrp->args.seq_args,
8400 &lrp->res.seq_res,
8401 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008402}
8403
8404static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8405{
8406 struct nfs4_layoutreturn *lrp = calldata;
8407 struct nfs_server *server;
8408
8409 dprintk("--> %s\n", __func__);
8410
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008411 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008412 return;
8413
8414 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008415 switch (task->tk_status) {
8416 default:
8417 task->tk_status = 0;
8418 case 0:
8419 break;
8420 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008421 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008422 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008423 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008424 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008425 return;
8426 }
Benny Halevycbe82602011-05-22 19:52:37 +03008427 dprintk("<-- %s\n", __func__);
8428}
8429
8430static void nfs4_layoutreturn_release(void *calldata)
8431{
8432 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008433 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008434 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008435
8436 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008437 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008438 if (lrp->res.lrs_present) {
8439 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8440 &lrp->args.range,
8441 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008442 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008443 } else
8444 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008445 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008446 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008447 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008448 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008449 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008450 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008451 kfree(calldata);
8452 dprintk("<-- %s\n", __func__);
8453}
8454
8455static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8456 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8457 .rpc_call_done = nfs4_layoutreturn_done,
8458 .rpc_release = nfs4_layoutreturn_release,
8459};
8460
Peng Tao6c166052014-11-17 09:30:40 +08008461int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008462{
8463 struct rpc_task *task;
8464 struct rpc_message msg = {
8465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8466 .rpc_argp = &lrp->args,
8467 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008468 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008469 };
8470 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008471 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008472 .rpc_message = &msg,
8473 .callback_ops = &nfs4_layoutreturn_call_ops,
8474 .callback_data = lrp,
8475 };
Peng Tao6c166052014-11-17 09:30:40 +08008476 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008477
Andrew Elble99ade3c2015-12-02 09:39:51 -05008478 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8479 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8480 &task_setup_data.rpc_client, &msg);
8481
Benny Halevycbe82602011-05-22 19:52:37 +03008482 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008483 if (!sync) {
8484 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8485 if (!lrp->inode) {
8486 nfs4_layoutreturn_release(lrp);
8487 return -EAGAIN;
8488 }
8489 task_setup_data.flags |= RPC_TASK_ASYNC;
8490 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008491 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008492 task = rpc_run_task(&task_setup_data);
8493 if (IS_ERR(task))
8494 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008495 if (sync)
8496 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008497 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008498 dprintk("<-- %s status=%d\n", __func__, status);
8499 rpc_put_task(task);
8500 return status;
8501}
8502
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008503static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008504_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8505 struct pnfs_device *pdev,
8506 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507{
8508 struct nfs4_getdeviceinfo_args args = {
8509 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008510 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8511 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008512 };
8513 struct nfs4_getdeviceinfo_res res = {
8514 .pdev = pdev,
8515 };
8516 struct rpc_message msg = {
8517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8518 .rpc_argp = &args,
8519 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008520 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008521 };
8522 int status;
8523
8524 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008525 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008526 if (res.notification & ~args.notify_types)
8527 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008528 if (res.notification != args.notify_types)
8529 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008530
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008531 dprintk("<-- %s status=%d\n", __func__, status);
8532
8533 return status;
8534}
8535
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008536int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8537 struct pnfs_device *pdev,
8538 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539{
8540 struct nfs4_exception exception = { };
8541 int err;
8542
8543 do {
8544 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008545 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008546 &exception);
8547 } while (exception.retry);
8548 return err;
8549}
8550EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8551
Andy Adamson863a3c62011-03-23 13:27:54 +00008552static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8553{
8554 struct nfs4_layoutcommit_data *data = calldata;
8555 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008556 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008557
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008558 nfs41_setup_sequence(session,
8559 &data->args.seq_args,
8560 &data->res.seq_res,
8561 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008562}
8563
8564static void
8565nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8566{
8567 struct nfs4_layoutcommit_data *data = calldata;
8568 struct nfs_server *server = NFS_SERVER(data->args.inode);
8569
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008570 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008571 return;
8572
8573 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008574 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8575 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8576 case -NFS4ERR_BADLAYOUT: /* no layout */
8577 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008578 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008579 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008580 break;
8581 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008582 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008583 rpc_restart_call_prepare(task);
8584 return;
8585 }
8586 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008587}
8588
8589static void nfs4_layoutcommit_release(void *calldata)
8590{
8591 struct nfs4_layoutcommit_data *data = calldata;
8592
Andy Adamsondb29c082011-07-30 20:52:38 -04008593 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008594 nfs_post_op_update_inode_force_wcc(data->args.inode,
8595 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008596 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008597 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008598 kfree(data);
8599}
8600
8601static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8602 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8603 .rpc_call_done = nfs4_layoutcommit_done,
8604 .rpc_release = nfs4_layoutcommit_release,
8605};
8606
8607int
Andy Adamsonef311532011-03-12 02:58:10 -05008608nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008609{
8610 struct rpc_message msg = {
8611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8612 .rpc_argp = &data->args,
8613 .rpc_resp = &data->res,
8614 .rpc_cred = data->cred,
8615 };
8616 struct rpc_task_setup task_setup_data = {
8617 .task = &data->task,
8618 .rpc_client = NFS_CLIENT(data->args.inode),
8619 .rpc_message = &msg,
8620 .callback_ops = &nfs4_layoutcommit_ops,
8621 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008622 };
8623 struct rpc_task *task;
8624 int status = 0;
8625
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008626 dprintk("NFS: initiating layoutcommit call. sync %d "
8627 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008628 data->args.lastbytewritten,
8629 data->args.inode->i_ino);
8630
Trond Myklebust472e2592015-02-05 16:50:30 -05008631 if (!sync) {
8632 data->inode = nfs_igrab_and_active(data->args.inode);
8633 if (data->inode == NULL) {
8634 nfs4_layoutcommit_release(data);
8635 return -EAGAIN;
8636 }
8637 task_setup_data.flags = RPC_TASK_ASYNC;
8638 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008639 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008640 task = rpc_run_task(&task_setup_data);
8641 if (IS_ERR(task))
8642 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008643 if (sync)
8644 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008645 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008646 dprintk("%s: status %d\n", __func__, status);
8647 rpc_put_task(task);
8648 return status;
8649}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008650
Andy Adamson97431202013-08-08 10:57:56 -04008651/**
8652 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8653 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8654 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008655static int
8656_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008657 struct nfs_fsinfo *info,
8658 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008659{
8660 struct nfs41_secinfo_no_name_args args = {
8661 .style = SECINFO_STYLE_CURRENT_FH,
8662 };
8663 struct nfs4_secinfo_res res = {
8664 .flavors = flavors,
8665 };
8666 struct rpc_message msg = {
8667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8668 .rpc_argp = &args,
8669 .rpc_resp = &res,
8670 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008671 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008672 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008673 int status;
8674
8675 if (use_integrity) {
8676 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008677 cred = nfs4_get_clid_cred(server->nfs_client);
8678 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008679 }
8680
8681 dprintk("--> %s\n", __func__);
8682 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8683 &res.seq_res, 0);
8684 dprintk("<-- %s status=%d\n", __func__, status);
8685
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008686 if (cred)
8687 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008688
8689 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008690}
8691
8692static int
8693nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8694 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8695{
8696 struct nfs4_exception exception = { };
8697 int err;
8698 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008699 /* first try using integrity protection */
8700 err = -NFS4ERR_WRONGSEC;
8701
8702 /* try to use integrity protection with machine cred */
8703 if (_nfs4_is_integrity_protected(server->nfs_client))
8704 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8705 flavors, true);
8706
8707 /*
8708 * if unable to use integrity protection, or SECINFO with
8709 * integrity protection returns NFS4ERR_WRONGSEC (which is
8710 * disallowed by spec, but exists in deployed servers) use
8711 * the current filesystem's rpc_client and the user cred.
8712 */
8713 if (err == -NFS4ERR_WRONGSEC)
8714 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8715 flavors, false);
8716
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008717 switch (err) {
8718 case 0:
8719 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008720 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008721 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008722 default:
8723 err = nfs4_handle_exception(server, err, &exception);
8724 }
8725 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008726out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008727 return err;
8728}
8729
8730static int
8731nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8732 struct nfs_fsinfo *info)
8733{
8734 int err;
8735 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008736 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008737 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008738 struct nfs4_secinfo4 *secinfo;
8739 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008740
8741 page = alloc_page(GFP_KERNEL);
8742 if (!page) {
8743 err = -ENOMEM;
8744 goto out;
8745 }
8746
8747 flavors = page_address(page);
8748 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8749
8750 /*
8751 * Fall back on "guess and check" method if
8752 * the server doesn't support SECINFO_NO_NAME
8753 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008754 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008755 err = nfs4_find_root_sec(server, fhandle, info);
8756 goto out_freepage;
8757 }
8758 if (err)
8759 goto out_freepage;
8760
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008761 for (i = 0; i < flavors->num_flavors; i++) {
8762 secinfo = &flavors->flavors[i];
8763
8764 switch (secinfo->flavor) {
8765 case RPC_AUTH_NULL:
8766 case RPC_AUTH_UNIX:
8767 case RPC_AUTH_GSS:
8768 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8769 &secinfo->flavor_info);
8770 break;
8771 default:
8772 flavor = RPC_AUTH_MAXFLAVOR;
8773 break;
8774 }
8775
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008776 if (!nfs_auth_info_match(&server->auth_info, flavor))
8777 flavor = RPC_AUTH_MAXFLAVOR;
8778
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008779 if (flavor != RPC_AUTH_MAXFLAVOR) {
8780 err = nfs4_lookup_root_sec(server, fhandle,
8781 info, flavor);
8782 if (!err)
8783 break;
8784 }
8785 }
8786
8787 if (flavor == RPC_AUTH_MAXFLAVOR)
8788 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008789
8790out_freepage:
8791 put_page(page);
8792 if (err == -EACCES)
8793 return -EPERM;
8794out:
8795 return err;
8796}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008797
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008798static int _nfs41_test_stateid(struct nfs_server *server,
8799 nfs4_stateid *stateid,
8800 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008801{
8802 int status;
8803 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008804 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008805 };
8806 struct nfs41_test_stateid_res res;
8807 struct rpc_message msg = {
8808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8809 .rpc_argp = &args,
8810 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008811 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008812 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008813 struct rpc_clnt *rpc_client = server->client;
8814
8815 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8816 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008817
Chuck Lever38527b12012-07-11 16:30:23 -04008818 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008819 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008820 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008821 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008822 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008823 if (status != NFS_OK) {
8824 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008825 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008826 }
8827 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008828 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008829}
8830
Trond Myklebust43912bb2016-09-22 13:38:56 -04008831static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8832 int err, struct nfs4_exception *exception)
8833{
8834 exception->retry = 0;
8835 switch(err) {
8836 case -NFS4ERR_DELAY:
8837 nfs4_handle_exception(server, err, exception);
8838 break;
8839 case -NFS4ERR_BADSESSION:
8840 case -NFS4ERR_BADSLOT:
8841 case -NFS4ERR_BAD_HIGH_SLOT:
8842 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8843 case -NFS4ERR_DEADSESSION:
8844 nfs4_do_handle_exception(server, err, exception);
8845 }
8846}
8847
Chuck Lever38527b12012-07-11 16:30:23 -04008848/**
8849 * nfs41_test_stateid - perform a TEST_STATEID operation
8850 *
8851 * @server: server / transport on which to perform the operation
8852 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008853 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008854 *
8855 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8856 * Otherwise a negative NFS4ERR value is returned if the operation
8857 * failed or the state ID is not currently valid.
8858 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008859static int nfs41_test_stateid(struct nfs_server *server,
8860 nfs4_stateid *stateid,
8861 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008862{
8863 struct nfs4_exception exception = { };
8864 int err;
8865 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008866 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008867 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008868 } while (exception.retry);
8869 return err;
8870}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008871
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008872struct nfs_free_stateid_data {
8873 struct nfs_server *server;
8874 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008875 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008876};
8877
8878static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8879{
8880 struct nfs_free_stateid_data *data = calldata;
8881 nfs41_setup_sequence(nfs4_get_session(data->server),
8882 &data->args.seq_args,
8883 &data->res.seq_res,
8884 task);
8885}
8886
8887static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8888{
8889 struct nfs_free_stateid_data *data = calldata;
8890
8891 nfs41_sequence_done(task, &data->res.seq_res);
8892
8893 switch (task->tk_status) {
8894 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008895 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008896 rpc_restart_call_prepare(task);
8897 }
8898}
8899
8900static void nfs41_free_stateid_release(void *calldata)
8901{
8902 kfree(calldata);
8903}
8904
Trond Myklebust17f26b12013-08-21 15:48:42 -04008905static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008906 .rpc_call_prepare = nfs41_free_stateid_prepare,
8907 .rpc_call_done = nfs41_free_stateid_done,
8908 .rpc_release = nfs41_free_stateid_release,
8909};
8910
8911static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8912 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008913 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008914 bool privileged)
8915{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008916 struct rpc_message msg = {
8917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008918 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008919 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008920 struct rpc_task_setup task_setup = {
8921 .rpc_client = server->client,
8922 .rpc_message = &msg,
8923 .callback_ops = &nfs41_free_stateid_ops,
8924 .flags = RPC_TASK_ASYNC,
8925 };
8926 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008927
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008928 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8929 &task_setup.rpc_client, &msg);
8930
Chuck Lever38527b12012-07-11 16:30:23 -04008931 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008932 data = kmalloc(sizeof(*data), GFP_NOFS);
8933 if (!data)
8934 return ERR_PTR(-ENOMEM);
8935 data->server = server;
8936 nfs4_stateid_copy(&data->args.stateid, stateid);
8937
8938 task_setup.callback_data = data;
8939
8940 msg.rpc_argp = &data->args;
8941 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008942 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008943 if (privileged)
8944 nfs4_set_sequence_privileged(&data->args.seq_args);
8945
8946 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008947}
8948
Chuck Lever38527b12012-07-11 16:30:23 -04008949/**
8950 * nfs41_free_stateid - perform a FREE_STATEID operation
8951 *
8952 * @server: server / transport on which to perform the operation
8953 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008954 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008955 *
8956 * Returns NFS_OK if the server freed "stateid". Otherwise a
8957 * negative NFS4ERR value is returned.
8958 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008959static int nfs41_free_stateid(struct nfs_server *server,
8960 nfs4_stateid *stateid,
8961 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008962{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008963 struct rpc_task *task;
8964 int ret;
8965
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008966 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008967 if (IS_ERR(task))
8968 return PTR_ERR(task);
8969 ret = rpc_wait_for_completion_task(task);
8970 if (!ret)
8971 ret = task->tk_status;
8972 rpc_put_task(task);
8973 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008974}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008975
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008976static void
8977nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008978{
8979 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008980 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008981
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008982 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008983 nfs4_free_lock_state(server, lsp);
8984 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008985 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008986 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008987}
8988
Trond Myklebust36281ca2012-03-04 18:13:56 -05008989static bool nfs41_match_stateid(const nfs4_stateid *s1,
8990 const nfs4_stateid *s2)
8991{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008992 if (s1->type != s2->type)
8993 return false;
8994
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008995 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008996 return false;
8997
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008998 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008999 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009000 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009001 return true;
9002
9003 return false;
9004}
9005
Andy Adamson557134a2009-04-01 09:21:53 -04009006#endif /* CONFIG_NFS_V4_1 */
9007
Trond Myklebust36281ca2012-03-04 18:13:56 -05009008static bool nfs4_match_stateid(const nfs4_stateid *s1,
9009 const nfs4_stateid *s2)
9010{
Trond Myklebustf597c532012-03-04 18:13:56 -05009011 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009012}
9013
9014
Trond Myklebust17280172012-03-11 13:11:00 -04009015static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009016 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009017 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018 .recover_open = nfs4_open_reclaim,
9019 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009020 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009021 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022};
9023
Andy Adamson591d71c2009-04-01 09:22:47 -04009024#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009025static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009026 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9027 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9028 .recover_open = nfs4_open_reclaim,
9029 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009030 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009031 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009032 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009033};
9034#endif /* CONFIG_NFS_V4_1 */
9035
Trond Myklebust17280172012-03-11 13:11:00 -04009036static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009037 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009038 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009039 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009041 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042};
9043
Andy Adamson591d71c2009-04-01 09:22:47 -04009044#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009045static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009046 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9047 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009048 .recover_open = nfs41_open_expired,
9049 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009050 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009051};
9052#endif /* CONFIG_NFS_V4_1 */
9053
Trond Myklebust17280172012-03-11 13:11:00 -04009054static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009055 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009056 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009057 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009058};
9059
9060#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009061static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009062 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009063 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009064 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009065};
9066#endif
9067
Chuck Leverec011fe2013-10-17 14:12:39 -04009068static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009069 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009070 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009071};
9072
9073#if defined(CONFIG_NFS_V4_1)
9074static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009075 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009076 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009077};
9078#endif /* CONFIG_NFS_V4_1 */
9079
Trond Myklebust97dc1352010-06-16 09:52:26 -04009080static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9081 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009082 .init_caps = NFS_CAP_READDIRPLUS
9083 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009084 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009085 .init_client = nfs40_init_client,
9086 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009087 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009088 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009089 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009090 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009091 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009092 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009093 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9094 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9095 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009096 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009097};
9098
9099#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009100static struct nfs_seqid *
9101nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9102{
9103 return NULL;
9104}
9105
Trond Myklebust97dc1352010-06-16 09:52:26 -04009106static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9107 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009108 .init_caps = NFS_CAP_READDIRPLUS
9109 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009110 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009111 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009112 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009113 .init_client = nfs41_init_client,
9114 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009115 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009116 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009117 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009118 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009119 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009120 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009121 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009122 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9123 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9124 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009125 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009126};
9127#endif
9128
Steve Dickson42c2c422013-05-22 12:50:38 -04009129#if defined(CONFIG_NFS_V4_2)
9130static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9131 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009132 .init_caps = NFS_CAP_READDIRPLUS
9133 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009134 | NFS_CAP_POSIX_LOCK
9135 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009136 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009137 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009138 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009139 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009140 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009141 | NFS_CAP_LAYOUTSTATS
9142 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009143 .init_client = nfs41_init_client,
9144 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009145 .match_stateid = nfs41_match_stateid,
9146 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009147 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009148 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009149 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009150 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009151 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009152 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9153 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9154 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009155 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009156};
9157#endif
9158
Trond Myklebust97dc1352010-06-16 09:52:26 -04009159const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9160 [0] = &nfs_v4_0_minor_ops,
9161#if defined(CONFIG_NFS_V4_1)
9162 [1] = &nfs_v4_1_minor_ops,
9163#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009164#if defined(CONFIG_NFS_V4_2)
9165 [2] = &nfs_v4_2_minor_ops,
9166#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009167};
9168
Trond Myklebust13997822016-07-24 17:10:52 -04009169static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009170{
9171 ssize_t error, error2;
9172
9173 error = generic_listxattr(dentry, list, size);
9174 if (error < 0)
9175 return error;
9176 if (list) {
9177 list += error;
9178 size -= error;
9179 }
9180
9181 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9182 if (error2 < 0)
9183 return error2;
9184 return error + error2;
9185}
9186
Trond Myklebust17f26b12013-08-21 15:48:42 -04009187static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009188 .create = nfs_create,
9189 .lookup = nfs_lookup,
9190 .atomic_open = nfs_atomic_open,
9191 .link = nfs_link,
9192 .unlink = nfs_unlink,
9193 .symlink = nfs_symlink,
9194 .mkdir = nfs_mkdir,
9195 .rmdir = nfs_rmdir,
9196 .mknod = nfs_mknod,
9197 .rename = nfs_rename,
9198 .permission = nfs_permission,
9199 .getattr = nfs_getattr,
9200 .setattr = nfs_setattr,
9201 .getxattr = generic_getxattr,
9202 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009203 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009204 .removexattr = generic_removexattr,
9205};
9206
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009207static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009208 .permission = nfs_permission,
9209 .getattr = nfs_getattr,
9210 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009211 .getxattr = generic_getxattr,
9212 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009213 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009214 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009215};
9216
David Howells509de812006-08-22 20:06:11 -04009217const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 .version = 4, /* protocol version */
9219 .dentry_ops = &nfs4_dentry_operations,
9220 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009221 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009222 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009224 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009225 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 .getattr = nfs4_proc_getattr,
9227 .setattr = nfs4_proc_setattr,
9228 .lookup = nfs4_proc_lookup,
9229 .access = nfs4_proc_access,
9230 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 .create = nfs4_proc_create,
9232 .remove = nfs4_proc_remove,
9233 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009234 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009236 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009237 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009238 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239 .link = nfs4_proc_link,
9240 .symlink = nfs4_proc_symlink,
9241 .mkdir = nfs4_proc_mkdir,
9242 .rmdir = nfs4_proc_remove,
9243 .readdir = nfs4_proc_readdir,
9244 .mknod = nfs4_proc_mknod,
9245 .statfs = nfs4_proc_statfs,
9246 .fsinfo = nfs4_proc_fsinfo,
9247 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009248 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009250 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009252 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009254 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009256 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009257 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009259 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009260 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009261 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009262 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009263 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009264 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009265 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009266 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009267 .create_server = nfs4_create_server,
9268 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269};
9270
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009271static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009272 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009273 .list = nfs4_xattr_list_nfs4_acl,
9274 .get = nfs4_xattr_get_nfs4_acl,
9275 .set = nfs4_xattr_set_nfs4_acl,
9276};
9277
9278const struct xattr_handler *nfs4_xattr_handlers[] = {
9279 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009280#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9281 &nfs4_xattr_nfs4_label_handler,
9282#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009283 NULL
9284};
9285
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286/*
9287 * Local variables:
9288 * c-basic-offset: 8
9289 * End:
9290 */