blob: 04baee4327b2287319e52f621d2dbd0bfc0f4eb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400366 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500368 int ret = errorcode;
369
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400370 exception->delay = 0;
371 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 exception->retry = 0;
373 switch(errorcode) {
374 case 0:
375 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500376 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400380 if (inode) {
381 int err;
382
383 err = nfs_async_inode_return_delegation(inode,
384 stateid);
385 if (err == 0)
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
389 break;
390 }
391 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 if (state == NULL)
393 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400394 ret = nfs4_schedule_stateid_recovery(server, state);
395 if (ret < 0)
396 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400398 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400399 if (state != NULL) {
400 ret = nfs4_schedule_stateid_recovery(server, state);
401 if (ret < 0)
402 break;
403 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500405 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400408 case -NFS4ERR_MOVED:
409 ret = nfs4_schedule_migration_recovery(server);
410 if (ret < 0)
411 break;
412 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500416#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
425 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400427 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500428#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
432 * fail
433 */
434 ret = -EBUSY;
435 break;
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400438 nfs_inc_server_stats(server, NFSIOS_DELAY);
439 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400440 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400441 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400442 exception->delay = 1;
443 return 0;
444
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800448 break;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
457 "uid/gids. "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
460 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 }
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500464wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->recovering = 1;
466 return 0;
467}
468
469/* This is the error handling routine for processes that are allowed
470 * to sleep.
471 */
472int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
473{
474 struct nfs_client *clp = server->nfs_client;
475 int ret;
476
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
480 goto out_retry;
481 }
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
485 return -EIO;
486 goto out_retry;
487 }
488 return ret;
489out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500490 if (ret == 0)
491 exception->retry = 1;
492 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500493}
494
Trond Myklebust037fc982015-09-20 15:51:00 -0400495static int
496nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
498{
499 struct nfs_client *clp = server->nfs_client;
500 int ret;
501
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
505 goto out_retry;
506 }
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
511 goto out_retry;
512 }
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 ret = -EIO;
515 return ret;
516out_retry:
517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
520}
521
522static int
523nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
525{
526 struct nfs4_exception exception = {
527 .state = state,
528 };
529
530 if (task->tk_status >= 0)
531 return 0;
532 if (timeout)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
535 task->tk_status,
536 &exception);
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
539 if (exception.retry)
540 return -EAGAIN;
541 return 0;
542}
543
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400544/*
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
547 */
548static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
549{
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
551
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
554 return true;
555
556 return false;
557}
Trond Myklebust65de8722008-12-23 15:21:44 -0500558
Trond Myklebust452e9352010-07-31 14:29:06 -0400559static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
565}
566
Trond Myklebust452e9352010-07-31 14:29:06 -0400567static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
568{
Trond Myklebustbe824162015-07-05 14:50:46 -0400569 struct nfs_client *clp = server->nfs_client;
570
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400573}
574
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400575struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
579};
580
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800581void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400583{
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
587
588 res->sr_slot = NULL;
589}
590
591static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
592{
593 args->sa_privileged = 1;
594}
595
Peng Taocb04ad22014-06-11 05:24:15 +0800596int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400600{
Chuck Lever3bd23842013-08-09 12:49:19 -0400601 struct nfs4_slot *slot;
602
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
605 goto out_start;
606
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
609 goto out_sleep;
610
611 slot = nfs4_alloc_slot(tbl);
612 if (IS_ERR(slot)) {
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
615 goto out_sleep;
616 }
617 spin_unlock(&tbl->slot_tbl_lock);
618
Trond Myklebust0a014a42016-09-22 13:38:51 -0400619 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400620 args->sa_slot = slot;
621 res->sr_slot = slot;
622
623out_start:
624 rpc_call_start(task);
625 return 0;
626
627out_sleep:
628 if (args->sa_privileged)
629 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
630 NULL, RPC_PRIORITY_PRIVILEGED);
631 else
632 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
633 spin_unlock(&tbl->slot_tbl_lock);
634 return -EAGAIN;
635}
Peng Taocb04ad22014-06-11 05:24:15 +0800636EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400637
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400638static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400639{
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
642
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 tbl = slot->table;
644 spin_lock(&tbl->slot_tbl_lock);
645 if (!nfs41_wake_and_assign_slot(tbl, slot))
646 nfs4_free_slot(tbl, slot);
647 spin_unlock(&tbl->slot_tbl_lock);
648
649 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400650}
651
652static int nfs40_sequence_done(struct rpc_task *task,
653 struct nfs4_sequence_res *res)
654{
655 if (res->sr_slot != NULL)
656 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400657 return 1;
658}
659
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400660#if defined(CONFIG_NFS_V4_1)
661
Trond Myklebustd185a332010-06-16 09:52:25 -0400662static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400663{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500664 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400665 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500667 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400668
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500669 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500670 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500671
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400672 /* Bump the slot sequence number */
673 if (slot->seq_done)
674 slot->seq_nr++;
675 slot->seq_done = 0;
676
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500677 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500678 /* Be nice to the server: try to ensure that the last transmitted
679 * value for highest_user_slotid <= target_highest_slotid
680 */
681 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
682 send_new_highest_used_slotid = true;
683
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500684 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500685 send_new_highest_used_slotid = false;
686 goto out_unlock;
687 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500688 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500689
690 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
691 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500692out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500693 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500695 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400696 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400697 if (waitqueue_active(&tbl->slot_waitq))
698 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400699}
700
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400701static int nfs41_sequence_process(struct rpc_task *task,
702 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400706 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500707 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500708 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400709
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500710 if (slot == NULL)
711 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400712 /* don't increment the sequence number if the task wasn't sent */
713 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400714 goto out;
715
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500716 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500717
Trond Myklebustac20d162012-12-15 15:36:07 -0500718 if (slot->interrupted) {
719 slot->interrupted = 0;
720 interrupted = true;
721 }
722
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400723 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500724 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 switch (res->sr_status) {
726 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400727 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400728 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500729 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500730 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500731 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400732 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
733 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500734 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500736 case 1:
737 /*
738 * sr_status remains 1 if an RPC level error occurred.
739 * The server may or may not have processed the sequence
740 * operation..
741 * Mark the slot as having hosted an interrupted RPC call.
742 */
743 slot->interrupted = 1;
744 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400745 case -NFS4ERR_DELAY:
746 /* The server detected a resend of the RPC call and
747 * returned NFS4ERR_DELAY as per Section 2.10.6.2
748 * of RFC5661.
749 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500750 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400751 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500752 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500753 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400754 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500755 case -NFS4ERR_BADSLOT:
756 /*
757 * The slot id we used was probably retired. Try again
758 * using a different slot id.
759 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500760 goto retry_nowait;
761 case -NFS4ERR_SEQ_MISORDERED:
762 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 * Was the last operation on this sequence interrupted?
764 * If so, retry after bumping the sequence number.
765 */
766 if (interrupted) {
767 ++slot->seq_nr;
768 goto retry_nowait;
769 }
770 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500771 * Could this slot have been previously retired?
772 * If so, then the server may be expecting seq_nr = 1!
773 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500774 if (slot->seq_nr != 1) {
775 slot->seq_nr = 1;
776 goto retry_nowait;
777 }
778 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500779 case -NFS4ERR_SEQ_FALSE_RETRY:
780 ++slot->seq_nr;
781 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 default:
783 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400784 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400785 }
786out:
787 /* The session may be reset by one of the error handlers. */
788 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500789out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500790 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500791retry_nowait:
792 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 task->tk_status = 0;
795 ret = 0;
796 }
797 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400799 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 goto out;
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
802 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400804
805int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (!nfs41_sequence_process(task, res))
808 return 0;
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
811 return 1;
812
813}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500814EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400815
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400816static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817{
818 if (res->sr_slot == NULL)
819 return 1;
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
823}
824
825static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826{
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
830 else
831 nfs40_sequence_free_slot(res);
832 }
833}
834
Peng Tao2c4b1312014-06-11 05:24:16 +0800835int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400836{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500837 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842}
Peng Tao2c4b1312014-06-11 05:24:16 +0800843EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000845int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846 struct nfs4_sequence_args *args,
847 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848 struct rpc_task *task)
849{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400850 struct nfs4_slot *slot;
851 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400852
853 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400855 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400856 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400857
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400858 tbl = &session->fc_slot_table;
859
Trond Myklebust69d206b2012-11-22 13:21:02 -0500860 task->tk_timeout = 0;
861
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400862 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400863 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400864 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500865 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500866 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400867 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400868 }
869
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500870 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500871 if (IS_ERR(slot)) {
872 /* If out of memory, try again in 1/4 second */
873 if (slot == ERR_PTR(-ENOMEM))
874 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400875 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400876 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400877 }
878 spin_unlock(&tbl->slot_tbl_lock);
879
Trond Myklebust0a014a42016-09-22 13:38:51 -0400880 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500881 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400882
Chuck Levere8d92382013-08-09 12:47:51 -0400883 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500884 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400885
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500887 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400888 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 /*
890 * sr_status is only set in decode_sequence, and so will remain
891 * set to 1 if an rpc level failure occurs.
892 */
893 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400894 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400895out_success:
896 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400899 /* Privileged tasks are queued with top priority */
900 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400901 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
902 NULL, RPC_PRIORITY_PRIVILEGED);
903 else
904 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400905 spin_unlock(&tbl->slot_tbl_lock);
906 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000908EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400909
Chuck Lever5a580e02013-08-09 12:48:09 -0400910static int nfs4_setup_sequence(const struct nfs_server *server,
911 struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400914{
Trond Myklebust035168a2010-06-16 09:52:26 -0400915 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400916 int ret = 0;
917
Chuck Lever3bd23842013-08-09 12:49:19 -0400918 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
920 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400921
Chuck Levere8d92382013-08-09 12:47:51 -0400922 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400923 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400924 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400925
Trond Myklebust9d12b212012-01-17 22:04:25 -0500926 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400927
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 dprintk("<-- %s status=%d\n", __func__, ret);
929 return ret;
930}
931
Andy Adamsonce5039c2009-04-01 09:22:13 -0400932static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
933{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400934 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400935 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400936
Trond Myklebust035168a2010-06-16 09:52:26 -0400937 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
938
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400939 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940}
941
Andy Adamson69ab40c2009-04-01 09:22:19 -0400942static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
943{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400944 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400945
Trond Myklebust14516c32010-07-31 14:29:06 -0400946 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400947}
948
Trond Myklebust17280172012-03-11 13:11:00 -0400949static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400951 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400952};
953
Chuck Lever3bd23842013-08-09 12:49:19 -0400954#else /* !CONFIG_NFS_V4_1 */
955
Chuck Lever5a580e02013-08-09 12:48:09 -0400956static int nfs4_setup_sequence(const struct nfs_server *server,
957 struct nfs4_sequence_args *args,
958 struct nfs4_sequence_res *res,
959 struct rpc_task *task)
960{
Peng Taocb04ad22014-06-11 05:24:15 +0800961 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
962 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400963}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400964
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400965static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
966{
967 return nfs40_sequence_done(task, res);
968}
969
970static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
971{
972 if (res->sr_slot != NULL)
973 nfs40_sequence_free_slot(res);
974}
975
Peng Tao2c4b1312014-06-11 05:24:16 +0800976int nfs4_sequence_done(struct rpc_task *task,
977 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400978{
Chuck Lever3bd23842013-08-09 12:49:19 -0400979 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400980}
Peng Tao2c4b1312014-06-11 05:24:16 +0800981EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400982
983#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400984
Chuck Lever9915ea72013-08-09 12:48:27 -0400985static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
986{
987 struct nfs4_call_sync_data *data = calldata;
988 nfs4_setup_sequence(data->seq_server,
989 data->seq_args, data->seq_res, task);
990}
991
992static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
993{
994 struct nfs4_call_sync_data *data = calldata;
995 nfs4_sequence_done(task, data->seq_res);
996}
997
998static const struct rpc_call_ops nfs40_call_sync_ops = {
999 .rpc_call_prepare = nfs40_call_sync_prepare,
1000 .rpc_call_done = nfs40_call_sync_done,
1001};
1002
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001003static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001004 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs4_sequence_args *args,
1007 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001008{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 int ret;
1010 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001011 struct nfs_client *clp = server->nfs_client;
1012 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001013 .seq_server = server,
1014 .seq_args = args,
1015 .seq_res = res,
1016 };
1017 struct rpc_task_setup task_setup = {
1018 .rpc_client = clnt,
1019 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001020 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001021 .callback_data = &data
1022 };
1023
1024 task = rpc_run_task(&task_setup);
1025 if (IS_ERR(task))
1026 ret = PTR_ERR(task);
1027 else {
1028 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001029 rpc_put_task(task);
1030 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001031 return ret;
1032}
1033
Bryan Schumaker7c513052011-03-24 17:12:24 +00001034int nfs4_call_sync(struct rpc_clnt *clnt,
1035 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001036 struct rpc_message *msg,
1037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 int cache_reply)
1040{
Chuck Levera9c92d62013-08-09 12:48:18 -04001041 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1046{
1047 struct nfs_inode *nfsi = NFS_I(dir);
1048
1049 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001051 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001053 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001054 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001055 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 spin_unlock(&dir->i_lock);
1057}
1058
1059struct nfs4_opendata {
1060 struct kref kref;
1061 struct nfs_openargs o_arg;
1062 struct nfs_openres o_res;
1063 struct nfs_open_confirmargs c_arg;
1064 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 struct nfs4_string owner_name;
1066 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001067 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001069 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001071 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct nfs4_state_owner *owner;
1073 struct nfs4_state *state;
1074 struct iattr attrs;
1075 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001076 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001077 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001078 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001079 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int cancelled;
1081};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001082
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001083static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1084 int err, struct nfs4_exception *exception)
1085{
1086 if (err != -EINVAL)
1087 return false;
1088 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1089 return false;
1090 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1091 exception->retry = 1;
1092 return true;
1093}
1094
Trond Myklebust6ae37332015-01-30 14:21:14 -05001095static u32
1096nfs4_map_atomic_open_share(struct nfs_server *server,
1097 fmode_t fmode, int openflags)
1098{
1099 u32 res = 0;
1100
1101 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1102 case FMODE_READ:
1103 res = NFS4_SHARE_ACCESS_READ;
1104 break;
1105 case FMODE_WRITE:
1106 res = NFS4_SHARE_ACCESS_WRITE;
1107 break;
1108 case FMODE_READ|FMODE_WRITE:
1109 res = NFS4_SHARE_ACCESS_BOTH;
1110 }
1111 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1112 goto out;
1113 /* Want no delegation if we're using O_DIRECT */
1114 if (openflags & O_DIRECT)
1115 res |= NFS4_SHARE_WANT_NO_DELEG;
1116out:
1117 return res;
1118}
1119
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001120static enum open_claim_type4
1121nfs4_map_atomic_open_claim(struct nfs_server *server,
1122 enum open_claim_type4 claim)
1123{
1124 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1125 return claim;
1126 switch (claim) {
1127 default:
1128 return claim;
1129 case NFS4_OPEN_CLAIM_FH:
1130 return NFS4_OPEN_CLAIM_NULL;
1131 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1132 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1133 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1134 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1135 }
1136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139{
1140 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001141 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001142 p->o_res.seqid = p->o_arg.seqid;
1143 p->c_res.seqid = p->c_arg.seqid;
1144 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001145 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001147 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001148}
1149
Al Viro82a2c1b2011-06-22 18:30:55 -04001150static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001151 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001152 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001153 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001154 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001155 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156{
Al Viro82a2c1b2011-06-22 18:30:55 -04001157 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001158 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001160 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 struct nfs4_opendata *p;
1162
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001163 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001164 if (p == NULL)
1165 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001166
1167 p->f_label = nfs4_label_alloc(server, gfp_mask);
1168 if (IS_ERR(p->f_label))
1169 goto err_free_p;
1170
Kinglong Meea49c2692015-07-27 15:31:38 +08001171 p->a_label = nfs4_label_alloc(server, gfp_mask);
1172 if (IS_ERR(p->a_label))
1173 goto err_free_f;
1174
Trond Myklebust63f5f792015-01-23 19:19:25 -05001175 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1176 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001177 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001178 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001179 nfs_sb_active(dentry->d_sb);
1180 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001181 p->dir = parent;
1182 p->owner = sp;
1183 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 p->o_arg.open_flags = flags;
1185 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001186 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1187 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001188 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1189 * will return permission denied for all bits until close */
1190 if (!(flags & O_EXCL)) {
1191 /* ask server to check for all possible rights as results
1192 * are cached */
1193 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1194 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001195 }
David Howells7539bba2006-08-22 20:06:09 -04001196 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001197 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1198 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001199 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001201 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001202 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001203 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001204 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1205 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001206 case NFS4_OPEN_CLAIM_NULL:
1207 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1208 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1209 p->o_arg.fh = NFS_FH(dir);
1210 break;
1211 case NFS4_OPEN_CLAIM_PREVIOUS:
1212 case NFS4_OPEN_CLAIM_FH:
1213 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1214 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001215 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001216 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001217 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001218 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001219
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 p->o_arg.u.attrs = &p->attrs;
1221 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001222
1223 verf[0] = jiffies;
1224 verf[1] = current->pid;
1225 memcpy(p->o_arg.u.verifier.data, verf,
1226 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001228 p->c_arg.fh = &p->o_res.fh;
1229 p->c_arg.stateid = &p->o_res.stateid;
1230 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001232 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001234
1235err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001236 nfs4_label_free(p->a_label);
1237err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001238 nfs4_label_free(p->f_label);
1239err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001240 kfree(p);
1241err:
1242 dput(parent);
1243 return NULL;
1244}
1245
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001246static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 struct nfs4_opendata *p = container_of(kref,
1249 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001250 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251
1252 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001253 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001254 if (p->state != NULL)
1255 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001256 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001257
Kinglong Meea49c2692015-07-27 15:31:38 +08001258 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001259 nfs4_label_free(p->f_label);
1260
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001261 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001262 dput(p->dentry);
1263 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001264 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001265 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001266 kfree(p);
1267}
1268
1269static void nfs4_opendata_put(struct nfs4_opendata *p)
1270{
1271 if (p != NULL)
1272 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001273}
1274
Trond Myklebust06f814a2006-01-03 09:55:07 +01001275static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1276{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001277 int ret;
1278
Trond Myklebust06f814a2006-01-03 09:55:07 +01001279 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001280 return ret;
1281}
1282
Trond Myklebust24311f82015-09-20 10:50:17 -04001283static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1284 fmode_t fmode)
1285{
1286 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1287 case FMODE_READ|FMODE_WRITE:
1288 return state->n_rdwr != 0;
1289 case FMODE_WRITE:
1290 return state->n_wronly != 0;
1291 case FMODE_READ:
1292 return state->n_rdonly != 0;
1293 }
1294 WARN_ON_ONCE(1);
1295 return false;
1296}
1297
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001299{
1300 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301
Trond Myklebust536e43d2012-01-17 22:04:26 -05001302 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001303 goto out;
1304 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001306 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1307 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308 break;
1309 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001310 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1311 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001312 break;
1313 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001314 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1315 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001318 return ret;
1319}
1320
Trond Myklebust2a606182015-08-19 22:30:00 -05001321static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1322 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001323{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001324 if (delegation == NULL)
1325 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001327 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001328 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1329 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001330 switch (claim) {
1331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_FH:
1333 break;
1334 case NFS4_OPEN_CLAIM_PREVIOUS:
1335 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1336 break;
1337 default:
1338 return 0;
1339 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001340 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 return 1;
1342}
1343
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001345{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001347 case FMODE_WRITE:
1348 state->n_wronly++;
1349 break;
1350 case FMODE_READ:
1351 state->n_rdonly++;
1352 break;
1353 case FMODE_READ|FMODE_WRITE:
1354 state->n_rdwr++;
1355 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001357}
1358
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001360{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 struct nfs_client *clp = state->owner->so_server->nfs_client;
1362 bool need_recover = false;
1363
1364 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1365 need_recover = true;
1366 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1367 need_recover = true;
1368 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1369 need_recover = true;
1370 if (need_recover)
1371 nfs4_state_mark_reclaim_nograce(clp, state);
1372}
1373
Trond Myklebuste999e802014-02-10 18:20:47 -05001374static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1375 nfs4_stateid *stateid)
1376{
1377 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1378 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001379 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1380 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001381 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001382 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001383 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1384 return true;
1385 return false;
1386}
1387
Trond Myklebustf95549c2015-01-23 18:06:09 -05001388static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1389{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001390 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1391 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001392 if (state->n_wronly)
1393 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 if (state->n_rdonly)
1395 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1396 if (state->n_rdwr)
1397 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001398 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001399}
1400
Trond Myklebust226056c2014-02-11 10:41:07 -05001401static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001402 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001403 nfs4_stateid *stateid, fmode_t fmode)
1404{
1405 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1406 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1407 case FMODE_WRITE:
1408 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1409 break;
1410 case FMODE_READ:
1411 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1412 break;
1413 case 0:
1414 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1415 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1416 clear_bit(NFS_OPEN_STATE, &state->flags);
1417 }
1418 if (stateid == NULL)
1419 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001420 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001421 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1422 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1423 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001424 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001425 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001426 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001428 nfs4_stateid_copy(&state->stateid, stateid);
1429 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001430}
1431
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001432static void nfs_clear_open_stateid(struct nfs4_state *state,
1433 nfs4_stateid *arg_stateid,
1434 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001435{
1436 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001437 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001438 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001439 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1440 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001441}
1442
Trond Myklebust003707c2007-07-05 18:07:55 -04001443static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1444{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001446 case FMODE_READ:
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1448 break;
1449 case FMODE_WRITE:
1450 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1451 break;
1452 case FMODE_READ|FMODE_WRITE:
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
1454 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001455 if (!nfs_need_update_open_stateid(state, stateid))
1456 return;
1457 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1458 nfs4_stateid_copy(&state->stateid, stateid);
1459 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001460}
1461
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001464 /*
1465 * Protect the call to nfs4_state_set_mode_locked and
1466 * serialise the stateid update
1467 */
Andrew Elble361cad32015-12-02 09:20:57 -05001468 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001469 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001470 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001471 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001472 set_bit(NFS_DELEGATED_STATE, &state->flags);
1473 }
1474 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001476 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001478 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001481static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001482{
1483 struct nfs_inode *nfsi = NFS_I(state->inode);
1484 struct nfs_delegation *deleg_cur;
1485 int ret = 0;
1486
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001488
1489 rcu_read_lock();
1490 deleg_cur = rcu_dereference(nfsi->delegation);
1491 if (deleg_cur == NULL)
1492 goto no_delegation;
1493
1494 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001495 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001496 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001497 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001498 goto no_delegation_unlock;
1499
1500 if (delegation == NULL)
1501 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001502 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001503 goto no_delegation_unlock;
1504
Trond Myklebustb7391f42008-12-23 15:21:52 -05001505 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001506 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508no_delegation_unlock:
1509 spin_unlock(&deleg_cur->lock);
1510no_delegation:
1511 rcu_read_unlock();
1512
1513 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001514 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001515 ret = 1;
1516 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001517 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1518 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001519
1520 return ret;
1521}
1522
Trond Myklebust39071e62015-01-24 15:07:56 -05001523static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1524 const nfs4_stateid *stateid)
1525{
1526 struct nfs4_state *state = lsp->ls_state;
1527 bool ret = false;
1528
1529 spin_lock(&state->state_lock);
1530 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1531 goto out_noupdate;
1532 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1533 goto out_noupdate;
1534 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1535 ret = true;
1536out_noupdate:
1537 spin_unlock(&state->state_lock);
1538 return ret;
1539}
Trond Myklebust34310432008-12-23 15:21:38 -05001540
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542{
1543 struct nfs_delegation *delegation;
1544
1545 rcu_read_lock();
1546 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 rcu_read_unlock();
1549 return;
1550 }
1551 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001552 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553}
1554
Trond Myklebust6ee41262007-07-08 14:11:36 -04001555static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001556{
1557 struct nfs4_state *state = opendata->state;
1558 struct nfs_inode *nfsi = NFS_I(state->inode);
1559 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001560 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001562 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 nfs4_stateid stateid;
1564 int ret = -EAGAIN;
1565
Trond Myklebustaac00a82007-07-05 19:02:21 -04001566 for (;;) {
Anna 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 Myklebust035168a2010-06-16 09:52:26 -04002087 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002088 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002089 &data->o_res.seq_res,
2090 task) != 0)
2091 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002092
2093 /* Set the create mode (note dependency on the session type) */
2094 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2095 if (data->o_arg.open_flags & O_EXCL) {
2096 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2097 if (nfs4_has_persistent_session(clp))
2098 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2099 else if (clp->cl_mvops->minor_version > 0)
2100 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2101 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002102 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002103unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002104 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002105 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002106out_no_action:
2107 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002108out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002109 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002110}
2111
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112static void nfs4_open_done(struct rpc_task *task, void *calldata)
2113{
2114 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002117
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002118 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002119 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002120
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002122 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2123 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002124 case S_IFREG:
2125 break;
2126 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128 break;
2129 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002131 break;
2132 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002134 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002135 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002136 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002137 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2138 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002139 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002140 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002141}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002142
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143static void nfs4_open_release(void *calldata)
2144{
2145 struct nfs4_opendata *data = calldata;
2146 struct nfs4_state *state = NULL;
2147
2148 /* If this request hasn't been cancelled, do nothing */
2149 if (data->cancelled == 0)
2150 goto out_free;
2151 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002152 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002153 goto out_free;
2154 /* In case we need an open_confirm, no cleanup! */
2155 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2156 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002158 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002159 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002161 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002162}
2163
2164static const struct rpc_call_ops nfs4_open_ops = {
2165 .rpc_call_prepare = nfs4_open_prepare,
2166 .rpc_call_done = nfs4_open_done,
2167 .rpc_release = nfs4_open_release,
2168};
2169
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002170static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002171{
David Howells2b0143b2015-03-17 22:25:59 +00002172 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002173 struct nfs_server *server = NFS_SERVER(dir);
2174 struct nfs_openargs *o_arg = &data->o_arg;
2175 struct nfs_openres *o_res = &data->o_res;
2176 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2179 .rpc_argp = o_arg,
2180 .rpc_resp = o_res,
2181 .rpc_cred = data->owner->so_cred,
2182 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 struct rpc_task_setup task_setup_data = {
2184 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 .callback_ops = &nfs4_open_ops,
2187 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002188 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 .flags = RPC_TASK_ASYNC,
2190 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 int status;
2192
Chuck Levera9c92d62013-08-09 12:48:18 -04002193 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002194 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002195 data->rpc_done = 0;
2196 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002197 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002198 data->is_recover = 0;
2199 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002200 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002201 data->is_recover = 1;
2202 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002203 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002204 if (IS_ERR(task))
2205 return PTR_ERR(task);
2206 status = nfs4_wait_for_completion_rpc_task(task);
2207 if (status != 0) {
2208 data->cancelled = 1;
2209 smp_wmb();
2210 } else
2211 status = data->rpc_status;
2212 rpc_put_task(task);
2213
2214 return status;
2215}
2216
2217static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2218{
David Howells2b0143b2015-03-17 22:25:59 +00002219 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 struct nfs_openres *o_res = &data->o_res;
2221 int status;
2222
2223 status = nfs4_run_open_task(data, 1);
2224 if (status != 0 || !data->rpc_done)
2225 return status;
2226
Trond Myklebust6926afd2012-01-07 13:22:46 -05002227 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2228
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002229 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2230 status = _nfs4_proc_open_confirm(data);
2231 if (status != 0)
2232 return status;
2233 }
2234
2235 return status;
2236}
2237
Trond Myklebustf3792d62014-07-10 08:54:32 -04002238/*
2239 * Additional permission checks in order to distinguish between an
2240 * open for read, and an open for execute. This works around the
2241 * fact that NFSv4 OPEN treats read and execute permissions as being
2242 * the same.
2243 * Note that in the non-execute case, we want to turn off permission
2244 * checking if we just created a new file (POSIX open() semantics).
2245 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002246static int nfs4_opendata_access(struct rpc_cred *cred,
2247 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002248 struct nfs4_state *state, fmode_t fmode,
2249 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002250{
2251 struct nfs_access_entry cache;
2252 u32 mask;
2253
2254 /* access call failed or for some reason the server doesn't
2255 * support any access modes -- defer access call until later */
2256 if (opendata->o_res.access_supported == 0)
2257 return 0;
2258
2259 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002260 /*
2261 * Use openflags to check for exec, because fmode won't
2262 * always have FMODE_EXEC set when file open for exec.
2263 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002264 if (openflags & __FMODE_EXEC) {
2265 /* ONLY check for exec rights */
2266 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002267 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002268 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002269
2270 cache.cred = cred;
2271 cache.jiffies = jiffies;
2272 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2273 nfs_access_add_cache(state->inode, &cache);
2274
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002275 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002276 return 0;
2277
2278 /* even though OPEN succeeded, access is denied. Close the file */
2279 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002280 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002281}
2282
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002283/*
2284 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2285 */
2286static int _nfs4_proc_open(struct nfs4_opendata *data)
2287{
David Howells2b0143b2015-03-17 22:25:59 +00002288 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002289 struct nfs_server *server = NFS_SERVER(dir);
2290 struct nfs_openargs *o_arg = &data->o_arg;
2291 struct nfs_openres *o_res = &data->o_res;
2292 int status;
2293
2294 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002295 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002296 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002297 if (status != 0) {
2298 if (status == -NFS4ERR_BADNAME &&
2299 !(o_arg->open_flags & O_CREAT))
2300 return -ENOENT;
2301 return status;
2302 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303
Trond Myklebust6926afd2012-01-07 13:22:46 -05002304 nfs_fattr_map_and_free_names(server, &data->f_attr);
2305
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002306 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002307 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002308 if (o_arg->open_flags & O_EXCL)
2309 data->file_created = 1;
2310 else if (o_res->cinfo.before != o_res->cinfo.after)
2311 data->file_created = 1;
2312 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002313 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2314 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002321 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Andy Adamsond83217c2011-03-01 01:34:17 +00002325static int nfs4_recover_expired_lease(struct nfs_server *server)
2326{
2327 return nfs4_client_recover_expired_lease(server->nfs_client);
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/*
2331 * OPEN_EXPIRED:
2332 * reclaim state on the server after a network partition.
2333 * Assumes caller holds the appropriate lock
2334 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002335static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002337 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002338 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002340 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002341 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002342 if (IS_ERR(opendata))
2343 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002344 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002345 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002346 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002347 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002351static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002352{
Trond Myklebust539cd032007-06-05 11:46:42 -04002353 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002354 struct nfs4_exception exception = { };
2355 int err;
2356
2357 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002358 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002359 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2361 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002362 switch (err) {
2363 default:
2364 goto out;
2365 case -NFS4ERR_GRACE:
2366 case -NFS4ERR_DELAY:
2367 nfs4_handle_exception(server, err, &exception);
2368 err = 0;
2369 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002370 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002371out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002372 return err;
2373}
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Trond Myklebust864472e2006-01-03 09:55:15 +01002380 ctx = nfs4_state_find_open_context(state);
2381 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002382 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002383 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002384 put_nfs_open_context(ctx);
2385 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002388static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2389{
2390 nfs_remove_bad_delegation(state->inode);
2391 write_seqlock(&state->seqlock);
2392 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2393 write_sequnlock(&state->seqlock);
2394 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2395}
2396
2397static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2398{
2399 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2400 nfs_finish_clear_delegation_stateid(state);
2401}
2402
2403static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2404{
2405 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2406 nfs40_clear_delegation_stateid(state);
2407 return nfs4_open_expired(sp, state);
2408}
2409
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002410#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002411static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2412 nfs4_stateid *stateid,
2413 struct rpc_cred *cred)
2414{
2415 int status;
2416
2417 status = nfs41_test_stateid(server, stateid, cred);
2418
2419 switch (status) {
2420 case -NFS4ERR_EXPIRED:
2421 case -NFS4ERR_ADMIN_REVOKED:
2422 case -NFS4ERR_DELEG_REVOKED:
2423 /* Ack the revoked state to the server */
2424 nfs41_free_stateid(server, stateid, cred);
2425 case -NFS4ERR_BAD_STATEID:
2426 return status;
2427 }
2428 return NFS_OK;
2429}
2430
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002431static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002432{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002433 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002435 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002436 struct rpc_cred *cred;
2437 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002438
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002439 /* Get the delegation credential for use by test/free_stateid */
2440 rcu_read_lock();
2441 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002442 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002443 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002444 return;
2445 }
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002446 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2447 rcu_read_unlock();
2448 nfs_finish_clear_delegation_stateid(state);
2449 return;
2450 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002451
2452 nfs4_stateid_copy(&stateid, &delegation->stateid);
2453 cred = get_rpccred(delegation->cred);
2454 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002455 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002456 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002457 if (status != NFS_OK)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002458 nfs_finish_clear_delegation_stateid(state);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002459
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002460 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002461}
2462
2463/**
2464 * nfs41_check_open_stateid - possibly free an open stateid
2465 *
2466 * @state: NFSv4 state for an inode
2467 *
2468 * Returns NFS_OK if recovery for this stateid is now finished.
2469 * Otherwise a negative NFS4ERR value is returned.
2470 */
2471static int nfs41_check_open_stateid(struct nfs4_state *state)
2472{
2473 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002474 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002475 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002476 int status;
2477
2478 /* If a state reset has been done, test_stateid is unneeded */
2479 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2480 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2481 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2482 return -NFS4ERR_BAD_STATEID;
2483
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002484 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002485 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002486 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002487 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2488 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2489 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002490 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002491 }
2492 return status;
2493}
2494
2495static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2496{
Chuck Levereb64cf92012-07-11 16:30:05 -04002497 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002498
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002499 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002500 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002501 if (status != NFS_OK)
2502 status = nfs4_open_expired(sp, state);
2503 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002504}
2505#endif
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002508 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2509 * fields corresponding to attributes that were used to store the verifier.
2510 * Make sure we clobber those fields in the later setattr call
2511 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002512static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2513 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002514{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002515 const u32 *attrset = opendata->o_res.attrset;
2516
2517 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002518 !(sattr->ia_valid & ATTR_ATIME_SET))
2519 sattr->ia_valid |= ATTR_ATIME;
2520
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002521 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002522 !(sattr->ia_valid & ATTR_MTIME_SET))
2523 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002524
2525 /* Except MODE, it seems harmless of setting twice. */
2526 if ((attrset[1] & FATTR4_WORD1_MODE))
2527 sattr->ia_valid &= ~ATTR_MODE;
2528
2529 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2530 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002531}
2532
Trond Myklebustc21443c2013-02-07 14:26:21 -05002533static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2534 fmode_t fmode,
2535 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002536 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002537{
2538 struct nfs4_state_owner *sp = opendata->owner;
2539 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002540 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002541 struct nfs4_state *state;
2542 unsigned int seq;
2543 int ret;
2544
2545 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2546
2547 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002548 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002549 goto out;
2550
2551 state = nfs4_opendata_to_nfs4_state(opendata);
2552 ret = PTR_ERR(state);
2553 if (IS_ERR(state))
2554 goto out;
2555 if (server->caps & NFS_CAP_POSIX_LOCK)
2556 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002557 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2558 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002559
Trond Myklebust275bb302013-05-29 13:11:28 -04002560 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002561 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002562 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002563 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002564 alias = d_exact_alias(dentry, state->inode);
2565 if (!alias)
2566 alias = d_splice_alias(igrab(state->inode), dentry);
2567 /* d_splice_alias() can't fail here - it's a non-directory */
2568 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002569 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002570 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002571 }
2572 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002573 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002574 }
2575
Trond Myklebustc21443c2013-02-07 14:26:21 -05002576 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2577 if (ret != 0)
2578 goto out;
2579
Trond Myklebust3efb9722013-05-29 13:17:04 -04002580 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002581 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002582 nfs_inode_attach_open_context(ctx);
2583 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2584 nfs4_schedule_stateid_recovery(server, state);
2585 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002586out:
2587 return ret;
2588}
2589
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002590/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002591 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
Andy Adamson82be4172012-05-23 05:02:35 -04002593static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002594 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002595 int flags,
2596 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002597 struct nfs4_label *label,
2598 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 struct nfs4_state_owner *sp;
2601 struct nfs4_state *state = NULL;
2602 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002603 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002604 struct dentry *dentry = ctx->dentry;
2605 struct rpc_cred *cred = ctx->cred;
2606 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2607 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002608 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002609 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002610 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002614 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2615 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2617 goto out_err;
2618 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002619 status = nfs4_recover_expired_lease(server);
2620 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002621 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002622 if (d_really_is_positive(dentry))
2623 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002624 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002625 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002626 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002627 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002628 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002629 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002630 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
David Quigley14c43f72013-05-22 12:50:43 -04002632 if (label) {
2633 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2634 if (IS_ERR(olabel)) {
2635 status = PTR_ERR(olabel);
2636 goto err_opendata_put;
2637 }
2638 }
2639
Trond Myklebuste911b812014-03-26 13:24:37 -07002640 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2641 if (!opendata->f_attr.mdsthreshold) {
2642 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2643 if (!opendata->f_attr.mdsthreshold)
2644 goto err_free_label;
2645 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002646 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002647 }
David Howells2b0143b2015-03-17 22:25:59 +00002648 if (d_really_is_positive(dentry))
2649 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002650
Trond Myklebust3efb9722013-05-29 13:17:04 -04002651 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002652 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002653 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002654 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002655
NeilBrownefcbc042015-07-30 13:00:56 +10002656 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002657 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002658 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002659 /*
2660 * send create attributes which was not set by open
2661 * with an extra setattr.
2662 */
2663 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2664 nfs_fattr_init(opendata->o_res.f_attr);
2665 status = nfs4_do_setattr(state->inode, cred,
2666 opendata->o_res.f_attr, sattr,
2667 state, label, olabel);
2668 if (status == 0) {
2669 nfs_setattr_update_inode(state->inode, sattr,
2670 opendata->o_res.f_attr);
2671 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2672 }
David Quigley1775fd32013-05-22 12:50:42 -04002673 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002674 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002675 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002676 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002677
Trond Myklebuste911b812014-03-26 13:24:37 -07002678 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002679 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002680 opendata->f_attr.mdsthreshold = NULL;
2681 }
Andy Adamson82be4172012-05-23 05:02:35 -04002682
David Quigley14c43f72013-05-22 12:50:43 -04002683 nfs4_label_free(olabel);
2684
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002685 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002688err_free_label:
2689 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002690err_opendata_put:
2691 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002692err_put_state_owner:
2693 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return status;
2696}
2697
2698
Andy Adamson82be4172012-05-23 05:02:35 -04002699static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002700 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002701 int flags,
2702 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002703 struct nfs4_label *label,
2704 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002706 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct nfs4_exception exception = { };
2708 struct nfs4_state *res;
2709 int status;
2710
2711 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002712 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002713 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002714 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (status == 0)
2716 break;
2717 /* NOTE: BAD_SEQID means the server and client disagree about the
2718 * book-keeping w.r.t. state-changing operations
2719 * (OPEN/CLOSE/LOCK/LOCKU...)
2720 * It is actually a sign of a bug on the client or on the server.
2721 *
2722 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002723 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 * have unhashed the old state_owner for us, and that we can
2725 * therefore safely retry using a new one. We should still warn
2726 * the user though...
2727 */
2728 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002729 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002730 " returned a bad sequence-id error!\n",
2731 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 exception.retry = 1;
2733 continue;
2734 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002735 /*
2736 * BAD_STATEID on OPEN means that the server cancelled our
2737 * state before it received the OPEN_CONFIRM.
2738 * Recover by retrying the request as per the discussion
2739 * on Page 181 of RFC3530.
2740 */
2741 if (status == -NFS4ERR_BAD_STATEID) {
2742 exception.retry = 1;
2743 continue;
2744 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002745 if (status == -EAGAIN) {
2746 /* We must have found a delegation */
2747 exception.retry = 1;
2748 continue;
2749 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002750 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2751 continue;
2752 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 status, &exception));
2754 } while (exception.retry);
2755 return res;
2756}
2757
Trond Myklebust8487c472016-06-26 08:44:35 -04002758static int _nfs4_do_setattr(struct inode *inode,
2759 struct nfs_setattrargs *arg,
2760 struct nfs_setattrres *res,
2761 struct rpc_cred *cred,
2762 struct nfs4_state *state)
2763{
2764 struct nfs_server *server = NFS_SERVER(inode);
2765 struct rpc_message msg = {
2766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2767 .rpc_argp = arg,
2768 .rpc_resp = res,
2769 .rpc_cred = cred,
2770 };
2771 struct rpc_cred *delegation_cred = NULL;
2772 unsigned long timestamp = jiffies;
2773 fmode_t fmode;
2774 bool truncate;
2775 int status;
2776
2777 nfs_fattr_init(res->fattr);
2778
2779 /* Servers should only apply open mode checks for file size changes */
2780 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2781 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2782
2783 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2784 /* Use that stateid */
2785 } else if (truncate && state != NULL) {
2786 struct nfs_lockowner lockowner = {
2787 .l_owner = current->files,
2788 .l_pid = current->tgid,
2789 };
2790 if (!nfs4_valid_open_stateid(state))
2791 return -EBADF;
2792 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2793 &arg->stateid, &delegation_cred) == -EIO)
2794 return -EBADF;
2795 } else
2796 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2797 if (delegation_cred)
2798 msg.rpc_cred = delegation_cred;
2799
2800 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2801
2802 put_rpccred(delegation_cred);
2803 if (status == 0 && state != NULL)
2804 renew_lease(server, timestamp);
2805 trace_nfs4_setattr(inode, &arg->stateid, status);
2806 return status;
2807}
2808
2809static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2810 struct nfs_fattr *fattr, struct iattr *sattr,
2811 struct nfs4_state *state, struct nfs4_label *ilabel,
2812 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002814 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002816 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 .iap = sattr,
2818 .server = server,
2819 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002820 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 };
2822 struct nfs_setattrres res = {
2823 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002824 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 .server = server,
2826 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002827 struct nfs4_exception exception = {
2828 .state = state,
2829 .inode = inode,
2830 .stateid = &arg.stateid,
2831 };
2832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
David Quigleyaa9c2662013-05-22 12:50:44 -04002834 arg.bitmask = nfs4_bitmask(server, ilabel);
2835 if (ilabel)
2836 arg.bitmask = nfs4_bitmask(server, olabel);
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002839 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002840 switch (err) {
2841 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002842 if (!(sattr->ia_valid & ATTR_SIZE)) {
2843 pr_warn_once("NFSv4: server %s is incorrectly "
2844 "applying open mode checks to "
2845 "a SETATTR that is not "
2846 "changing file size.\n",
2847 server->nfs_client->cl_hostname);
2848 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002849 if (state && !(state->state & FMODE_WRITE)) {
2850 err = -EBADF;
2851 if (sattr->ia_valid & ATTR_OPEN)
2852 err = -EACCES;
2853 goto out;
2854 }
2855 }
2856 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002858out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return err;
2860}
2861
Peng Tao500d7012015-09-22 11:35:22 +08002862static bool
2863nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2864{
2865 if (inode == NULL || !nfs_have_layout(inode))
2866 return false;
2867
2868 return pnfs_wait_on_layoutreturn(inode, task);
2869}
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871struct nfs4_closedata {
2872 struct inode *inode;
2873 struct nfs4_state *state;
2874 struct nfs_closeargs arg;
2875 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002876 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002877 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002878 bool roc;
2879 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880};
2881
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002882static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002883{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002884 struct nfs4_closedata *calldata = data;
2885 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002886 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002887
Fred Isamanf7e89172011-01-06 11:36:32 +00002888 if (calldata->roc)
2889 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002890 nfs4_put_open_state(calldata->state);
2891 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002892 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002893 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002894 kfree(calldata);
2895}
2896
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002897static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002899 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002902 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Chuck Levera3ca5652012-03-01 17:00:40 -05002904 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002905 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2906 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002907 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /* hmm. we are done with the inode, and in the process of freeing
2909 * the state_owner. we keep this around to process errors
2910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 switch (task->tk_status) {
2912 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002913 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002914 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002915 pnfs_roc_set_barrier(state->inode,
2916 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002917 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002918 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002919 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002921 case -NFS4ERR_OLD_STATEID:
2922 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002924 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002925 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002926 rpc_restart_call_prepare(task);
2927 goto out_release;
2928 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002929 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002932 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002933 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002934 goto out_release;
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002937 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2938 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002939out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002940 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002941 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002942 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002945static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002947 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002948 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002949 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002950 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002951 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002952
Chuck Levera3ca5652012-03-01 17:00:40 -05002953 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002954 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002955 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002956
Trond Myklebust88069f72009-12-08 08:33:16 -05002957 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002958 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002959 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2960 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2961 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002962 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002963 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002964 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002965 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002966 if (state->n_rdonly == 0)
2967 call_close |= is_rdonly;
2968 else if (is_rdonly)
2969 calldata->arg.fmode |= FMODE_READ;
2970 if (state->n_wronly == 0)
2971 call_close |= is_wronly;
2972 else if (is_wronly)
2973 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002974 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2975 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002976 } else if (is_rdwr)
2977 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2978
Trond Myklebust5d422302013-03-14 16:57:48 -04002979 if (!nfs4_valid_open_stateid(state))
2980 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002981 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002982
2983 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002984 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002985 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002986 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002987
Peng Tao500d7012015-09-22 11:35:22 +08002988 if (nfs4_wait_on_layoutreturn(inode, task)) {
2989 nfs_release_seqid(calldata->arg.seqid);
2990 goto out_wait;
2991 }
2992
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002993 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002994 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002995 if (calldata->roc)
2996 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2997
Trond Myklebust6ae37332015-01-30 14:21:14 -05002998 calldata->arg.share_access =
2999 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3000 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003001
Trond Myklebust516a6af2005-10-27 22:12:41 -04003002 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003003 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003004 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003005 &calldata->arg.seq_args,
3006 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003007 task) != 0)
3008 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003009 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003010 return;
3011out_no_action:
3012 task->tk_action = NULL;
3013out_wait:
3014 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003017static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003018 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003019 .rpc_call_done = nfs4_close_done,
3020 .rpc_release = nfs4_free_closedata,
3021};
3022
Peng Taofe08c542014-07-03 13:05:01 +08003023static bool nfs4_roc(struct inode *inode)
3024{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003025 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003026 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003027 return pnfs_roc(inode);
3028}
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030/*
3031 * It is possible for data to be read/written from a mem-mapped file
3032 * after the sys_close call (which hits the vfs layer as a flush).
3033 * This means that we can't safely call nfsv4 close on a file until
3034 * the inode is cleared. This in turn means that we are not good
3035 * NFSv4 citizens - we do not indicate to the server to update the file's
3036 * share state even when we are done with one of the three share
3037 * stateid's in the inode.
3038 *
3039 * NOTE: Caller must be holding the sp->so_owner semaphore!
3040 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003041int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003043 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003044 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003046 struct nfs4_state_owner *sp = state->owner;
3047 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003048 struct rpc_message msg = {
3049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3050 .rpc_cred = state->owner->so_cred,
3051 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003052 struct rpc_task_setup task_setup_data = {
3053 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003054 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003055 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003056 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003057 .flags = RPC_TASK_ASYNC,
3058 };
Trond Myklebust95121352005-10-18 14:20:12 -07003059 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003061 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3062 &task_setup_data.rpc_client, &msg);
3063
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003064 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003066 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003067 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003068 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003070 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003072 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3073 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003074 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003075 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003076 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003077 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003078 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003079 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003080 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003081 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003082 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003083
Trond Myklebust1174dd12010-12-21 10:52:24 -05003084 msg.rpc_argp = &calldata->arg;
3085 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003086 task_setup_data.callback_data = calldata;
3087 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003088 if (IS_ERR(task))
3089 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003090 status = 0;
3091 if (wait)
3092 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003093 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003094 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003095out_free_calldata:
3096 kfree(calldata);
3097out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003098 nfs4_put_open_state(state);
3099 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003100 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Trond Myklebust2b484292010-09-17 10:56:51 -04003103static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003104nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3105 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003108 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3109
3110 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Trond Myklebust565277f2007-10-15 18:17:53 -04003112 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003113 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003114
3115 nfs4_label_release_security(label);
3116
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003117 if (IS_ERR(state))
3118 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003119 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
Trond Myklebust1185a552009-12-03 15:54:02 -05003122static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003123{
3124 if (ctx->state == NULL)
3125 return;
3126 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003127 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003128 else
Al Viro643168c2011-06-22 18:20:23 -04003129 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Trond Myklebustb944dba2013-11-04 15:20:20 -05003132#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3133#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003134#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3137{
Kinglong Mee8c612822015-08-26 21:12:58 +08003138 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003139 struct nfs4_server_caps_arg args = {
3140 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003141 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003142 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 struct nfs4_server_caps_res res = {};
3144 struct rpc_message msg = {
3145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003146 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 .rpc_resp = &res,
3148 };
3149 int status;
3150
Kinglong Mee8c612822015-08-26 21:12:58 +08003151 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3152 FATTR4_WORD0_FH_EXPIRE_TYPE |
3153 FATTR4_WORD0_LINK_SUPPORT |
3154 FATTR4_WORD0_SYMLINK_SUPPORT |
3155 FATTR4_WORD0_ACLSUPPORT;
3156 if (minorversion)
3157 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3158
Bryan Schumaker7c513052011-03-24 17:12:24 +00003159 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003161 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003162 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003163 case 0:
3164 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3165 res.attr_bitmask[2] = 0;
3166 break;
3167 case 1:
3168 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3169 break;
3170 case 2:
3171 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003174 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3175 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3176 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3177 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003178 NFS_CAP_CTIME|NFS_CAP_MTIME|
3179 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003180 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3181 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 server->caps |= NFS_CAP_ACLS;
3183 if (res.has_links != 0)
3184 server->caps |= NFS_CAP_HARDLINKS;
3185 if (res.has_symlinks != 0)
3186 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003187 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3188 server->caps |= NFS_CAP_FILEID;
3189 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3190 server->caps |= NFS_CAP_MODE;
3191 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3192 server->caps |= NFS_CAP_NLINK;
3193 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3194 server->caps |= NFS_CAP_OWNER;
3195 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3196 server->caps |= NFS_CAP_OWNER_GROUP;
3197 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3198 server->caps |= NFS_CAP_ATIME;
3199 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3200 server->caps |= NFS_CAP_CTIME;
3201 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3202 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003203#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3204 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3205 server->caps |= NFS_CAP_SECURITY_LABEL;
3206#endif
3207 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3208 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003209 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003210
Trond Myklebusta65318b2009-03-11 14:10:28 -04003211 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3212 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3213 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003214 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003215 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3216 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003218 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return status;
3222}
3223
Trond Myklebust55a97592006-06-09 09:34:19 -04003224int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct nfs4_exception exception = { };
3227 int err;
3228 do {
3229 err = nfs4_handle_exception(server,
3230 _nfs4_server_capabilities(server, fhandle),
3231 &exception);
3232 } while (exception.retry);
3233 return err;
3234}
3235
3236static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3237 struct nfs_fsinfo *info)
3238{
David Quigleyaa9c2662013-05-22 12:50:44 -04003239 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003241 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 };
3243 struct nfs4_lookup_res res = {
3244 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003245 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 .fh = fhandle,
3247 };
3248 struct rpc_message msg = {
3249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3250 .rpc_argp = &args,
3251 .rpc_resp = &res,
3252 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003253
David Quigleyaa9c2662013-05-22 12:50:44 -04003254 bitmask[0] = nfs4_fattr_bitmap[0];
3255 bitmask[1] = nfs4_fattr_bitmap[1];
3256 /*
3257 * Process the label in the upcoming getfattr
3258 */
3259 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3260
Trond Myklebust0e574af2005-10-27 22:12:38 -04003261 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003262 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
3265static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3266 struct nfs_fsinfo *info)
3267{
3268 struct nfs4_exception exception = { };
3269 int err;
3270 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003271 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003272 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003273 switch (err) {
3274 case 0:
3275 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003276 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003277 default:
3278 err = nfs4_handle_exception(server, err, &exception);
3279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003281out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return err;
3283}
3284
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003285static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3286 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3287{
Trond Myklebustc2190662013-08-26 19:23:04 -04003288 struct rpc_auth_create_args auth_args = {
3289 .pseudoflavor = flavor,
3290 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003291 struct rpc_auth *auth;
3292 int ret;
3293
Trond Myklebustc2190662013-08-26 19:23:04 -04003294 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003295 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003296 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003297 goto out;
3298 }
3299 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003300out:
3301 return ret;
3302}
3303
Chuck Lever9a744ba2013-03-16 15:56:02 -04003304/*
3305 * Retry pseudoroot lookup with various security flavors. We do this when:
3306 *
3307 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3308 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3309 *
3310 * Returns zero on success, or a negative NFS4ERR value, or a
3311 * negative errno value.
3312 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003313static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003314 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003316 /* Per 3530bis 15.33.5 */
3317 static const rpc_authflavor_t flav_array[] = {
3318 RPC_AUTH_GSS_KRB5P,
3319 RPC_AUTH_GSS_KRB5I,
3320 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003321 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003322 RPC_AUTH_NULL,
3323 };
3324 int status = -EPERM;
3325 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003327 if (server->auth_info.flavor_len > 0) {
3328 /* try each flavor specified by user */
3329 for (i = 0; i < server->auth_info.flavor_len; i++) {
3330 status = nfs4_lookup_root_sec(server, fhandle, info,
3331 server->auth_info.flavors[i]);
3332 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3333 continue;
3334 break;
3335 }
3336 } else {
3337 /* no flavors specified by user, try default list */
3338 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3339 status = nfs4_lookup_root_sec(server, fhandle, info,
3340 flav_array[i]);
3341 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3342 continue;
3343 break;
3344 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003345 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003346
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003347 /*
3348 * -EACCESS could mean that the user doesn't have correct permissions
3349 * to access the mount. It could also mean that we tried to mount
3350 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3351 * existing mount programs don't handle -EACCES very well so it should
3352 * be mapped to -EPERM instead.
3353 */
3354 if (status == -EACCES)
3355 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003356 return status;
3357}
3358
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003359/**
3360 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3361 * @server: initialized nfs_server handle
3362 * @fhandle: we fill in the pseudo-fs root file handle
3363 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003364 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003365 *
3366 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003367 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003368int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003369 struct nfs_fsinfo *info,
3370 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003371{
Andre Przywarac7757072015-04-23 17:17:40 +01003372 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003373
Andre Przywarac7757072015-04-23 17:17:40 +01003374 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003375 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003376
3377 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003378 status = server->nfs_client->cl_mvops->find_root_sec(server,
3379 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (status == 0)
3382 status = nfs4_server_capabilities(server, fhandle);
3383 if (status == 0)
3384 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003385
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003386 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
Bryan Schumakerbae36242012-05-10 15:07:31 -04003389static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3390 struct nfs_fsinfo *info)
3391{
3392 int error;
3393 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003394 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003395
3396 error = nfs4_server_capabilities(server, mntfh);
3397 if (error < 0) {
3398 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3399 return error;
3400 }
3401
David Quigley14c43f72013-05-22 12:50:43 -04003402 label = nfs4_label_alloc(server, GFP_KERNEL);
3403 if (IS_ERR(label))
3404 return PTR_ERR(label);
3405
David Quigley1775fd32013-05-22 12:50:42 -04003406 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003407 if (error < 0) {
3408 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003409 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003410 }
3411
3412 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3413 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3414 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3415
David Quigley14c43f72013-05-22 12:50:43 -04003416err_free_label:
3417 nfs4_label_free(label);
3418
Bryan Schumakerbae36242012-05-10 15:07:31 -04003419 return error;
3420}
3421
Manoj Naik6b97fd32006-06-09 09:34:29 -04003422/*
3423 * Get locations and (maybe) other attributes of a referral.
3424 * Note that we'll actually follow the referral later when
3425 * we detect fsid mismatch in inode revalidation
3426 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003427static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3428 const struct qstr *name, struct nfs_fattr *fattr,
3429 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003430{
3431 int status = -ENOMEM;
3432 struct page *page = NULL;
3433 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003434
3435 page = alloc_page(GFP_KERNEL);
3436 if (page == NULL)
3437 goto out;
3438 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3439 if (locations == NULL)
3440 goto out;
3441
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003442 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003443 if (status != 0)
3444 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003445
3446 /*
3447 * If the fsid didn't change, this is a migration event, not a
3448 * referral. Cause us to drop into the exception handler, which
3449 * will kick off migration recovery.
3450 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003451 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003452 dprintk("%s: server did not return a different fsid for"
3453 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003454 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003455 goto out;
3456 }
Andy Adamson533eb462011-06-13 18:25:56 -04003457 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3458 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003459
Andy Adamson533eb462011-06-13 18:25:56 -04003460 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003461 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003462 memset(fhandle, 0, sizeof(struct nfs_fh));
3463out:
3464 if (page)
3465 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003466 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003467 return status;
3468}
3469
David Quigley1775fd32013-05-22 12:50:42 -04003470static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3471 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 struct nfs4_getattr_arg args = {
3474 .fh = fhandle,
3475 .bitmask = server->attr_bitmask,
3476 };
3477 struct nfs4_getattr_res res = {
3478 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003479 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 .server = server,
3481 };
3482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3484 .rpc_argp = &args,
3485 .rpc_resp = &res,
3486 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003487
3488 args.bitmask = nfs4_bitmask(server, label);
3489
Trond Myklebust0e574af2005-10-27 22:12:38 -04003490 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
David Quigley1775fd32013-05-22 12:50:42 -04003494static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3495 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497 struct nfs4_exception exception = { };
3498 int err;
3499 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003500 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3501 trace_nfs4_getattr(server, fhandle, fattr, err);
3502 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 &exception);
3504 } while (exception.retry);
3505 return err;
3506}
3507
3508/*
3509 * The file is not closed if it is opened due to the a request to change
3510 * the size of the file. The open call will not be needed once the
3511 * VFS layer lookup-intents are implemented.
3512 *
3513 * Close is called when the inode is destroyed.
3514 * If we haven't opened the file for O_WRONLY, we
3515 * need to in the size_change case to obtain a stateid.
3516 *
3517 * Got race?
3518 * Because OPEN is always done by name in nfsv4, it is
3519 * possible that we opened a different file by the same
3520 * name. We can recognize this race condition, but we
3521 * can't do anything about it besides returning an error.
3522 *
3523 * This will be fixed with VFS changes (lookup-intent).
3524 */
3525static int
3526nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3527 struct iattr *sattr)
3528{
David Howells2b0143b2015-03-17 22:25:59 +00003529 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003530 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003531 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003532 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 int status;
3534
Peng Tao88ac8152014-09-12 11:04:10 +08003535 if (pnfs_ld_layoutret_on_setattr(inode) &&
3536 sattr->ia_valid & ATTR_SIZE &&
3537 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003538 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003539
Trond Myklebust0e574af2005-10-27 22:12:38 -04003540 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Andy Adamson26699402012-05-30 16:12:24 -04003542 /* Deal with open(O_TRUNC) */
3543 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003544 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003545
3546 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003547 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003548 return 0;
3549
Trond Myklebustd5308382005-11-04 15:33:38 -05003550 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003551 if (sattr->ia_valid & ATTR_FILE) {
3552 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003553
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003554 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003555 if (ctx) {
3556 cred = ctx->cred;
3557 state = ctx->state;
3558 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003559 }
3560
David Quigley14c43f72013-05-22 12:50:43 -04003561 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3562 if (IS_ERR(label))
3563 return PTR_ERR(label);
3564
3565 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003566 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003567 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003568 nfs_setsecurity(inode, fattr, label);
3569 }
David Quigley14c43f72013-05-22 12:50:43 -04003570 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return status;
3572}
3573
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003574static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3575 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003576 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003577{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003578 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003579 int status;
3580 struct nfs4_lookup_arg args = {
3581 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003582 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003583 .name = name,
3584 };
3585 struct nfs4_lookup_res res = {
3586 .server = server,
3587 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003588 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003589 .fh = fhandle,
3590 };
3591 struct rpc_message msg = {
3592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3593 .rpc_argp = &args,
3594 .rpc_resp = &res,
3595 };
3596
David Quigleyaa9c2662013-05-22 12:50:44 -04003597 args.bitmask = nfs4_bitmask(server, label);
3598
David Howells2b3de442006-08-22 20:06:09 -04003599 nfs_fattr_init(fattr);
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003602 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 dprintk("NFS reply lookup: %d\n", status);
3604 return status;
3605}
3606
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003607static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003608{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003609 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003610 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003611 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3612 fattr->nlink = 2;
3613}
3614
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003615static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003616 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003617 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003618{
3619 struct nfs4_exception exception = { };
3620 struct rpc_clnt *client = *clnt;
3621 int err;
3622 do {
David Quigley1775fd32013-05-22 12:50:42 -04003623 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003624 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003625 switch (err) {
3626 case -NFS4ERR_BADNAME:
3627 err = -ENOENT;
3628 goto out;
3629 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003630 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003631 if (err == -NFS4ERR_MOVED)
3632 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003633 goto out;
3634 case -NFS4ERR_WRONGSEC:
3635 err = -EPERM;
3636 if (client != *clnt)
3637 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003638 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003639 if (IS_ERR(client))
3640 return PTR_ERR(client);
3641
3642 exception.retry = 1;
3643 break;
3644 default:
3645 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3646 }
3647 } while (exception.retry);
3648
3649out:
3650 if (err == 0)
3651 *clnt = client;
3652 else if (client != *clnt)
3653 rpc_shutdown_client(client);
3654
3655 return err;
3656}
3657
Al Virobeffb8f2016-07-20 16:34:42 -04003658static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003659 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3660 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003662 int status;
3663 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003664
David Quigley1775fd32013-05-22 12:50:42 -04003665 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003666 if (client != NFS_CLIENT(dir)) {
3667 rpc_shutdown_client(client);
3668 nfs_fixup_secinfo_attributes(fattr);
3669 }
3670 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003673struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003674nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003675 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3676{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003677 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003678 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003679
David Quigley1775fd32013-05-22 12:50:42 -04003680 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003681 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003682 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003683 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003684}
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3687{
Trond Myklebust76b32992007-08-10 17:45:11 -04003688 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 struct nfs4_accessargs args = {
3690 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003691 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003693 struct nfs4_accessres res = {
3694 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003695 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 struct rpc_message msg = {
3697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3698 .rpc_argp = &args,
3699 .rpc_resp = &res,
3700 .rpc_cred = entry->cred,
3701 };
3702 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705 /*
3706 * Determine which access bits we want to ask for...
3707 */
3708 if (mode & MAY_READ)
3709 args.access |= NFS4_ACCESS_READ;
3710 if (S_ISDIR(inode->i_mode)) {
3711 if (mode & MAY_WRITE)
3712 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3713 if (mode & MAY_EXEC)
3714 args.access |= NFS4_ACCESS_LOOKUP;
3715 } else {
3716 if (mode & MAY_WRITE)
3717 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3718 if (mode & MAY_EXEC)
3719 args.access |= NFS4_ACCESS_EXECUTE;
3720 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003721
3722 res.fattr = nfs_alloc_fattr();
3723 if (res.fattr == NULL)
3724 return -ENOMEM;
3725
Bryan Schumaker7c513052011-03-24 17:12:24 +00003726 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003728 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003729 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003731 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return status;
3733}
3734
3735static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3736{
3737 struct nfs4_exception exception = { };
3738 int err;
3739 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003740 err = _nfs4_proc_access(inode, entry);
3741 trace_nfs4_access(inode, err);
3742 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 &exception);
3744 } while (exception.retry);
3745 return err;
3746}
3747
3748/*
3749 * TODO: For the time being, we don't try to get any attributes
3750 * along with any of the zero-copy operations READ, READDIR,
3751 * READLINK, WRITE.
3752 *
3753 * In the case of the first three, we want to put the GETATTR
3754 * after the read-type operation -- this is because it is hard
3755 * to predict the length of a GETATTR response in v4, and thus
3756 * align the READ data correctly. This means that the GETATTR
3757 * may end up partially falling into the page cache, and we should
3758 * shift it into the 'tail' of the xdr_buf before processing.
3759 * To do this efficiently, we need to know the total length
3760 * of data received, which doesn't seem to be available outside
3761 * of the RPC layer.
3762 *
3763 * In the case of WRITE, we also want to put the GETATTR after
3764 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003765 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 *
3767 * Both of these changes to the XDR layer would in fact be quite
3768 * minor, but I decided to leave them for a subsequent patch.
3769 */
3770static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3771 unsigned int pgbase, unsigned int pglen)
3772{
3773 struct nfs4_readlink args = {
3774 .fh = NFS_FH(inode),
3775 .pgbase = pgbase,
3776 .pglen = pglen,
3777 .pages = &page,
3778 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003779 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3782 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003783 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 };
3785
Bryan Schumaker7c513052011-03-24 17:12:24 +00003786 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 -07003787}
3788
3789static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3790 unsigned int pgbase, unsigned int pglen)
3791{
3792 struct nfs4_exception exception = { };
3793 int err;
3794 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003795 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3796 trace_nfs4_readlink(inode, err);
3797 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 &exception);
3799 } while (exception.retry);
3800 return err;
3801}
3802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003804 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806static int
3807nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003808 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
David Quigleyaa9c2662013-05-22 12:50:44 -04003810 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003811 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 int status = 0;
3814
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003815 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3816 if (IS_ERR(ctx))
3817 return PTR_ERR(ctx);
3818
David Quigleyaa9c2662013-05-22 12:50:44 -04003819 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3820
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003821 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003822 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 if (IS_ERR(state)) {
3824 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003828 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003829 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 return status;
3831}
3832
Al Virobeffb8f2016-07-20 16:34:42 -04003833static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Trond Myklebust16e42952005-10-27 22:12:44 -04003835 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003836 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003838 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003840 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003841 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003842 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3845 .rpc_argp = &args,
3846 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003848 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003849
Bryan Schumaker7c513052011-03-24 17:12:24 +00003850 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003851 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003852 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return status;
3854}
3855
Al Virobeffb8f2016-07-20 16:34:42 -04003856static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
3858 struct nfs4_exception exception = { };
3859 int err;
3860 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003861 err = _nfs4_proc_remove(dir, name);
3862 trace_nfs4_remove(dir, name, err);
3863 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 &exception);
3865 } while (exception.retry);
3866 return err;
3867}
3868
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003869static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003871 struct nfs_server *server = NFS_SERVER(dir);
3872 struct nfs_removeargs *args = msg->rpc_argp;
3873 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003875 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003877 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003878
3879 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003882static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3883{
Al Viro884be172016-04-28 23:56:31 -04003884 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003885 &data->args.seq_args,
3886 &data->res.seq_res,
3887 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888}
3889
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003890static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003892 struct nfs_unlinkdata *data = task->tk_calldata;
3893 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003894
Trond Myklebust14516c32010-07-31 14:29:06 -04003895 if (!nfs4_sequence_done(task, &res->seq_res))
3896 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003897 if (nfs4_async_handle_error(task, res->server, NULL,
3898 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003899 return 0;
3900 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003901 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
Jeff Laytond3d41522010-09-17 17:31:57 -04003904static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3905{
3906 struct nfs_server *server = NFS_SERVER(dir);
3907 struct nfs_renameargs *arg = msg->rpc_argp;
3908 struct nfs_renameres *res = msg->rpc_resp;
3909
3910 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003911 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003912 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003913}
3914
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003915static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3916{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003917 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3918 &data->args.seq_args,
3919 &data->res.seq_res,
3920 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003921}
3922
3923static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3924 struct inode *new_dir)
3925{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003926 struct nfs_renamedata *data = task->tk_calldata;
3927 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003928
3929 if (!nfs4_sequence_done(task, &res->seq_res))
3930 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003931 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003932 return 0;
3933
3934 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003935 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003936 return 1;
3937}
3938
Al Virobeffb8f2016-07-20 16:34:42 -04003939static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003941 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct nfs4_link_arg arg = {
3943 .fh = NFS_FH(inode),
3944 .dir_fh = NFS_FH(dir),
3945 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003946 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003948 struct nfs4_link_res res = {
3949 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003950 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003951 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3954 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003955 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003957 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Trond Myklebust136f2622010-04-16 16:22:49 -04003959 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003960 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003961 goto out;
3962
David Quigley14c43f72013-05-22 12:50:43 -04003963 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3964 if (IS_ERR(res.label)) {
3965 status = PTR_ERR(res.label);
3966 goto out;
3967 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003968 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003969
Bryan Schumaker7c513052011-03-24 17:12:24 +00003970 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003971 if (!status) {
3972 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003973 status = nfs_post_op_update_inode(inode, res.fattr);
3974 if (!status)
3975 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003976 }
David Quigley14c43f72013-05-22 12:50:43 -04003977
3978
3979 nfs4_label_free(res.label);
3980
Trond Myklebust136f2622010-04-16 16:22:49 -04003981out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003982 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return status;
3984}
3985
Al Virobeffb8f2016-07-20 16:34:42 -04003986static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
3988 struct nfs4_exception exception = { };
3989 int err;
3990 do {
3991 err = nfs4_handle_exception(NFS_SERVER(inode),
3992 _nfs4_proc_link(inode, dir, name),
3993 &exception);
3994 } while (exception.retry);
3995 return err;
3996}
3997
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003998struct nfs4_createdata {
3999 struct rpc_message msg;
4000 struct nfs4_create_arg arg;
4001 struct nfs4_create_res res;
4002 struct nfs_fh fh;
4003 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004004 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004005};
4006
4007static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004008 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004009{
4010 struct nfs4_createdata *data;
4011
4012 data = kzalloc(sizeof(*data), GFP_KERNEL);
4013 if (data != NULL) {
4014 struct nfs_server *server = NFS_SERVER(dir);
4015
David Quigley14c43f72013-05-22 12:50:43 -04004016 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4017 if (IS_ERR(data->label))
4018 goto out_free;
4019
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004020 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4021 data->msg.rpc_argp = &data->arg;
4022 data->msg.rpc_resp = &data->res;
4023 data->arg.dir_fh = NFS_FH(dir);
4024 data->arg.server = server;
4025 data->arg.name = name;
4026 data->arg.attrs = sattr;
4027 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004028 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004029 data->res.server = server;
4030 data->res.fh = &data->fh;
4031 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004032 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004033 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004034 }
4035 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004036out_free:
4037 kfree(data);
4038 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004039}
4040
4041static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4042{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004043 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004044 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004045 if (status == 0) {
4046 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004047 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004048 }
4049 return status;
4050}
4051
4052static void nfs4_free_createdata(struct nfs4_createdata *data)
4053{
David Quigley14c43f72013-05-22 12:50:43 -04004054 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004055 kfree(data);
4056}
4057
Chuck Lever4f390c12006-08-22 20:06:22 -04004058static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004059 struct page *page, unsigned int len, struct iattr *sattr,
4060 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004062 struct nfs4_createdata *data;
4063 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Chuck Lever94a6d752006-08-22 20:06:23 -04004065 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004066 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004067
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004068 status = -ENOMEM;
4069 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4070 if (data == NULL)
4071 goto out;
4072
4073 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4074 data->arg.u.symlink.pages = &page;
4075 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004076 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004078 status = nfs4_do_create(dir, dentry, data);
4079
4080 nfs4_free_createdata(data);
4081out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return status;
4083}
4084
Chuck Lever4f390c12006-08-22 20:06:22 -04004085static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004086 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
4088 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004089 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004091
4092 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004095 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4096 trace_nfs4_symlink(dir, &dentry->d_name, err);
4097 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 &exception);
4099 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004100
4101 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 return err;
4103}
4104
4105static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004106 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004108 struct nfs4_createdata *data;
4109 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004111 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4112 if (data == NULL)
4113 goto out;
4114
David Quigley1775fd32013-05-22 12:50:42 -04004115 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004116 status = nfs4_do_create(dir, dentry, data);
4117
4118 nfs4_free_createdata(data);
4119out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return status;
4121}
4122
4123static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4124 struct iattr *sattr)
4125{
4126 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004127 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004129
David Quigleyaa9c2662013-05-22 12:50:44 -04004130 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4131
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004132 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004134 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4135 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4136 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 &exception);
4138 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004139 nfs4_label_release_security(label);
4140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return err;
4142}
4143
4144static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004145 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
David Howells2b0143b2015-03-17 22:25:59 +00004147 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 struct nfs4_readdir_arg args = {
4149 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004150 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 .pgbase = 0,
4152 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004153 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004154 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 };
4156 struct nfs4_readdir_res res;
4157 struct rpc_message msg = {
4158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4159 .rpc_argp = &args,
4160 .rpc_resp = &res,
4161 .rpc_cred = cred,
4162 };
4163 int status;
4164
Al Viro6de14722013-09-16 10:53:17 -04004165 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4166 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004167 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004168 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004170 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 -05004171 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004172 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004173 status += args.pgbase;
4174 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004175
4176 nfs_invalidate_atime(dir);
4177
Harvey Harrison3110ff82008-05-02 13:42:44 -07004178 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return status;
4180}
4181
4182static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004183 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 struct nfs4_exception exception = { };
4186 int err;
4187 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004188 err = _nfs4_proc_readdir(dentry, cred, cookie,
4189 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004190 trace_nfs4_readdir(d_inode(dentry), err);
4191 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 &exception);
4193 } while (exception.retry);
4194 return err;
4195}
4196
4197static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004198 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004200 struct nfs4_createdata *data;
4201 int mode = sattr->ia_mode;
4202 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4205 if (data == NULL)
4206 goto out;
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004211 data->arg.ftype = NF4BLK;
4212 data->arg.u.device.specdata1 = MAJOR(rdev);
4213 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
4215 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 data->arg.ftype = NF4CHR;
4217 data->arg.u.device.specdata1 = MAJOR(rdev);
4218 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004219 } else if (!S_ISSOCK(mode)) {
4220 status = -EINVAL;
4221 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
David Quigley1775fd32013-05-22 12:50:42 -04004223
David Quigleyaa9c2662013-05-22 12:50:44 -04004224 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004225 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004226out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 nfs4_free_createdata(data);
4228out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return status;
4230}
4231
4232static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4233 struct iattr *sattr, dev_t rdev)
4234{
4235 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004236 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004238
David Quigleyaa9c2662013-05-22 12:50:44 -04004239 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4240
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004241 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004243 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4244 trace_nfs4_mknod(dir, &dentry->d_name, err);
4245 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 &exception);
4247 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004248
4249 nfs4_label_release_security(label);
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return err;
4252}
4253
4254static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4255 struct nfs_fsstat *fsstat)
4256{
4257 struct nfs4_statfs_arg args = {
4258 .fh = fhandle,
4259 .bitmask = server->attr_bitmask,
4260 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004261 struct nfs4_statfs_res res = {
4262 .fsstat = fsstat,
4263 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 struct rpc_message msg = {
4265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4266 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004267 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 };
4269
Trond Myklebust0e574af2005-10-27 22:12:38 -04004270 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004271 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
4274static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4275{
4276 struct nfs4_exception exception = { };
4277 int err;
4278 do {
4279 err = nfs4_handle_exception(server,
4280 _nfs4_proc_statfs(server, fhandle, fsstat),
4281 &exception);
4282 } while (exception.retry);
4283 return err;
4284}
4285
4286static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4287 struct nfs_fsinfo *fsinfo)
4288{
4289 struct nfs4_fsinfo_arg args = {
4290 .fh = fhandle,
4291 .bitmask = server->attr_bitmask,
4292 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004293 struct nfs4_fsinfo_res res = {
4294 .fsinfo = fsinfo,
4295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4298 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004299 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 };
4301
Bryan Schumaker7c513052011-03-24 17:12:24 +00004302 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
4305static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4306{
4307 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004308 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 int err;
4310
4311 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004312 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004313 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004314 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004315 nfs4_set_lease_period(server->nfs_client,
4316 fsinfo->lease_time * HZ,
4317 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004318 break;
4319 }
4320 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 } while (exception.retry);
4322 return err;
4323}
4324
4325static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4326{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004327 int error;
4328
Trond Myklebust0e574af2005-10-27 22:12:38 -04004329 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004330 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004331 if (error == 0) {
4332 /* block layout checks this! */
4333 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004334 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004335 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004336
4337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
4340static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4341 struct nfs_pathconf *pathconf)
4342{
4343 struct nfs4_pathconf_arg args = {
4344 .fh = fhandle,
4345 .bitmask = server->attr_bitmask,
4346 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004347 struct nfs4_pathconf_res res = {
4348 .pathconf = pathconf,
4349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4352 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004353 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 };
4355
4356 /* None of the pathconf attributes are mandatory to implement */
4357 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4358 memset(pathconf, 0, sizeof(*pathconf));
4359 return 0;
4360 }
4361
Trond Myklebust0e574af2005-10-27 22:12:38 -04004362 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004363 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
4365
4366static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4367 struct nfs_pathconf *pathconf)
4368{
4369 struct nfs4_exception exception = { };
4370 int err;
4371
4372 do {
4373 err = nfs4_handle_exception(server,
4374 _nfs4_proc_pathconf(server, fhandle, pathconf),
4375 &exception);
4376 } while (exception.retry);
4377 return err;
4378}
4379
Trond Myklebust5521abf2013-03-16 20:54:34 -04004380int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004381 const struct nfs_open_context *ctx,
4382 const struct nfs_lock_context *l_ctx,
4383 fmode_t fmode)
4384{
4385 const struct nfs_lockowner *lockowner = NULL;
4386
4387 if (l_ctx != NULL)
4388 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004389 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004390}
4391EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4392
Trond Myklebust5521abf2013-03-16 20:54:34 -04004393static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4394 const struct nfs_open_context *ctx,
4395 const struct nfs_lock_context *l_ctx,
4396 fmode_t fmode)
4397{
4398 nfs4_stateid current_stateid;
4399
Trond Myklebuste1253be2014-03-05 08:44:23 -05004400 /* If the current stateid represents a lost lock, then exit */
4401 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4402 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004403 return nfs4_stateid_match(stateid, &current_stateid);
4404}
4405
4406static bool nfs4_error_stateid_expired(int err)
4407{
4408 switch (err) {
4409 case -NFS4ERR_DELEG_REVOKED:
4410 case -NFS4ERR_ADMIN_REVOKED:
4411 case -NFS4ERR_BAD_STATEID:
4412 case -NFS4ERR_STALE_STATEID:
4413 case -NFS4ERR_OLD_STATEID:
4414 case -NFS4ERR_OPENMODE:
4415 case -NFS4ERR_EXPIRED:
4416 return true;
4417 }
4418 return false;
4419}
4420
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004421void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004422{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004423 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004424}
4425
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004426static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004428 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004430 trace_nfs4_read(hdr, task->tk_status);
4431 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004432 hdr->args.context->state,
4433 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004434 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004435 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004437
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004438 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004440 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Trond Myklebust5521abf2013-03-16 20:54:34 -04004444static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004445 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004446{
4447
4448 if (!nfs4_error_stateid_expired(task->tk_status) ||
4449 nfs4_stateid_is_current(&args->stateid,
4450 args->context,
4451 args->lock_context,
4452 FMODE_READ))
4453 return false;
4454 rpc_restart_call_prepare(task);
4455 return true;
4456}
4457
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004458static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004459{
4460
4461 dprintk("--> %s\n", __func__);
4462
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004463 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004464 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004466 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004467 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4468 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004469}
4470
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004471static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4472 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004474 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004475 if (!hdr->pgio_done_cb)
4476 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004478 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004481static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4482 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004483{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004484 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4485 &hdr->args.seq_args,
4486 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004487 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004488 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004489 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4490 hdr->args.lock_context,
4491 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004492 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004493 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004494 return -EIO;
4495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
4497
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004498static int nfs4_write_done_cb(struct rpc_task *task,
4499 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004501 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004502
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004503 trace_nfs4_write(hdr, task->tk_status);
4504 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004505 hdr->args.context->state,
4506 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004507 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004508 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004510 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004511 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004512 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004513 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Trond Myklebust5521abf2013-03-16 20:54:34 -04004517static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004518 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004519{
4520
4521 if (!nfs4_error_stateid_expired(task->tk_status) ||
4522 nfs4_stateid_is_current(&args->stateid,
4523 args->context,
4524 args->lock_context,
4525 FMODE_WRITE))
4526 return false;
4527 rpc_restart_call_prepare(task);
4528 return true;
4529}
4530
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004531static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004532{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004533 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004534 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004535 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004536 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004537 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4538 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004539}
4540
Trond Myklebust5a37f852012-04-28 14:55:16 -04004541static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004542bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004543{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004544 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004545 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004546 return false;
4547 /* Otherwise, request attributes if and only if we don't hold
4548 * a delegation
4549 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004550 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004551}
Fred Isamana69aef12011-03-03 15:13:47 +00004552
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004553static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4554 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004556 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004557
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004558 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4559 hdr->args.bitmask = NULL;
4560 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004561 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004562 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004563
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004564 if (!hdr->pgio_done_cb)
4565 hdr->pgio_done_cb = nfs4_write_done_cb;
4566 hdr->res.server = server;
4567 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004569 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004570 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
Fred Isaman0b7c0152012-04-20 14:47:39 -04004573static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4574{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004575 nfs4_setup_sequence(NFS_SERVER(data->inode),
4576 &data->args.seq_args,
4577 &data->res.seq_res,
4578 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579}
4580
Fred Isaman0b7c0152012-04-20 14:47:39 -04004581static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004584
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004585 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004586 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4587 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004588 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004589 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
Fred Isaman0b7c0152012-04-20 14:47:39 -04004594static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004595{
4596 if (!nfs4_sequence_done(task, &data->res.seq_res))
4597 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004598 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004599}
4600
Fred Isaman0b7c0152012-04-20 14:47:39 -04004601static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004603 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004604
Fred Isaman0b7c0152012-04-20 14:47:39 -04004605 if (data->commit_done_cb == NULL)
4606 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004607 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004609 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
4611
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004612struct nfs4_renewdata {
4613 struct nfs_client *client;
4614 unsigned long timestamp;
4615};
4616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617/*
4618 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4619 * standalone procedure for queueing an asynchronous RENEW.
4620 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004621static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004622{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004623 struct nfs4_renewdata *data = calldata;
4624 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004625
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004626 if (atomic_read(&clp->cl_count) > 1)
4627 nfs4_schedule_state_renewal(clp);
4628 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004629 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004630}
4631
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004632static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004634 struct nfs4_renewdata *data = calldata;
4635 struct nfs_client *clp = data->client;
4636 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004638 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004639 switch (task->tk_status) {
4640 case 0:
4641 break;
4642 case -NFS4ERR_LEASE_MOVED:
4643 nfs4_schedule_lease_moved_recovery(clp);
4644 break;
4645 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004646 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004647 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4648 return;
4649 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004650 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004651 return;
4652 }
4653 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004655 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004658static const struct rpc_call_ops nfs4_renew_ops = {
4659 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004660 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004661};
4662
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004663static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
4665 struct rpc_message msg = {
4666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4667 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004668 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004670 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004672 if (renew_flags == 0)
4673 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004674 if (!atomic_inc_not_zero(&clp->cl_count))
4675 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004676 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004677 if (data == NULL)
4678 return -ENOMEM;
4679 data->client = clp;
4680 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004681 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004682 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004685static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
4687 struct rpc_message msg = {
4688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4689 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004690 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 };
4692 unsigned long now = jiffies;
4693 int status;
4694
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004695 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 if (status < 0)
4697 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004698 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return 0;
4700}
4701
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004702static inline int nfs4_server_supports_acls(struct nfs_server *server)
4703{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004704 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004705}
4706
Trond Myklebust21f498c2012-08-24 10:59:25 -04004707/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4708 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004709 * the stack.
4710 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004711#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004712
Neil Hormane9e3d722011-03-04 19:26:03 -05004713static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004714 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004715{
4716 struct page *newpage, **spages;
4717 int rc = 0;
4718 size_t len;
4719 spages = pages;
4720
4721 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004722 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004723 newpage = alloc_page(GFP_KERNEL);
4724
4725 if (newpage == NULL)
4726 goto unwind;
4727 memcpy(page_address(newpage), buf, len);
4728 buf += len;
4729 buflen -= len;
4730 *pages++ = newpage;
4731 rc++;
4732 } while (buflen != 0);
4733
4734 return rc;
4735
4736unwind:
4737 for(; rc > 0; rc--)
4738 __free_page(spages[rc-1]);
4739 return -ENOMEM;
4740}
4741
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004742struct nfs4_cached_acl {
4743 int cached;
4744 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004745 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004746};
4747
4748static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004749{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004750 struct nfs_inode *nfsi = NFS_I(inode);
4751
4752 spin_lock(&inode->i_lock);
4753 kfree(nfsi->nfs4_acl);
4754 nfsi->nfs4_acl = acl;
4755 spin_unlock(&inode->i_lock);
4756}
4757
4758static void nfs4_zap_acl_attr(struct inode *inode)
4759{
4760 nfs4_set_cached_acl(inode, NULL);
4761}
4762
4763static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4764{
4765 struct nfs_inode *nfsi = NFS_I(inode);
4766 struct nfs4_cached_acl *acl;
4767 int ret = -ENOENT;
4768
4769 spin_lock(&inode->i_lock);
4770 acl = nfsi->nfs4_acl;
4771 if (acl == NULL)
4772 goto out;
4773 if (buf == NULL) /* user is just asking for length */
4774 goto out_len;
4775 if (acl->cached == 0)
4776 goto out;
4777 ret = -ERANGE; /* see getxattr(2) man page */
4778 if (acl->len > buflen)
4779 goto out;
4780 memcpy(buf, acl->data, acl->len);
4781out_len:
4782 ret = acl->len;
4783out:
4784 spin_unlock(&inode->i_lock);
4785 return ret;
4786}
4787
Sachin Prabhu5794d212012-04-17 14:36:40 +01004788static 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 +00004789{
4790 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004791 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004792
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004793 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004794 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004795 if (acl == NULL)
4796 goto out;
4797 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004798 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004799 } else {
4800 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4801 if (acl == NULL)
4802 goto out;
4803 acl->cached = 0;
4804 }
4805 acl->len = acl_len;
4806out:
4807 nfs4_set_cached_acl(inode, acl);
4808}
4809
Andy Adamsonbf118a32011-12-07 11:55:27 -05004810/*
4811 * The getxattr API returns the required buffer length when called with a
4812 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4813 * the required buf. On a NULL buf, we send a page of data to the server
4814 * guessing that the ACL request can be serviced by a page. If so, we cache
4815 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4816 * the cache. If not so, we throw away the page, and cache the required
4817 * length. The next getxattr call will then produce another round trip to
4818 * the server, this time with the input buf of the required size.
4819 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004820static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004821{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004822 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004823 struct nfs_getaclargs args = {
4824 .fh = NFS_FH(inode),
4825 .acl_pages = pages,
4826 .acl_len = buflen,
4827 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004828 struct nfs_getaclres res = {
4829 .acl_len = buflen,
4830 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004831 struct rpc_message msg = {
4832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4833 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004834 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004835 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004836 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4837 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004838
Andy Adamsonbf118a32011-12-07 11:55:27 -05004839 /* As long as we're doing a round trip to the server anyway,
4840 * let's be prepared for a page of acl data. */
4841 if (npages == 0)
4842 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004843 if (npages > ARRAY_SIZE(pages))
4844 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004845
Andy Adamsonbf118a32011-12-07 11:55:27 -05004846 for (i = 0; i < npages; i++) {
4847 pages[i] = alloc_page(GFP_KERNEL);
4848 if (!pages[i])
4849 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004850 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004851
4852 /* for decoding across pages */
4853 res.acl_scratch = alloc_page(GFP_KERNEL);
4854 if (!res.acl_scratch)
4855 goto out_free;
4856
Andy Adamsonbf118a32011-12-07 11:55:27 -05004857 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004858
Peng Taode040be2012-01-10 22:42:47 +08004859 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004860 __func__, buf, buflen, npages, args.acl_len);
4861 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4862 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004863 if (ret)
4864 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004865
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004866 /* Handle the case where the passed-in buffer is too short */
4867 if (res.acl_flags & NFS4_ACL_TRUNC) {
4868 /* Did the user only issue a request for the acl length? */
4869 if (buf == NULL)
4870 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004871 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004872 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004873 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004874 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004875 if (buf) {
4876 if (res.acl_len > buflen) {
4877 ret = -ERANGE;
4878 goto out_free;
4879 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004880 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004881 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004882out_ok:
4883 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004884out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004885 for (i = 0; i < npages; i++)
4886 if (pages[i])
4887 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004888 if (res.acl_scratch)
4889 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004890 return ret;
4891}
4892
Trond Myklebust16b42892006-08-24 12:27:15 -04004893static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4894{
4895 struct nfs4_exception exception = { };
4896 ssize_t ret;
4897 do {
4898 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004899 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004900 if (ret >= 0)
4901 break;
4902 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4903 } while (exception.retry);
4904 return ret;
4905}
4906
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004907static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4908{
4909 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004910 int ret;
4911
4912 if (!nfs4_server_supports_acls(server))
4913 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004914 ret = nfs_revalidate_inode(server, inode);
4915 if (ret < 0)
4916 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004917 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4918 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004919 ret = nfs4_read_cached_acl(inode, buf, buflen);
4920 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004921 /* -ENOENT is returned if there is no ACL or if there is an ACL
4922 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004923 return ret;
4924 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004925}
4926
Trond Myklebust16b42892006-08-24 12:27:15 -04004927static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004928{
4929 struct nfs_server *server = NFS_SERVER(inode);
4930 struct page *pages[NFS4ACL_MAXPAGES];
4931 struct nfs_setaclargs arg = {
4932 .fh = NFS_FH(inode),
4933 .acl_pages = pages,
4934 .acl_len = buflen,
4935 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004936 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004937 struct rpc_message msg = {
4938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4939 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004940 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004941 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004942 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004943 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004944
4945 if (!nfs4_server_supports_acls(server))
4946 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004947 if (npages > ARRAY_SIZE(pages))
4948 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004949 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004950 if (i < 0)
4951 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004952 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004953 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004954
4955 /*
4956 * Free each page after tx, so the only ref left is
4957 * held by the network stack
4958 */
4959 for (; i > 0; i--)
4960 put_page(pages[i-1]);
4961
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004962 /*
4963 * Acl update can result in inode attribute update.
4964 * so mark the attribute cache invalid.
4965 */
4966 spin_lock(&inode->i_lock);
4967 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4968 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004969 nfs_access_zap_cache(inode);
4970 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004971 return ret;
4972}
4973
Trond Myklebust16b42892006-08-24 12:27:15 -04004974static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4975{
4976 struct nfs4_exception exception = { };
4977 int err;
4978 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004979 err = __nfs4_proc_set_acl(inode, buf, buflen);
4980 trace_nfs4_set_acl(inode, err);
4981 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004982 &exception);
4983 } while (exception.retry);
4984 return err;
4985}
4986
David Quigleyaa9c2662013-05-22 12:50:44 -04004987#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4988static int _nfs4_get_security_label(struct inode *inode, void *buf,
4989 size_t buflen)
4990{
4991 struct nfs_server *server = NFS_SERVER(inode);
4992 struct nfs_fattr fattr;
4993 struct nfs4_label label = {0, 0, buflen, buf};
4994
4995 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004996 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004997 .fh = NFS_FH(inode),
4998 .bitmask = bitmask,
4999 };
5000 struct nfs4_getattr_res res = {
5001 .fattr = &fattr,
5002 .label = &label,
5003 .server = server,
5004 };
5005 struct rpc_message msg = {
5006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005007 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005008 .rpc_resp = &res,
5009 };
5010 int ret;
5011
5012 nfs_fattr_init(&fattr);
5013
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005014 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005015 if (ret)
5016 return ret;
5017 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5018 return -ENOENT;
5019 if (buflen < label.len)
5020 return -ERANGE;
5021 return 0;
5022}
5023
5024static int nfs4_get_security_label(struct inode *inode, void *buf,
5025 size_t buflen)
5026{
5027 struct nfs4_exception exception = { };
5028 int err;
5029
5030 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5031 return -EOPNOTSUPP;
5032
5033 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005034 err = _nfs4_get_security_label(inode, buf, buflen);
5035 trace_nfs4_get_security_label(inode, err);
5036 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005037 &exception);
5038 } while (exception.retry);
5039 return err;
5040}
5041
5042static int _nfs4_do_set_security_label(struct inode *inode,
5043 struct nfs4_label *ilabel,
5044 struct nfs_fattr *fattr,
5045 struct nfs4_label *olabel)
5046{
5047
5048 struct iattr sattr = {0};
5049 struct nfs_server *server = NFS_SERVER(inode);
5050 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005051 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005052 .fh = NFS_FH(inode),
5053 .iap = &sattr,
5054 .server = server,
5055 .bitmask = bitmask,
5056 .label = ilabel,
5057 };
5058 struct nfs_setattrres res = {
5059 .fattr = fattr,
5060 .label = olabel,
5061 .server = server,
5062 };
5063 struct rpc_message msg = {
5064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005065 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005066 .rpc_resp = &res,
5067 };
5068 int status;
5069
Jeff Layton12207f62013-11-01 10:49:32 -04005070 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005071
Jeff Layton12207f62013-11-01 10:49:32 -04005072 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005073 if (status)
5074 dprintk("%s failed: %d\n", __func__, status);
5075
5076 return status;
5077}
5078
5079static int nfs4_do_set_security_label(struct inode *inode,
5080 struct nfs4_label *ilabel,
5081 struct nfs_fattr *fattr,
5082 struct nfs4_label *olabel)
5083{
5084 struct nfs4_exception exception = { };
5085 int err;
5086
5087 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005088 err = _nfs4_do_set_security_label(inode, ilabel,
5089 fattr, olabel);
5090 trace_nfs4_set_security_label(inode, err);
5091 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005092 &exception);
5093 } while (exception.retry);
5094 return err;
5095}
5096
5097static int
Al Viro59301222016-05-27 10:19:30 -04005098nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005099{
5100 struct nfs4_label ilabel, *olabel = NULL;
5101 struct nfs_fattr fattr;
5102 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005103 int status;
5104
5105 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5106 return -EOPNOTSUPP;
5107
5108 nfs_fattr_init(&fattr);
5109
5110 ilabel.pi = 0;
5111 ilabel.lfs = 0;
5112 ilabel.label = (char *)buf;
5113 ilabel.len = buflen;
5114
5115 cred = rpc_lookup_cred();
5116 if (IS_ERR(cred))
5117 return PTR_ERR(cred);
5118
5119 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5120 if (IS_ERR(olabel)) {
5121 status = -PTR_ERR(olabel);
5122 goto out;
5123 }
5124
5125 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5126 if (status == 0)
5127 nfs_setsecurity(inode, &fattr, olabel);
5128
5129 nfs4_label_free(olabel);
5130out:
5131 put_rpccred(cred);
5132 return status;
5133}
5134#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5135
5136
Chuck Leverf0920752012-05-21 22:45:41 -04005137static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5138 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005139{
5140 __be32 verf[2];
5141
Chuck Lever2c820d92012-05-21 22:45:33 -04005142 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5143 /* An impossible timestamp guarantees this value
5144 * will never match a generated boot time. */
5145 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005146 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005147 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005148 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005149 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5150 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005151 }
Chuck Levercd937102012-03-02 17:14:31 -05005152 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5153}
5154
Jeff Laytona3192682015-06-09 19:43:59 -04005155static int
5156nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005157{
Jeff Laytona3192682015-06-09 19:43:59 -04005158 size_t len;
5159 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005160
Trond Myklebustceb3a162015-01-03 15:16:04 -05005161 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005162 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005163
Jeff Laytona3192682015-06-09 19:43:59 -04005164 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005165 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005166 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5167 1 +
5168 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5169 1;
5170 rcu_read_unlock();
5171
5172 if (len > NFS4_OPAQUE_LIMIT + 1)
5173 return -EINVAL;
5174
5175 /*
5176 * Since this string is allocated at mount time, and held until the
5177 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5178 * about a memory-reclaim deadlock.
5179 */
5180 str = kmalloc(len, GFP_KERNEL);
5181 if (!str)
5182 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005183
Chuck Levere984a552012-09-14 17:24:21 -04005184 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005185 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005186 clp->cl_ipaddr,
5187 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5188 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005189 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005190
Jeff Laytona3192682015-06-09 19:43:59 -04005191 clp->cl_owner_id = str;
5192 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005193}
5194
Jeff Layton873e3852015-06-09 19:44:00 -04005195static int
5196nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005197{
Jeff Layton873e3852015-06-09 19:44:00 -04005198 size_t len;
5199 char *str;
5200
5201 len = 10 + 10 + 1 + 10 + 1 +
5202 strlen(nfs4_client_id_uniquifier) + 1 +
5203 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5204
5205 if (len > NFS4_OPAQUE_LIMIT + 1)
5206 return -EINVAL;
5207
5208 /*
5209 * Since this string is allocated at mount time, and held until the
5210 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5211 * about a memory-reclaim deadlock.
5212 */
5213 str = kmalloc(len, GFP_KERNEL);
5214 if (!str)
5215 return -ENOMEM;
5216
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005217 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005218 clp->rpc_ops->version, clp->cl_minorversion,
5219 nfs4_client_id_uniquifier,
5220 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005221 clp->cl_owner_id = str;
5222 return 0;
5223}
5224
5225static int
5226nfs4_init_uniform_client_string(struct nfs_client *clp)
5227{
Jeff Layton873e3852015-06-09 19:44:00 -04005228 size_t len;
5229 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005230
5231 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005232 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005233
5234 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005235 return nfs4_init_uniquifier_client_string(clp);
5236
5237 len = 10 + 10 + 1 + 10 + 1 +
5238 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5239
5240 if (len > NFS4_OPAQUE_LIMIT + 1)
5241 return -EINVAL;
5242
5243 /*
5244 * Since this string is allocated at mount time, and held until the
5245 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5246 * about a memory-reclaim deadlock.
5247 */
5248 str = kmalloc(len, GFP_KERNEL);
5249 if (!str)
5250 return -ENOMEM;
5251
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005252 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005253 clp->rpc_ops->version, clp->cl_minorversion,
5254 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005255 clp->cl_owner_id = str;
5256 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005257}
5258
Chuck Lever706cb8d2014-03-12 12:51:47 -04005259/*
5260 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5261 * services. Advertise one based on the address family of the
5262 * clientaddr.
5263 */
5264static unsigned int
5265nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5266{
5267 if (strchr(clp->cl_ipaddr, ':') != NULL)
5268 return scnprintf(buf, len, "tcp6");
5269 else
5270 return scnprintf(buf, len, "tcp");
5271}
5272
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005273static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5274{
5275 struct nfs4_setclientid *sc = calldata;
5276
5277 if (task->tk_status == 0)
5278 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5279}
5280
5281static const struct rpc_call_ops nfs4_setclientid_ops = {
5282 .rpc_call_done = nfs4_setclientid_done,
5283};
5284
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005285/**
5286 * nfs4_proc_setclientid - Negotiate client ID
5287 * @clp: state data structure
5288 * @program: RPC program for NFSv4 callback service
5289 * @port: IP port number for NFS4 callback service
5290 * @cred: RPC credential to use for this call
5291 * @res: where to place the result
5292 *
5293 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5294 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005295int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5296 unsigned short port, struct rpc_cred *cred,
5297 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
5299 nfs4_verifier sc_verifier;
5300 struct nfs4_setclientid setclientid = {
5301 .sc_verifier = &sc_verifier,
5302 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005303 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 };
5305 struct rpc_message msg = {
5306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5307 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005308 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005309 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005311 struct rpc_task *task;
5312 struct rpc_task_setup task_setup_data = {
5313 .rpc_client = clp->cl_rpcclient,
5314 .rpc_message = &msg,
5315 .callback_ops = &nfs4_setclientid_ops,
5316 .callback_data = &setclientid,
5317 .flags = RPC_TASK_TIMEOUT,
5318 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005319 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Chuck Leverde734832012-07-11 16:30:50 -04005321 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005322 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005323
5324 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5325 status = nfs4_init_uniform_client_string(clp);
5326 else
Jeff Laytona3192682015-06-09 19:43:59 -04005327 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005328
5329 if (status)
5330 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005331
Chuck Leverde734832012-07-11 16:30:50 -04005332 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005333 setclientid.sc_netid_len =
5334 nfs4_init_callback_netid(clp,
5335 setclientid.sc_netid,
5336 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005337 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005338 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 clp->cl_ipaddr, port >> 8, port & 255);
5340
Jeff Layton3a6bb732015-06-09 19:43:57 -04005341 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005342 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005343 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005344 task = rpc_run_task(&task_setup_data);
5345 if (IS_ERR(task)) {
5346 status = PTR_ERR(task);
5347 goto out;
5348 }
5349 status = task->tk_status;
5350 if (setclientid.sc_cred) {
5351 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5352 put_rpccred(setclientid.sc_cred);
5353 }
5354 rpc_put_task(task);
5355out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005356 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005357 dprintk("NFS reply setclientid: %d\n", status);
5358 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005361/**
5362 * nfs4_proc_setclientid_confirm - Confirm client ID
5363 * @clp: state data structure
5364 * @res: result of a previous SETCLIENTID
5365 * @cred: RPC credential to use for this call
5366 *
5367 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5368 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005369int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005370 struct nfs4_setclientid_res *arg,
5371 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 struct rpc_message msg = {
5374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005375 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005376 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 int status;
5379
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005380 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5381 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5382 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005383 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005384 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005385 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 return status;
5387}
5388
Trond Myklebustfe650402006-01-03 09:55:18 +01005389struct nfs4_delegreturndata {
5390 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005391 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005392 struct nfs_fh fh;
5393 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005394 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005395 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005396 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005397 struct inode *inode;
5398 bool roc;
5399 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005400};
5401
Trond Myklebustfe650402006-01-03 09:55:18 +01005402static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5403{
5404 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005405
Trond Myklebust14516c32010-07-31 14:29:06 -04005406 if (!nfs4_sequence_done(task, &data->res.seq_res))
5407 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005408
Trond Myklebustca8acf82013-08-13 10:36:56 -04005409 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005410 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005411 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005412 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005413 case -NFS4ERR_ADMIN_REVOKED:
5414 case -NFS4ERR_DELEG_REVOKED:
5415 case -NFS4ERR_BAD_STATEID:
5416 case -NFS4ERR_OLD_STATEID:
5417 case -NFS4ERR_STALE_STATEID:
5418 case -NFS4ERR_EXPIRED:
5419 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005420 if (data->roc)
5421 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005422 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005423 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005424 if (nfs4_async_handle_error(task, data->res.server,
5425 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005426 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005427 return;
5428 }
5429 }
5430 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005431}
5432
5433static void nfs4_delegreturn_release(void *calldata)
5434{
Peng Tao039b7562014-07-03 13:05:02 +08005435 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005436 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005437
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005438 if (inode) {
5439 if (data->roc)
5440 pnfs_roc_release(inode);
5441 nfs_iput_and_deactive(inode);
5442 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005443 kfree(calldata);
5444}
5445
Andy Adamson938e1012009-04-01 09:22:28 -04005446static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5447{
5448 struct nfs4_delegreturndata *d_data;
5449
5450 d_data = (struct nfs4_delegreturndata *)data;
5451
Peng Tao500d7012015-09-22 11:35:22 +08005452 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5453 return;
5454
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005455 if (d_data->roc)
5456 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005457
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005458 nfs4_setup_sequence(d_data->res.server,
5459 &d_data->args.seq_args,
5460 &d_data->res.seq_res,
5461 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005462}
Andy Adamson938e1012009-04-01 09:22:28 -04005463
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005464static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005465 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005466 .rpc_call_done = nfs4_delegreturn_done,
5467 .rpc_release = nfs4_delegreturn_release,
5468};
5469
Trond Myklebuste6f81072008-01-24 18:14:34 -05005470static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005471{
5472 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005473 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005474 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005475 struct rpc_message msg = {
5476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5477 .rpc_cred = cred,
5478 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005479 struct rpc_task_setup task_setup_data = {
5480 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005481 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005482 .callback_ops = &nfs4_delegreturn_ops,
5483 .flags = RPC_TASK_ASYNC,
5484 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005485 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005486
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005487 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005488 if (data == NULL)
5489 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005490 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005491
5492 nfs4_state_protect(server->nfs_client,
5493 NFS_SP4_MACH_CRED_CLEANUP,
5494 &task_setup_data.rpc_client, &msg);
5495
Trond Myklebustfe650402006-01-03 09:55:18 +01005496 data->args.fhandle = &data->fh;
5497 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005498 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005499 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005500 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005501 data->res.fattr = &data->fattr;
5502 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005503 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005504 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005505 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005506 data->inode = nfs_igrab_and_active(inode);
5507 if (data->inode)
5508 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005509
Trond Myklebustc970aa82007-07-14 15:39:59 -04005510 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005511 msg.rpc_argp = &data->args;
5512 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005513 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005514 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005515 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005516 if (!issync)
5517 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005518 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005519 if (status != 0)
5520 goto out;
5521 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005522 if (status == 0)
5523 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5524 else
5525 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005526out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005527 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005528 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529}
5530
Trond Myklebuste6f81072008-01-24 18:14:34 -05005531int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
5533 struct nfs_server *server = NFS_SERVER(inode);
5534 struct nfs4_exception exception = { };
5535 int err;
5536 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005537 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005538 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 switch (err) {
5540 case -NFS4ERR_STALE_STATEID:
5541 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 case 0:
5543 return 0;
5544 }
5545 err = nfs4_handle_exception(server, err, &exception);
5546 } while (exception.retry);
5547 return err;
5548}
5549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5551{
5552 struct inode *inode = state->inode;
5553 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005554 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005555 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005557 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005559 struct nfs_lockt_res res = {
5560 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 };
5562 struct rpc_message msg = {
5563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5564 .rpc_argp = &arg,
5565 .rpc_resp = &res,
5566 .rpc_cred = state->owner->so_cred,
5567 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 struct nfs4_lock_state *lsp;
5569 int status;
5570
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005571 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005572 status = nfs4_set_lock_state(state, request);
5573 if (status != 0)
5574 goto out;
5575 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005576 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005577 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005578 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005579 switch (status) {
5580 case 0:
5581 request->fl_type = F_UNLCK;
5582 break;
5583 case -NFS4ERR_DENIED:
5584 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005586 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005587 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005588out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 return status;
5590}
5591
5592static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5593{
5594 struct nfs4_exception exception = { };
5595 int err;
5596
5597 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005598 err = _nfs4_proc_getlk(state, cmd, request);
5599 trace_nfs4_get_lock(request, state, cmd, err);
5600 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 &exception);
5602 } while (exception.retry);
5603 return err;
5604}
5605
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005606struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005607 struct nfs_locku_args arg;
5608 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005609 struct nfs4_lock_state *lsp;
5610 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005611 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005612 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005613 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005614};
5615
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005616static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5617 struct nfs_open_context *ctx,
5618 struct nfs4_lock_state *lsp,
5619 struct nfs_seqid *seqid)
5620{
5621 struct nfs4_unlockdata *p;
5622 struct inode *inode = lsp->ls_state->inode;
5623
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005624 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005625 if (p == NULL)
5626 return NULL;
5627 p->arg.fh = NFS_FH(inode);
5628 p->arg.fl = &p->fl;
5629 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005630 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005631 p->lsp = lsp;
5632 atomic_inc(&lsp->ls_count);
5633 /* Ensure we don't close file until we're done freeing locks! */
5634 p->ctx = get_nfs_open_context(ctx);
5635 memcpy(&p->fl, fl, sizeof(p->fl));
5636 p->server = NFS_SERVER(inode);
5637 return p;
5638}
5639
Trond Myklebust06f814a2006-01-03 09:55:07 +01005640static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005641{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005642 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005643 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005644 nfs4_put_lock_state(calldata->lsp);
5645 put_nfs_open_context(calldata->ctx);
5646 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005647}
5648
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005649static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005650{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005651 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005652
Trond Myklebust14516c32010-07-31 14:29:06 -04005653 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5654 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005655 switch (task->tk_status) {
5656 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005657 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005658 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005659 if (nfs4_update_lock_stateid(calldata->lsp,
5660 &calldata->res.stateid))
5661 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005662 case -NFS4ERR_BAD_STATEID:
5663 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005664 case -NFS4ERR_STALE_STATEID:
5665 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005666 if (!nfs4_stateid_match(&calldata->arg.stateid,
5667 &calldata->lsp->ls_stateid))
5668 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005669 break;
5670 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005671 if (nfs4_async_handle_error(task, calldata->server,
5672 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005673 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005674 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005675 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005676}
5677
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005678static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005679{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005680 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005682 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005683 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005684 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005685 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005686 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005687 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005688 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005689 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005690 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005691 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005692 &calldata->res.seq_res,
5693 task) != 0)
5694 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005695 return;
5696out_no_action:
5697 task->tk_action = NULL;
5698out_wait:
5699 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700}
5701
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005702static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005703 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005704 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005705 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005706};
5707
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005708static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5709 struct nfs_open_context *ctx,
5710 struct nfs4_lock_state *lsp,
5711 struct nfs_seqid *seqid)
5712{
5713 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005714 struct rpc_message msg = {
5715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5716 .rpc_cred = ctx->cred,
5717 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005718 struct rpc_task_setup task_setup_data = {
5719 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005720 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005721 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005722 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005723 .flags = RPC_TASK_ASYNC,
5724 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005725
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005726 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5727 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5728
Frank Filz137d6ac2007-07-09 15:32:29 -07005729 /* Ensure this is an unlock - when canceling a lock, the
5730 * canceled lock is passed in, and it won't be an unlock.
5731 */
5732 fl->fl_type = F_UNLCK;
5733
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005734 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5735 if (data == NULL) {
5736 nfs_free_seqid(seqid);
5737 return ERR_PTR(-ENOMEM);
5738 }
5739
Chuck Levera9c92d62013-08-09 12:48:18 -04005740 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005741 msg.rpc_argp = &data->arg;
5742 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005743 task_setup_data.callback_data = data;
5744 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005745}
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5748{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005749 struct inode *inode = state->inode;
5750 struct nfs4_state_owner *sp = state->owner;
5751 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005752 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005753 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005754 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005755 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005756 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005757 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Trond Myklebust9b073572006-06-29 16:38:34 -04005759 status = nfs4_set_lock_state(state, request);
5760 /* Unlock _before_ we do the RPC call */
5761 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005762 /* Exclude nfs_delegation_claim_locks() */
5763 mutex_lock(&sp->so_delegreturn_mutex);
5764 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005765 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005766 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005767 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005768 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005769 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005770 }
5771 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005772 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005773 if (status != 0)
5774 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005775 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005776 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005777 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5778 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005779 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5780 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005781 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005782 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005783 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005784 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005785 status = PTR_ERR(task);
5786 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005787 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005788 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005789 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005790out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005791 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005792 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005793 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794}
5795
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005796struct nfs4_lockdata {
5797 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005798 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005799 struct nfs4_lock_state *lsp;
5800 struct nfs_open_context *ctx;
5801 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005802 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005803 int rpc_status;
5804 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005805 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005806};
5807
5808static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005809 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5810 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005811{
5812 struct nfs4_lockdata *p;
5813 struct inode *inode = lsp->ls_state->inode;
5814 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005815 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005816
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005817 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005818 if (p == NULL)
5819 return NULL;
5820
5821 p->arg.fh = NFS_FH(inode);
5822 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005823 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005824 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005825 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005826 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5827 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005828 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005829 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005830 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005831 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005832 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005833 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005834 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005835 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005836 atomic_inc(&lsp->ls_count);
5837 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005838 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005839 memcpy(&p->fl, fl, sizeof(p->fl));
5840 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005841out_free_seqid:
5842 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005843out_free:
5844 kfree(p);
5845 return NULL;
5846}
5847
5848static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5849{
5850 struct nfs4_lockdata *data = calldata;
5851 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Harvey Harrison3110ff82008-05-02 13:42:44 -07005853 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005854 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005855 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005856 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005857 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005858 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005859 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005860 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005861 nfs4_stateid_copy(&data->arg.open_stateid,
5862 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005863 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005864 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005865 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005866 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005867 nfs4_stateid_copy(&data->arg.lock_stateid,
5868 &data->lsp->ls_stateid);
5869 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005870 if (!nfs4_valid_open_stateid(state)) {
5871 data->rpc_status = -EBADF;
5872 task->tk_action = NULL;
5873 goto out_release_open_seqid;
5874 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005875 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005876 if (nfs4_setup_sequence(data->server,
5877 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005878 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005879 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005880 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005881out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005882 nfs_release_seqid(data->arg.open_seqid);
5883out_release_lock_seqid:
5884 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005885out_wait:
5886 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005887 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005888}
5889
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005890static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5891{
5892 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005893 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005894
Harvey Harrison3110ff82008-05-02 13:42:44 -07005895 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005896
Trond Myklebust14516c32010-07-31 14:29:06 -04005897 if (!nfs4_sequence_done(task, &data->res.seq_res))
5898 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005899
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005900 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005901 switch (task->tk_status) {
5902 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005903 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005904 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005905 if (data->arg.new_lock) {
5906 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04005907 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005908 rpc_restart_call_prepare(task);
5909 break;
5910 }
5911 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005912 if (data->arg.new_lock_owner != 0) {
5913 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5914 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5915 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5916 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5917 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005918 break;
5919 case -NFS4ERR_BAD_STATEID:
5920 case -NFS4ERR_OLD_STATEID:
5921 case -NFS4ERR_STALE_STATEID:
5922 case -NFS4ERR_EXPIRED:
5923 if (data->arg.new_lock_owner != 0) {
5924 if (!nfs4_stateid_match(&data->arg.open_stateid,
5925 &lsp->ls_state->open_stateid))
5926 rpc_restart_call_prepare(task);
5927 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5928 &lsp->ls_stateid))
5929 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005931 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005932}
5933
5934static void nfs4_lock_release(void *calldata)
5935{
5936 struct nfs4_lockdata *data = calldata;
5937
Harvey Harrison3110ff82008-05-02 13:42:44 -07005938 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005939 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005940 if (data->cancelled != 0) {
5941 struct rpc_task *task;
5942 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5943 data->arg.lock_seqid);
5944 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005945 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005946 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005947 } else
5948 nfs_free_seqid(data->arg.lock_seqid);
5949 nfs4_put_lock_state(data->lsp);
5950 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005951 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005952 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005953 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005954}
5955
5956static const struct rpc_call_ops nfs4_lock_ops = {
5957 .rpc_call_prepare = nfs4_lock_prepare,
5958 .rpc_call_done = nfs4_lock_done,
5959 .rpc_release = nfs4_lock_release,
5960};
5961
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005962static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5963{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005964 switch (error) {
5965 case -NFS4ERR_ADMIN_REVOKED:
5966 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005967 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005968 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005969 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005970 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005971 break;
5972 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005973 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005974 case -NFS4ERR_EXPIRED:
5975 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005976 };
5977}
5978
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005979static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005980{
5981 struct nfs4_lockdata *data;
5982 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005983 struct rpc_message msg = {
5984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5985 .rpc_cred = state->owner->so_cred,
5986 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005987 struct rpc_task_setup task_setup_data = {
5988 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005989 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005990 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005991 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005992 .flags = RPC_TASK_ASYNC,
5993 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005994 int ret;
5995
Harvey Harrison3110ff82008-05-02 13:42:44 -07005996 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005997 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005998 fl->fl_u.nfs4_fl.owner,
5999 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000 if (data == NULL)
6001 return -ENOMEM;
6002 if (IS_SETLKW(cmd))
6003 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006004 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006005 msg.rpc_argp = &data->arg;
6006 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006007 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006008 if (recovery_type > NFS_LOCK_NEW) {
6009 if (recovery_type == NFS_LOCK_RECLAIM)
6010 data->arg.reclaim = NFS_LOCK_RECLAIM;
6011 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006012 } else
6013 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006014 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006015 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006016 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006017 ret = nfs4_wait_for_completion_rpc_task(task);
6018 if (ret == 0) {
6019 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006020 if (ret)
6021 nfs4_handle_setlk_error(data->server, data->lsp,
6022 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006023 } else
6024 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006025 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006026 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006027 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006028 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
6031static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6032{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006033 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006034 struct nfs4_exception exception = {
6035 .inode = state->inode,
6036 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006037 int err;
6038
6039 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006040 /* Cache the lock if possible... */
6041 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6042 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006043 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006044 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006045 break;
6046 nfs4_handle_exception(server, err, &exception);
6047 } while (exception.retry);
6048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049}
6050
6051static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6052{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006053 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006054 struct nfs4_exception exception = {
6055 .inode = state->inode,
6056 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006057 int err;
6058
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006059 err = nfs4_set_lock_state(state, request);
6060 if (err != 0)
6061 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006062 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006063 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6064 return 0;
6065 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006066 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006067 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6068 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006069 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006070 switch (err) {
6071 default:
6072 goto out;
6073 case -NFS4ERR_GRACE:
6074 case -NFS4ERR_DELAY:
6075 nfs4_handle_exception(server, err, &exception);
6076 err = 0;
6077 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006078 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006079out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006080 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081}
6082
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006083#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006084/**
6085 * nfs41_check_expired_locks - possibly free a lock stateid
6086 *
6087 * @state: NFSv4 state for an inode
6088 *
6089 * Returns NFS_OK if recovery for this stateid is now finished.
6090 * Otherwise a negative NFS4ERR value is returned.
6091 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006092static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006093{
Chuck Levereb64cf92012-07-11 16:30:05 -04006094 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006095 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006096 struct nfs_server *server = NFS_SERVER(state->inode);
6097
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006098 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006099 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006100 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6101
Trond Myklebust4586f6e2016-09-22 13:38:57 -04006102 status = nfs41_test_and_free_expired_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006103 &lsp->ls_stateid,
6104 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006105 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006106 if (status != NFS_OK) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006107 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006108 ret = status;
6109 }
6110 }
6111 };
6112
6113 return ret;
6114}
6115
6116static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6117{
6118 int status = NFS_OK;
6119
6120 if (test_bit(LK_STATE_IN_USE, &state->flags))
6121 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006122 if (status != NFS_OK)
6123 status = nfs4_lock_expired(state, request);
6124 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006125}
6126#endif
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6129{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006130 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006131 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006132 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006133 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Trond Myklebust01c3b862006-06-29 16:38:39 -04006135 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006136 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006137 if (status < 0)
6138 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006139 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006140 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006141 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006142 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006143 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006144 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006145 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006146 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006147 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006148 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006149 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006150 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006151 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006152 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006153out:
6154 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 return status;
6156}
6157
6158static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6159{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006160 struct nfs4_exception exception = {
6161 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006162 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006163 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 int err;
6165
6166 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006167 err = _nfs4_proc_setlk(state, cmd, request);
6168 if (err == -NFS4ERR_DENIED)
6169 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006171 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 } while (exception.retry);
6173 return err;
6174}
6175
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006176#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6177#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6178
6179static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006180nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6181 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006182{
6183 int status = -ERESTARTSYS;
6184 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6185
6186 while(!signalled()) {
6187 status = nfs4_proc_setlk(state, cmd, request);
6188 if ((status != -EAGAIN) || IS_SETLK(cmd))
6189 break;
6190 freezable_schedule_timeout_interruptible(timeout);
6191 timeout *= 2;
6192 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6193 status = -ERESTARTSYS;
6194 }
6195 return status;
6196}
6197
Jeff Laytona1d617d82016-09-17 18:17:39 -04006198#ifdef CONFIG_NFS_V4_1
6199struct nfs4_lock_waiter {
6200 struct task_struct *task;
6201 struct inode *inode;
6202 struct nfs_lowner *owner;
6203 bool notified;
6204};
6205
6206static int
6207nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6208{
6209 int ret;
6210 struct cb_notify_lock_args *cbnl = key;
6211 struct nfs4_lock_waiter *waiter = wait->private;
6212 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6213 *wowner = waiter->owner;
6214
6215 /* Only wake if the callback was for the same owner */
6216 if (lowner->clientid != wowner->clientid ||
6217 lowner->id != wowner->id ||
6218 lowner->s_dev != wowner->s_dev)
6219 return 0;
6220
6221 /* Make sure it's for the right inode */
6222 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6223 return 0;
6224
6225 waiter->notified = true;
6226
6227 /* override "private" so we can use default_wake_function */
6228 wait->private = waiter->task;
6229 ret = autoremove_wake_function(wait, mode, flags, key);
6230 wait->private = waiter;
6231 return ret;
6232}
6233
6234static int
6235nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6236{
6237 int status = -ERESTARTSYS;
6238 unsigned long flags;
6239 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6240 struct nfs_server *server = NFS_SERVER(state->inode);
6241 struct nfs_client *clp = server->nfs_client;
6242 wait_queue_head_t *q = &clp->cl_lock_waitq;
6243 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6244 .id = lsp->ls_seqid.owner_id,
6245 .s_dev = server->s_dev };
6246 struct nfs4_lock_waiter waiter = { .task = current,
6247 .inode = state->inode,
6248 .owner = &owner,
6249 .notified = false };
6250 wait_queue_t wait;
6251
6252 /* Don't bother with waitqueue if we don't expect a callback */
6253 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6254 return nfs4_retry_setlk_simple(state, cmd, request);
6255
6256 init_wait(&wait);
6257 wait.private = &waiter;
6258 wait.func = nfs4_wake_lock_waiter;
6259 add_wait_queue(q, &wait);
6260
6261 while(!signalled()) {
6262 status = nfs4_proc_setlk(state, cmd, request);
6263 if ((status != -EAGAIN) || IS_SETLK(cmd))
6264 break;
6265
6266 status = -ERESTARTSYS;
6267 spin_lock_irqsave(&q->lock, flags);
6268 if (waiter.notified) {
6269 spin_unlock_irqrestore(&q->lock, flags);
6270 continue;
6271 }
6272 set_current_state(TASK_INTERRUPTIBLE);
6273 spin_unlock_irqrestore(&q->lock, flags);
6274
6275 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6276 }
6277
6278 finish_wait(q, &wait);
6279 return status;
6280}
6281#else /* !CONFIG_NFS_V4_1 */
6282static inline int
6283nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6284{
6285 return nfs4_retry_setlk_simple(state, cmd, request);
6286}
6287#endif
6288
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289static int
6290nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6291{
6292 struct nfs_open_context *ctx;
6293 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 int status;
6295
6296 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006297 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 state = ctx->state;
6299
6300 if (request->fl_start < 0 || request->fl_end < 0)
6301 return -EINVAL;
6302
Trond Myklebustd9531262009-07-21 19:22:38 -04006303 if (IS_GETLK(cmd)) {
6304 if (state != NULL)
6305 return nfs4_proc_getlk(state, F_GETLK, request);
6306 return 0;
6307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308
6309 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6310 return -EINVAL;
6311
Trond Myklebustd9531262009-07-21 19:22:38 -04006312 if (request->fl_type == F_UNLCK) {
6313 if (state != NULL)
6314 return nfs4_proc_unlck(state, cmd, request);
6315 return 0;
6316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Trond Myklebustd9531262009-07-21 19:22:38 -04006318 if (state == NULL)
6319 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006320
6321 if ((request->fl_flags & FL_POSIX) &&
6322 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6323 return -ENOLCK;
6324
Trond Myklebust55725512012-04-18 12:48:35 -04006325 /*
6326 * Don't rely on the VFS having checked the file open mode,
6327 * since it won't do this for flock() locks.
6328 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006329 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006330 case F_RDLCK:
6331 if (!(filp->f_mode & FMODE_READ))
6332 return -EBADF;
6333 break;
6334 case F_WRLCK:
6335 if (!(filp->f_mode & FMODE_WRITE))
6336 return -EBADF;
6337 }
6338
Jeff Layton1ea67db2016-09-17 18:17:37 -04006339 status = nfs4_set_lock_state(state, request);
6340 if (status != 0)
6341 return status;
6342
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006343 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344}
6345
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006346int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006347{
6348 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006349 int err;
6350
6351 err = nfs4_set_lock_state(state, fl);
6352 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006353 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006354 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006355 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006356}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006357
Trond Myklebustcf470c32012-03-07 13:49:12 -05006358struct nfs_release_lockowner_data {
6359 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006360 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006361 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006362 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006363 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006364};
6365
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006366static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6367{
6368 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006369 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006370 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6371 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006372 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006373 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006374}
6375
6376static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6377{
6378 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006379 struct nfs_server *server = data->server;
6380
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006381 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006382
6383 switch (task->tk_status) {
6384 case 0:
6385 renew_lease(server, data->timestamp);
6386 break;
6387 case -NFS4ERR_STALE_CLIENTID:
6388 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006389 nfs4_schedule_lease_recovery(server->nfs_client);
6390 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006391 case -NFS4ERR_LEASE_MOVED:
6392 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006393 if (nfs4_async_handle_error(task, server,
6394 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006395 rpc_restart_call_prepare(task);
6396 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006397}
6398
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006399static void nfs4_release_lockowner_release(void *calldata)
6400{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006401 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006402 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006403 kfree(calldata);
6404}
6405
Trond Myklebust17280172012-03-11 13:11:00 -04006406static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006407 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6408 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006409 .rpc_release = nfs4_release_lockowner_release,
6410};
6411
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006412static void
6413nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006414{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006415 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006416 struct rpc_message msg = {
6417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6418 };
6419
6420 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006421 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006422
Trond Myklebustcf470c32012-03-07 13:49:12 -05006423 data = kmalloc(sizeof(*data), GFP_NOFS);
6424 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006425 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006426 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006427 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006428 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6429 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6430 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006431
Trond Myklebustcf470c32012-03-07 13:49:12 -05006432 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006433 msg.rpc_resp = &data->res;
6434 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006435 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006436}
6437
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006438#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6439
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006440static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006441 struct dentry *unused, struct inode *inode,
6442 const char *key, const void *buf,
6443 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006444{
Al Viro59301222016-05-27 10:19:30 -04006445 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006446}
6447
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006448static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006449 struct dentry *unused, struct inode *inode,
6450 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006451{
Al Virob2968212016-04-10 20:48:24 -04006452 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006453}
6454
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006455static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006456{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006457 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006458}
6459
David Quigleyc9bccef2013-05-22 12:50:45 -04006460#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006461
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006462static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006463 struct dentry *unused, struct inode *inode,
6464 const char *key, const void *buf,
6465 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006466{
6467 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006468 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006469
6470 return -EOPNOTSUPP;
6471}
6472
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006473static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006474 struct dentry *unused, struct inode *inode,
6475 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006476{
6477 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006478 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006479 return -EOPNOTSUPP;
6480}
6481
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006482static ssize_t
6483nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006484{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006485 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006486
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006487 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6488 len = security_inode_listsecurity(inode, list, list_len);
6489 if (list_len && len > list_len)
6490 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006491 }
6492 return len;
6493}
6494
6495static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6496 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006497 .get = nfs4_xattr_get_nfs4_label,
6498 .set = nfs4_xattr_set_nfs4_label,
6499};
David Quigleyc9bccef2013-05-22 12:50:45 -04006500
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006501#else
6502
6503static ssize_t
6504nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6505{
6506 return 0;
6507}
6508
6509#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006510
Andy Adamson533eb462011-06-13 18:25:56 -04006511/*
6512 * nfs_fhget will use either the mounted_on_fileid or the fileid
6513 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006514static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6515{
Andy Adamson533eb462011-06-13 18:25:56 -04006516 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6517 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6518 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006519 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006520 return;
6521
6522 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006523 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006524 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6525 fattr->nlink = 2;
6526}
6527
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006528static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6529 const struct qstr *name,
6530 struct nfs4_fs_locations *fs_locations,
6531 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006532{
6533 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006534 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006535 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006536 };
6537 struct nfs4_fs_locations_arg args = {
6538 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006539 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006540 .page = page,
6541 .bitmask = bitmask,
6542 };
Benny Halevy22958462009-04-01 09:22:02 -04006543 struct nfs4_fs_locations_res res = {
6544 .fs_locations = fs_locations,
6545 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6548 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006549 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006550 };
6551 int status;
6552
Harvey Harrison3110ff82008-05-02 13:42:44 -07006553 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006554
6555 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6556 * is not supported */
6557 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6558 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6559 else
6560 bitmask[0] |= FATTR4_WORD0_FILEID;
6561
Trond Myklebustc228fd32007-01-13 02:28:11 -05006562 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006563 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006564 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006565 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006566 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006567 return status;
6568}
6569
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006570int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6571 const struct qstr *name,
6572 struct nfs4_fs_locations *fs_locations,
6573 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006574{
6575 struct nfs4_exception exception = { };
6576 int err;
6577 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006578 err = _nfs4_proc_fs_locations(client, dir, name,
6579 fs_locations, page);
6580 trace_nfs4_get_fs_locations(dir, name, err);
6581 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006582 &exception);
6583 } while (exception.retry);
6584 return err;
6585}
6586
Chuck Leverb03d7352013-10-17 14:12:50 -04006587/*
6588 * This operation also signals the server that this client is
6589 * performing migration recovery. The server can stop returning
6590 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6591 * appended to this compound to identify the client ID which is
6592 * performing recovery.
6593 */
6594static int _nfs40_proc_get_locations(struct inode *inode,
6595 struct nfs4_fs_locations *locations,
6596 struct page *page, struct rpc_cred *cred)
6597{
6598 struct nfs_server *server = NFS_SERVER(inode);
6599 struct rpc_clnt *clnt = server->client;
6600 u32 bitmask[2] = {
6601 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6602 };
6603 struct nfs4_fs_locations_arg args = {
6604 .clientid = server->nfs_client->cl_clientid,
6605 .fh = NFS_FH(inode),
6606 .page = page,
6607 .bitmask = bitmask,
6608 .migration = 1, /* skip LOOKUP */
6609 .renew = 1, /* append RENEW */
6610 };
6611 struct nfs4_fs_locations_res res = {
6612 .fs_locations = locations,
6613 .migration = 1,
6614 .renew = 1,
6615 };
6616 struct rpc_message msg = {
6617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6618 .rpc_argp = &args,
6619 .rpc_resp = &res,
6620 .rpc_cred = cred,
6621 };
6622 unsigned long now = jiffies;
6623 int status;
6624
6625 nfs_fattr_init(&locations->fattr);
6626 locations->server = server;
6627 locations->nlocations = 0;
6628
6629 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6630 nfs4_set_sequence_privileged(&args.seq_args);
6631 status = nfs4_call_sync_sequence(clnt, server, &msg,
6632 &args.seq_args, &res.seq_res);
6633 if (status)
6634 return status;
6635
6636 renew_lease(server, now);
6637 return 0;
6638}
6639
6640#ifdef CONFIG_NFS_V4_1
6641
6642/*
6643 * This operation also signals the server that this client is
6644 * performing migration recovery. The server can stop asserting
6645 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6646 * performing this operation is identified in the SEQUENCE
6647 * operation in this compound.
6648 *
6649 * When the client supports GETATTR(fs_locations_info), it can
6650 * be plumbed in here.
6651 */
6652static int _nfs41_proc_get_locations(struct inode *inode,
6653 struct nfs4_fs_locations *locations,
6654 struct page *page, struct rpc_cred *cred)
6655{
6656 struct nfs_server *server = NFS_SERVER(inode);
6657 struct rpc_clnt *clnt = server->client;
6658 u32 bitmask[2] = {
6659 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6660 };
6661 struct nfs4_fs_locations_arg args = {
6662 .fh = NFS_FH(inode),
6663 .page = page,
6664 .bitmask = bitmask,
6665 .migration = 1, /* skip LOOKUP */
6666 };
6667 struct nfs4_fs_locations_res res = {
6668 .fs_locations = locations,
6669 .migration = 1,
6670 };
6671 struct rpc_message msg = {
6672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6673 .rpc_argp = &args,
6674 .rpc_resp = &res,
6675 .rpc_cred = cred,
6676 };
6677 int status;
6678
6679 nfs_fattr_init(&locations->fattr);
6680 locations->server = server;
6681 locations->nlocations = 0;
6682
6683 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6684 nfs4_set_sequence_privileged(&args.seq_args);
6685 status = nfs4_call_sync_sequence(clnt, server, &msg,
6686 &args.seq_args, &res.seq_res);
6687 if (status == NFS4_OK &&
6688 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6689 status = -NFS4ERR_LEASE_MOVED;
6690 return status;
6691}
6692
6693#endif /* CONFIG_NFS_V4_1 */
6694
6695/**
6696 * nfs4_proc_get_locations - discover locations for a migrated FSID
6697 * @inode: inode on FSID that is migrating
6698 * @locations: result of query
6699 * @page: buffer
6700 * @cred: credential to use for this operation
6701 *
6702 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6703 * operation failed, or a negative errno if a local error occurred.
6704 *
6705 * On success, "locations" is filled in, but if the server has
6706 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6707 * asserted.
6708 *
6709 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6710 * from this client that require migration recovery.
6711 */
6712int nfs4_proc_get_locations(struct inode *inode,
6713 struct nfs4_fs_locations *locations,
6714 struct page *page, struct rpc_cred *cred)
6715{
6716 struct nfs_server *server = NFS_SERVER(inode);
6717 struct nfs_client *clp = server->nfs_client;
6718 const struct nfs4_mig_recovery_ops *ops =
6719 clp->cl_mvops->mig_recovery_ops;
6720 struct nfs4_exception exception = { };
6721 int status;
6722
6723 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6724 (unsigned long long)server->fsid.major,
6725 (unsigned long long)server->fsid.minor,
6726 clp->cl_hostname);
6727 nfs_display_fhandle(NFS_FH(inode), __func__);
6728
6729 do {
6730 status = ops->get_locations(inode, locations, page, cred);
6731 if (status != -NFS4ERR_DELAY)
6732 break;
6733 nfs4_handle_exception(server, status, &exception);
6734 } while (exception.retry);
6735 return status;
6736}
6737
Chuck Lever44c99932013-10-17 14:13:30 -04006738/*
6739 * This operation also signals the server that this client is
6740 * performing "lease moved" recovery. The server can stop
6741 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6742 * is appended to this compound to identify the client ID which is
6743 * performing recovery.
6744 */
6745static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6746{
6747 struct nfs_server *server = NFS_SERVER(inode);
6748 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6749 struct rpc_clnt *clnt = server->client;
6750 struct nfs4_fsid_present_arg args = {
6751 .fh = NFS_FH(inode),
6752 .clientid = clp->cl_clientid,
6753 .renew = 1, /* append RENEW */
6754 };
6755 struct nfs4_fsid_present_res res = {
6756 .renew = 1,
6757 };
6758 struct rpc_message msg = {
6759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6760 .rpc_argp = &args,
6761 .rpc_resp = &res,
6762 .rpc_cred = cred,
6763 };
6764 unsigned long now = jiffies;
6765 int status;
6766
6767 res.fh = nfs_alloc_fhandle();
6768 if (res.fh == NULL)
6769 return -ENOMEM;
6770
6771 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6772 nfs4_set_sequence_privileged(&args.seq_args);
6773 status = nfs4_call_sync_sequence(clnt, server, &msg,
6774 &args.seq_args, &res.seq_res);
6775 nfs_free_fhandle(res.fh);
6776 if (status)
6777 return status;
6778
6779 do_renew_lease(clp, now);
6780 return 0;
6781}
6782
6783#ifdef CONFIG_NFS_V4_1
6784
6785/*
6786 * This operation also signals the server that this client is
6787 * performing "lease moved" recovery. The server can stop asserting
6788 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6789 * this operation is identified in the SEQUENCE operation in this
6790 * compound.
6791 */
6792static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6793{
6794 struct nfs_server *server = NFS_SERVER(inode);
6795 struct rpc_clnt *clnt = server->client;
6796 struct nfs4_fsid_present_arg args = {
6797 .fh = NFS_FH(inode),
6798 };
6799 struct nfs4_fsid_present_res res = {
6800 };
6801 struct rpc_message msg = {
6802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6803 .rpc_argp = &args,
6804 .rpc_resp = &res,
6805 .rpc_cred = cred,
6806 };
6807 int status;
6808
6809 res.fh = nfs_alloc_fhandle();
6810 if (res.fh == NULL)
6811 return -ENOMEM;
6812
6813 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6814 nfs4_set_sequence_privileged(&args.seq_args);
6815 status = nfs4_call_sync_sequence(clnt, server, &msg,
6816 &args.seq_args, &res.seq_res);
6817 nfs_free_fhandle(res.fh);
6818 if (status == NFS4_OK &&
6819 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6820 status = -NFS4ERR_LEASE_MOVED;
6821 return status;
6822}
6823
6824#endif /* CONFIG_NFS_V4_1 */
6825
6826/**
6827 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6828 * @inode: inode on FSID to check
6829 * @cred: credential to use for this operation
6830 *
6831 * Server indicates whether the FSID is present, moved, or not
6832 * recognized. This operation is necessary to clear a LEASE_MOVED
6833 * condition for this client ID.
6834 *
6835 * Returns NFS4_OK if the FSID is present on this server,
6836 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6837 * NFS4ERR code if some error occurred on the server, or a
6838 * negative errno if a local failure occurred.
6839 */
6840int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6841{
6842 struct nfs_server *server = NFS_SERVER(inode);
6843 struct nfs_client *clp = server->nfs_client;
6844 const struct nfs4_mig_recovery_ops *ops =
6845 clp->cl_mvops->mig_recovery_ops;
6846 struct nfs4_exception exception = { };
6847 int status;
6848
6849 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6850 (unsigned long long)server->fsid.major,
6851 (unsigned long long)server->fsid.minor,
6852 clp->cl_hostname);
6853 nfs_display_fhandle(NFS_FH(inode), __func__);
6854
6855 do {
6856 status = ops->fsid_present(inode, cred);
6857 if (status != -NFS4ERR_DELAY)
6858 break;
6859 nfs4_handle_exception(server, status, &exception);
6860 } while (exception.retry);
6861 return status;
6862}
6863
Andy Adamson5ec16a82013-08-08 10:57:55 -04006864/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006865 * If 'use_integrity' is true and the state managment nfs_client
6866 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6867 * and the machine credential as per RFC3530bis and RFC5661 Security
6868 * Considerations sections. Otherwise, just use the user cred with the
6869 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006870 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006871static 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 +00006872{
6873 int status;
6874 struct nfs4_secinfo_arg args = {
6875 .dir_fh = NFS_FH(dir),
6876 .name = name,
6877 };
6878 struct nfs4_secinfo_res res = {
6879 .flavors = flavors,
6880 };
6881 struct rpc_message msg = {
6882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6883 .rpc_argp = &args,
6884 .rpc_resp = &res,
6885 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006886 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006887 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006888
6889 if (use_integrity) {
6890 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006891 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6892 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006893 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006894
6895 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006896
6897 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6898 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6899
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006900 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6901 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006902 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006903
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006904 if (cred)
6905 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006906
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006907 return status;
6908}
6909
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006910int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6911 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006912{
6913 struct nfs4_exception exception = { };
6914 int err;
6915 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006916 err = -NFS4ERR_WRONGSEC;
6917
6918 /* try to use integrity protection with machine cred */
6919 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6920 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6921
6922 /*
6923 * if unable to use integrity protection, or SECINFO with
6924 * integrity protection returns NFS4ERR_WRONGSEC (which is
6925 * disallowed by spec, but exists in deployed servers) use
6926 * the current filesystem's rpc_client and the user cred.
6927 */
6928 if (err == -NFS4ERR_WRONGSEC)
6929 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6930
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006931 trace_nfs4_secinfo(dir, name, err);
6932 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006933 &exception);
6934 } while (exception.retry);
6935 return err;
6936}
6937
Andy Adamson557134a2009-04-01 09:21:53 -04006938#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006939/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006940 * Check the exchange flags returned by the server for invalid flags, having
6941 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6942 * DS flags set.
6943 */
6944static int nfs4_check_cl_exchange_flags(u32 flags)
6945{
6946 if (flags & ~EXCHGID4_FLAG_MASK_R)
6947 goto out_inval;
6948 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6949 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6950 goto out_inval;
6951 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6952 goto out_inval;
6953 return NFS_OK;
6954out_inval:
6955 return -NFS4ERR_INVAL;
6956}
6957
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006958static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006959nfs41_same_server_scope(struct nfs41_server_scope *a,
6960 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006961{
6962 if (a->server_scope_sz == b->server_scope_sz &&
6963 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6964 return true;
6965
6966 return false;
6967}
6968
Andy Adamson02a95de2016-02-05 16:08:37 -05006969static void
6970nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6971{
6972}
6973
6974static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6975 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6976};
6977
Andy Adamson357f54d2010-12-14 10:11:57 -05006978/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006979 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006980 *
6981 * The 4.1 client currently uses the same TCP connection for the
6982 * fore and backchannel.
6983 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006984static
6985int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6986 struct rpc_xprt *xprt,
6987 struct nfs_client *clp,
6988 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006989{
6990 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006991 struct nfs41_bind_conn_to_session_args args = {
6992 .client = clp,
6993 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6994 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006995 struct nfs41_bind_conn_to_session_res res;
6996 struct rpc_message msg = {
6997 .rpc_proc =
6998 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006999 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007000 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007001 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007002 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007003 struct rpc_task_setup task_setup_data = {
7004 .rpc_client = clnt,
7005 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007006 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007007 .rpc_message = &msg,
7008 .flags = RPC_TASK_TIMEOUT,
7009 };
7010 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007011
7012 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007013
Trond Myklebust71a097c2015-02-18 09:27:18 -08007014 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7015 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7016 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007017
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007018 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7019 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7020 args.dir = NFS4_CDFC4_FORE;
7021
7022 task = rpc_run_task(&task_setup_data);
7023 if (!IS_ERR(task)) {
7024 status = task->tk_status;
7025 rpc_put_task(task);
7026 } else
7027 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007028 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007029 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007030 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007031 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7032 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7033 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007034 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007035 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007036 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007037 dprintk("NFS: %s: Unexpected direction from server\n",
7038 __func__);
7039 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007040 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007041 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007042 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007043 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7044 __func__);
7045 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007046 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007047 }
7048 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007049out:
7050 dprintk("<-- %s status= %d\n", __func__, status);
7051 return status;
7052}
7053
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007054struct rpc_bind_conn_calldata {
7055 struct nfs_client *clp;
7056 struct rpc_cred *cred;
7057};
7058
7059static int
7060nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7061 struct rpc_xprt *xprt,
7062 void *calldata)
7063{
7064 struct rpc_bind_conn_calldata *p = calldata;
7065
7066 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7067}
7068
7069int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7070{
7071 struct rpc_bind_conn_calldata data = {
7072 .clp = clp,
7073 .cred = cred,
7074 };
7075 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7076 nfs4_proc_bind_conn_to_session_callback, &data);
7077}
7078
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007079/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007080 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7081 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007082 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007083static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7084 .how = SP4_MACH_CRED,
7085 .enforce.u.words = {
7086 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7087 1 << (OP_EXCHANGE_ID - 32) |
7088 1 << (OP_CREATE_SESSION - 32) |
7089 1 << (OP_DESTROY_SESSION - 32) |
7090 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007091 },
7092 .allow.u.words = {
7093 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007094 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007095 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007096 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007097 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007098 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007099 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007100 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007101 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007102 1 << (OP_FREE_STATEID - 32) |
7103 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007104 }
7105};
7106
7107/*
7108 * Select the state protection mode for client `clp' given the server results
7109 * from exchange_id in `sp'.
7110 *
7111 * Returns 0 on success, negative errno otherwise.
7112 */
7113static int nfs4_sp4_select_mode(struct nfs_client *clp,
7114 struct nfs41_state_protection *sp)
7115{
7116 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7117 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7118 1 << (OP_EXCHANGE_ID - 32) |
7119 1 << (OP_CREATE_SESSION - 32) |
7120 1 << (OP_DESTROY_SESSION - 32) |
7121 1 << (OP_DESTROY_CLIENTID - 32)
7122 };
7123 unsigned int i;
7124
7125 if (sp->how == SP4_MACH_CRED) {
7126 /* Print state protect result */
7127 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7128 for (i = 0; i <= LAST_NFS4_OP; i++) {
7129 if (test_bit(i, sp->enforce.u.longs))
7130 dfprintk(MOUNT, " enforce op %d\n", i);
7131 if (test_bit(i, sp->allow.u.longs))
7132 dfprintk(MOUNT, " allow op %d\n", i);
7133 }
7134
7135 /* make sure nothing is on enforce list that isn't supported */
7136 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7137 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7138 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7139 return -EINVAL;
7140 }
7141 }
7142
7143 /*
7144 * Minimal mode - state operations are allowed to use machine
7145 * credential. Note this already happens by default, so the
7146 * client doesn't have to do anything more than the negotiation.
7147 *
7148 * NOTE: we don't care if EXCHANGE_ID is in the list -
7149 * we're already using the machine cred for exchange_id
7150 * and will never use a different cred.
7151 */
7152 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7153 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7154 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7155 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7156 dfprintk(MOUNT, "sp4_mach_cred:\n");
7157 dfprintk(MOUNT, " minimal mode enabled\n");
7158 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7159 } else {
7160 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7161 return -EINVAL;
7162 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007163
7164 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007165 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7166 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007167 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7168 dfprintk(MOUNT, " cleanup mode enabled\n");
7169 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7170 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007171
Andrew Elble99ade3c2015-12-02 09:39:51 -05007172 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7173 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7174 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7175 &clp->cl_sp4_flags);
7176 }
7177
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007178 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7179 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7180 dfprintk(MOUNT, " secinfo mode enabled\n");
7181 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7182 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007183
7184 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7185 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7186 dfprintk(MOUNT, " stateid mode enabled\n");
7187 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7188 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007189
7190 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7191 dfprintk(MOUNT, " write mode enabled\n");
7192 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7193 }
7194
7195 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7196 dfprintk(MOUNT, " commit mode enabled\n");
7197 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7198 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007199 }
7200
7201 return 0;
7202}
7203
Andy Adamson8d89bd72016-09-09 09:22:18 -04007204struct nfs41_exchange_id_data {
7205 struct nfs41_exchange_id_res res;
7206 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007207 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007208 int rpc_status;
7209};
7210
7211static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7212{
7213 struct nfs41_exchange_id_data *cdata =
7214 (struct nfs41_exchange_id_data *)data;
7215 struct nfs_client *clp = cdata->args.client;
7216 int status = task->tk_status;
7217
7218 trace_nfs4_exchange_id(clp, status);
7219
7220 if (status == 0)
7221 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007222
7223 if (cdata->xprt && status == 0) {
7224 status = nfs4_detect_session_trunking(clp, &cdata->res,
7225 cdata->xprt);
7226 goto out;
7227 }
7228
Andy Adamson8d89bd72016-09-09 09:22:18 -04007229 if (status == 0)
7230 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7231
7232 if (status == 0) {
7233 clp->cl_clientid = cdata->res.clientid;
7234 clp->cl_exchange_flags = cdata->res.flags;
7235 /* Client ID is not confirmed */
7236 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7237 clear_bit(NFS4_SESSION_ESTABLISHED,
7238 &clp->cl_session->session_state);
7239 clp->cl_seqid = cdata->res.seqid;
7240 }
7241
7242 kfree(clp->cl_serverowner);
7243 clp->cl_serverowner = cdata->res.server_owner;
7244 cdata->res.server_owner = NULL;
7245
7246 /* use the most recent implementation id */
7247 kfree(clp->cl_implid);
7248 clp->cl_implid = cdata->res.impl_id;
7249 cdata->res.impl_id = NULL;
7250
7251 if (clp->cl_serverscope != NULL &&
7252 !nfs41_same_server_scope(clp->cl_serverscope,
7253 cdata->res.server_scope)) {
7254 dprintk("%s: server_scope mismatch detected\n",
7255 __func__);
7256 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7257 kfree(clp->cl_serverscope);
7258 clp->cl_serverscope = NULL;
7259 }
7260
7261 if (clp->cl_serverscope == NULL) {
7262 clp->cl_serverscope = cdata->res.server_scope;
7263 cdata->res.server_scope = NULL;
7264 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007265 /* Save the EXCHANGE_ID verifier session trunk tests */
7266 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7267 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007268 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007269out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007270 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007271 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007272}
7273
7274static void nfs4_exchange_id_release(void *data)
7275{
7276 struct nfs41_exchange_id_data *cdata =
7277 (struct nfs41_exchange_id_data *)data;
7278
7279 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007280 if (cdata->xprt) {
7281 xprt_put(cdata->xprt);
7282 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7283 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007284 kfree(cdata->res.impl_id);
7285 kfree(cdata->res.server_scope);
7286 kfree(cdata->res.server_owner);
7287 kfree(cdata);
7288}
7289
7290static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7291 .rpc_call_done = nfs4_exchange_id_done,
7292 .rpc_release = nfs4_exchange_id_release,
7293};
7294
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007295/*
7296 * _nfs4_proc_exchange_id()
7297 *
7298 * Wrapper for EXCHANGE_ID operation.
7299 */
7300static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007301 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007302{
7303 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007304 struct rpc_message msg = {
7305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007306 .rpc_cred = cred,
7307 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007308 struct rpc_task_setup task_setup_data = {
7309 .rpc_client = clp->cl_rpcclient,
7310 .callback_ops = &nfs4_exchange_id_call_ops,
7311 .rpc_message = &msg,
7312 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7313 };
7314 struct nfs41_exchange_id_data *calldata;
7315 struct rpc_task *task;
7316 int status = -EIO;
7317
7318 if (!atomic_inc_not_zero(&clp->cl_count))
7319 goto out;
7320
7321 status = -ENOMEM;
7322 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7323 if (!calldata)
7324 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007325
Andy Adamsonad0849a2016-09-09 09:22:28 -04007326 if (!xprt)
7327 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007328
7329 status = nfs4_init_uniform_client_string(clp);
7330 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007331 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007332
7333 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007334 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007335 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007336
Andy Adamson8d89bd72016-09-09 09:22:18 -04007337 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7338 GFP_NOFS);
7339 status = -ENOMEM;
7340 if (unlikely(calldata->res.server_owner == NULL))
7341 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007342
Andy Adamson8d89bd72016-09-09 09:22:18 -04007343 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007344 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007345 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007346 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007347
Andy Adamson8d89bd72016-09-09 09:22:18 -04007348 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7349 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007350 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007351
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007352 switch (sp4_how) {
7353 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007354 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007355 break;
7356
7357 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007358 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007359 break;
7360
7361 default:
7362 /* unsupported! */
7363 WARN_ON_ONCE(1);
7364 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007365 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007366 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007367 if (xprt) {
7368 calldata->xprt = xprt;
7369 task_setup_data.rpc_xprt = xprt;
7370 task_setup_data.flags =
7371 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7372 calldata->args.verifier = &clp->cl_confirm;
7373 } else {
7374 calldata->args.verifier = &verifier;
7375 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007376 calldata->args.client = clp;
7377#ifdef CONFIG_NFS_V4_1_MIGRATION
7378 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7379 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7380 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7381#else
7382 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7383 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7384#endif
7385 msg.rpc_argp = &calldata->args;
7386 msg.rpc_resp = &calldata->res;
7387 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007388
Andy Adamson8d89bd72016-09-09 09:22:18 -04007389 task = rpc_run_task(&task_setup_data);
7390 if (IS_ERR(task)) {
7391 status = PTR_ERR(task);
7392 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007393 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007394
Andy Adamsonad0849a2016-09-09 09:22:28 -04007395 if (!xprt) {
7396 status = rpc_wait_for_completion_task(task);
7397 if (!status)
7398 status = calldata->rpc_status;
7399 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007400 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007401
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007403out:
Chuck Lever177313f2012-05-21 22:44:58 -04007404 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007405 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007406 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007407 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007408 clp->cl_implid->date.seconds,
7409 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007410 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007411 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412
7413out_impl_id:
7414 kfree(calldata->res.impl_id);
7415out_server_scope:
7416 kfree(calldata->res.server_scope);
7417out_server_owner:
7418 kfree(calldata->res.server_owner);
7419out_calldata:
7420 kfree(calldata);
7421 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007422}
7423
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007424/*
7425 * nfs4_proc_exchange_id()
7426 *
7427 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7428 *
7429 * Since the clientid has expired, all compounds using sessions
7430 * associated with the stale clientid will be returning
7431 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7432 * be in some phase of session reset.
7433 *
7434 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7435 */
7436int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7437{
7438 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7439 int status;
7440
7441 /* try SP4_MACH_CRED if krb5i/p */
7442 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7443 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007444 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007445 if (!status)
7446 return 0;
7447 }
7448
7449 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007450 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007451}
7452
Andy Adamson04fa2c62016-09-09 09:22:29 -04007453/**
7454 * nfs4_test_session_trunk
7455 *
7456 * This is an add_xprt_test() test function called from
7457 * rpc_clnt_setup_test_and_add_xprt.
7458 *
7459 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7460 * and is dereferrenced in nfs4_exchange_id_release
7461 *
7462 * Upon success, add the new transport to the rpc_clnt
7463 *
7464 * @clnt: struct rpc_clnt to get new transport
7465 * @xprt: the rpc_xprt to test
7466 * @data: call data for _nfs4_proc_exchange_id.
7467 */
7468int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7469 void *data)
7470{
7471 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7472 u32 sp4_how;
7473
7474 dprintk("--> %s try %s\n", __func__,
7475 xprt->address_strings[RPC_DISPLAY_ADDR]);
7476
7477 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7478
7479 /* Test connection for session trunking. Async exchange_id call */
7480 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7481}
7482EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7483
Trond Myklebust66245532012-05-25 17:18:09 -04007484static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7485 struct rpc_cred *cred)
7486{
7487 struct rpc_message msg = {
7488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7489 .rpc_argp = clp,
7490 .rpc_cred = cred,
7491 };
7492 int status;
7493
7494 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007495 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007496 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007497 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007498 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7499 return status;
7500}
7501
7502static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7503 struct rpc_cred *cred)
7504{
7505 unsigned int loop;
7506 int ret;
7507
7508 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7509 ret = _nfs4_proc_destroy_clientid(clp, cred);
7510 switch (ret) {
7511 case -NFS4ERR_DELAY:
7512 case -NFS4ERR_CLIENTID_BUSY:
7513 ssleep(1);
7514 break;
7515 default:
7516 return ret;
7517 }
7518 }
7519 return 0;
7520}
7521
7522int nfs4_destroy_clientid(struct nfs_client *clp)
7523{
7524 struct rpc_cred *cred;
7525 int ret = 0;
7526
7527 if (clp->cl_mvops->minor_version < 1)
7528 goto out;
7529 if (clp->cl_exchange_flags == 0)
7530 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007531 if (clp->cl_preserve_clid)
7532 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007533 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007534 ret = nfs4_proc_destroy_clientid(clp, cred);
7535 if (cred)
7536 put_rpccred(cred);
7537 switch (ret) {
7538 case 0:
7539 case -NFS4ERR_STALE_CLIENTID:
7540 clp->cl_exchange_flags = 0;
7541 }
7542out:
7543 return ret;
7544}
7545
Andy Adamson2050f0c2009-04-01 09:22:30 -04007546struct nfs4_get_lease_time_data {
7547 struct nfs4_get_lease_time_args *args;
7548 struct nfs4_get_lease_time_res *res;
7549 struct nfs_client *clp;
7550};
7551
7552static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7553 void *calldata)
7554{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007555 struct nfs4_get_lease_time_data *data =
7556 (struct nfs4_get_lease_time_data *)calldata;
7557
7558 dprintk("--> %s\n", __func__);
7559 /* just setup sequence, do not trigger session recovery
7560 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007561 nfs41_setup_sequence(data->clp->cl_session,
7562 &data->args->la_seq_args,
7563 &data->res->lr_seq_res,
7564 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007565 dprintk("<-- %s\n", __func__);
7566}
7567
7568/*
7569 * Called from nfs4_state_manager thread for session setup, so don't recover
7570 * from sequence operation or clientid errors.
7571 */
7572static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7573{
7574 struct nfs4_get_lease_time_data *data =
7575 (struct nfs4_get_lease_time_data *)calldata;
7576
7577 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007578 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7579 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007580 switch (task->tk_status) {
7581 case -NFS4ERR_DELAY:
7582 case -NFS4ERR_GRACE:
7583 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7584 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7585 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007586 /* fall through */
7587 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007588 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007589 return;
7590 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007591 dprintk("<-- %s\n", __func__);
7592}
7593
Trond Myklebust17280172012-03-11 13:11:00 -04007594static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007595 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7596 .rpc_call_done = nfs4_get_lease_time_done,
7597};
7598
7599int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7600{
7601 struct rpc_task *task;
7602 struct nfs4_get_lease_time_args args;
7603 struct nfs4_get_lease_time_res res = {
7604 .lr_fsinfo = fsinfo,
7605 };
7606 struct nfs4_get_lease_time_data data = {
7607 .args = &args,
7608 .res = &res,
7609 .clp = clp,
7610 };
7611 struct rpc_message msg = {
7612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7613 .rpc_argp = &args,
7614 .rpc_resp = &res,
7615 };
7616 struct rpc_task_setup task_setup = {
7617 .rpc_client = clp->cl_rpcclient,
7618 .rpc_message = &msg,
7619 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007620 .callback_data = &data,
7621 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007622 };
7623 int status;
7624
Chuck Levera9c92d62013-08-09 12:48:18 -04007625 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007626 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007627 dprintk("--> %s\n", __func__);
7628 task = rpc_run_task(&task_setup);
7629
7630 if (IS_ERR(task))
7631 status = PTR_ERR(task);
7632 else {
7633 status = task->tk_status;
7634 rpc_put_task(task);
7635 }
7636 dprintk("<-- %s return %d\n", __func__, status);
7637
7638 return status;
7639}
7640
Andy Adamsonfc931582009-04-01 09:22:31 -04007641/*
7642 * Initialize the values to be used by the client in CREATE_SESSION
7643 * If nfs4_init_session set the fore channel request and response sizes,
7644 * use them.
7645 *
7646 * Set the back channel max_resp_sz_cached to zero to force the client to
7647 * always set csa_cachethis to FALSE because the current implementation
7648 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7649 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007650static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7651 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007652{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007653 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007654 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007655
Andy Adamson18aad3d2013-06-26 12:21:49 -04007656 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7657 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7658
Andy Adamsonfc931582009-04-01 09:22:31 -04007659 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007660 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7661 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007662 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007663 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007664
7665 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007666 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007667 __func__,
7668 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007669 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007670
7671 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007672 args->bc_attrs.max_rqst_sz = max_bc_payload;
7673 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007674 args->bc_attrs.max_resp_sz_cached = 0;
7675 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007676 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007677
7678 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7679 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7680 __func__,
7681 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7682 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7683 args->bc_attrs.max_reqs);
7684}
7685
Trond Myklebust79969dd2015-02-18 11:30:18 -08007686static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7687 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007688{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007689 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007690 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007691
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007692 if (rcvd->max_resp_sz > sent->max_resp_sz)
7693 return -EINVAL;
7694 /*
7695 * Our requested max_ops is the minimum we need; we're not
7696 * prepared to break up compounds into smaller pieces than that.
7697 * So, no point even trying to continue if the server won't
7698 * cooperate:
7699 */
7700 if (rcvd->max_ops < sent->max_ops)
7701 return -EINVAL;
7702 if (rcvd->max_reqs == 0)
7703 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007704 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7705 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007706 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007707}
7708
Trond Myklebust79969dd2015-02-18 11:30:18 -08007709static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7710 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007711{
7712 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007713 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007714
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007715 if (!(res->flags & SESSION4_BACK_CHAN))
7716 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007717 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7718 return -EINVAL;
7719 if (rcvd->max_resp_sz < sent->max_resp_sz)
7720 return -EINVAL;
7721 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7722 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007723 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007724 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007725 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007726 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007727out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007728 return 0;
7729}
Andy Adamson8d353012009-04-01 09:22:32 -04007730
Andy Adamson8d353012009-04-01 09:22:32 -04007731static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007732 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007733{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007734 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007735
Trond Myklebust79969dd2015-02-18 11:30:18 -08007736 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007737 if (ret)
7738 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007739 return nfs4_verify_back_channel_attrs(args, res);
7740}
7741
7742static void nfs4_update_session(struct nfs4_session *session,
7743 struct nfs41_create_session_res *res)
7744{
7745 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007746 /* Mark client id and session as being confirmed */
7747 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7748 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007749 session->flags = res->flags;
7750 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007751 if (res->flags & SESSION4_BACK_CHAN)
7752 memcpy(&session->bc_attrs, &res->bc_attrs,
7753 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007754}
7755
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007756static int _nfs4_proc_create_session(struct nfs_client *clp,
7757 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007758{
7759 struct nfs4_session *session = clp->cl_session;
7760 struct nfs41_create_session_args args = {
7761 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007762 .clientid = clp->cl_clientid,
7763 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007764 .cb_program = NFS4_CALLBACK,
7765 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007766 struct nfs41_create_session_res res;
7767
Andy Adamsonfc931582009-04-01 09:22:31 -04007768 struct rpc_message msg = {
7769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7770 .rpc_argp = &args,
7771 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007772 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007773 };
7774 int status;
7775
Chuck Lever6b26cc82016-05-02 14:40:40 -04007776 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007777 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007778
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007779 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007780 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007781
Trond Myklebustb519d402016-09-11 14:50:01 -04007782 switch (status) {
7783 case -NFS4ERR_STALE_CLIENTID:
7784 case -NFS4ERR_DELAY:
7785 case -ETIMEDOUT:
7786 case -EACCES:
7787 case -EAGAIN:
7788 goto out;
7789 };
7790
7791 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007792 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007793 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007794 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007795 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007796 if (status)
7797 goto out;
7798 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007799 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007800out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007801 return status;
7802}
7803
7804/*
7805 * Issues a CREATE_SESSION operation to the server.
7806 * It is the responsibility of the caller to verify the session is
7807 * expired before calling this routine.
7808 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007809int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007810{
7811 int status;
7812 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007813 struct nfs4_session *session = clp->cl_session;
7814
7815 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7816
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007817 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007818 if (status)
7819 goto out;
7820
Andy Adamsonaacd5532011-11-09 13:58:21 -05007821 /* Init or reset the session slot tables */
7822 status = nfs4_setup_session_slot_tables(session);
7823 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007824 if (status)
7825 goto out;
7826
7827 ptr = (unsigned *)&session->sess_id.data[0];
7828 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7829 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007830out:
7831 dprintk("<-- %s\n", __func__);
7832 return status;
7833}
7834
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007835/*
7836 * Issue the over-the-wire RPC DESTROY_SESSION.
7837 * The caller must serialize access to this routine.
7838 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007839int nfs4_proc_destroy_session(struct nfs4_session *session,
7840 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007841{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007842 struct rpc_message msg = {
7843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7844 .rpc_argp = session,
7845 .rpc_cred = cred,
7846 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007847 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007848
7849 dprintk("--> nfs4_proc_destroy_session\n");
7850
7851 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007852 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7853 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007854
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007855 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007856 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007857
7858 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007859 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007860 "Session has been destroyed regardless...\n", status);
7861
7862 dprintk("<-- nfs4_proc_destroy_session\n");
7863 return status;
7864}
7865
Trond Myklebust7b38c362012-05-23 13:23:31 -04007866/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007867 * Renew the cl_session lease.
7868 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007869struct nfs4_sequence_data {
7870 struct nfs_client *clp;
7871 struct nfs4_sequence_args args;
7872 struct nfs4_sequence_res res;
7873};
7874
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007875static void nfs41_sequence_release(void *data)
7876{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007877 struct nfs4_sequence_data *calldata = data;
7878 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007879
Alexandros Batsakis71358402010-02-05 03:45:05 -08007880 if (atomic_read(&clp->cl_count) > 1)
7881 nfs4_schedule_state_renewal(clp);
7882 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007883 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007884}
7885
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007886static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7887{
7888 switch(task->tk_status) {
7889 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007890 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7891 return -EAGAIN;
7892 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007893 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007894 }
7895 return 0;
7896}
7897
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007898static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007899{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007900 struct nfs4_sequence_data *calldata = data;
7901 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007902
Trond Myklebust14516c32010-07-31 14:29:06 -04007903 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7904 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007905
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007906 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007907 if (task->tk_status < 0) {
7908 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007909 if (atomic_read(&clp->cl_count) == 1)
7910 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007911
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007912 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7913 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007914 return;
7915 }
7916 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007917 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007918out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007919 dprintk("<-- %s\n", __func__);
7920}
7921
7922static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7923{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007924 struct nfs4_sequence_data *calldata = data;
7925 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007926 struct nfs4_sequence_args *args;
7927 struct nfs4_sequence_res *res;
7928
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007929 args = task->tk_msg.rpc_argp;
7930 res = task->tk_msg.rpc_resp;
7931
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007932 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007933}
7934
7935static const struct rpc_call_ops nfs41_sequence_ops = {
7936 .rpc_call_done = nfs41_sequence_call_done,
7937 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007938 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007939};
7940
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007941static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7942 struct rpc_cred *cred,
7943 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007944{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007945 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007946 struct rpc_message msg = {
7947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7948 .rpc_cred = cred,
7949 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007950 struct rpc_task_setup task_setup_data = {
7951 .rpc_client = clp->cl_rpcclient,
7952 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007953 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007954 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007955 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007956
Alexandros Batsakis71358402010-02-05 03:45:05 -08007957 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007958 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007959 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007960 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007961 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007962 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007963 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007964 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007965 if (is_privileged)
7966 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007967 msg.rpc_argp = &calldata->args;
7968 msg.rpc_resp = &calldata->res;
7969 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007970 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007971
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007972 return rpc_run_task(&task_setup_data);
7973}
7974
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007975static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007976{
7977 struct rpc_task *task;
7978 int ret = 0;
7979
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007980 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007981 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007982 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007983 if (IS_ERR(task))
7984 ret = PTR_ERR(task);
7985 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007986 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007987 dprintk("<-- %s status=%d\n", __func__, ret);
7988 return ret;
7989}
7990
7991static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7992{
7993 struct rpc_task *task;
7994 int ret;
7995
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007996 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007997 if (IS_ERR(task)) {
7998 ret = PTR_ERR(task);
7999 goto out;
8000 }
8001 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008002 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008003 ret = task->tk_status;
8004 rpc_put_task(task);
8005out:
8006 dprintk("<-- %s status=%d\n", __func__, ret);
8007 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008008}
8009
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008010struct nfs4_reclaim_complete_data {
8011 struct nfs_client *clp;
8012 struct nfs41_reclaim_complete_args arg;
8013 struct nfs41_reclaim_complete_res res;
8014};
8015
8016static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8017{
8018 struct nfs4_reclaim_complete_data *calldata = data;
8019
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008020 nfs41_setup_sequence(calldata->clp->cl_session,
8021 &calldata->arg.seq_args,
8022 &calldata->res.seq_res,
8023 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008024}
8025
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008026static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8027{
8028 switch(task->tk_status) {
8029 case 0:
8030 case -NFS4ERR_COMPLETE_ALREADY:
8031 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8032 break;
8033 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008034 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008035 /* fall through */
8036 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008037 return -EAGAIN;
8038 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008039 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008040 }
8041 return 0;
8042}
8043
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008044static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8045{
8046 struct nfs4_reclaim_complete_data *calldata = data;
8047 struct nfs_client *clp = calldata->clp;
8048 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8049
8050 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008051 if (!nfs41_sequence_done(task, res))
8052 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008053
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008054 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008055 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8056 rpc_restart_call_prepare(task);
8057 return;
8058 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008059 dprintk("<-- %s\n", __func__);
8060}
8061
8062static void nfs4_free_reclaim_complete_data(void *data)
8063{
8064 struct nfs4_reclaim_complete_data *calldata = data;
8065
8066 kfree(calldata);
8067}
8068
8069static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8070 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8071 .rpc_call_done = nfs4_reclaim_complete_done,
8072 .rpc_release = nfs4_free_reclaim_complete_data,
8073};
8074
8075/*
8076 * Issue a global reclaim complete.
8077 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008078static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8079 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008080{
8081 struct nfs4_reclaim_complete_data *calldata;
8082 struct rpc_task *task;
8083 struct rpc_message msg = {
8084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008085 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008086 };
8087 struct rpc_task_setup task_setup_data = {
8088 .rpc_client = clp->cl_rpcclient,
8089 .rpc_message = &msg,
8090 .callback_ops = &nfs4_reclaim_complete_call_ops,
8091 .flags = RPC_TASK_ASYNC,
8092 };
8093 int status = -ENOMEM;
8094
8095 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008096 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008097 if (calldata == NULL)
8098 goto out;
8099 calldata->clp = clp;
8100 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008101
Chuck Levera9c92d62013-08-09 12:48:18 -04008102 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008103 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008104 msg.rpc_argp = &calldata->arg;
8105 msg.rpc_resp = &calldata->res;
8106 task_setup_data.callback_data = calldata;
8107 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008108 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008109 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008110 goto out;
8111 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008112 status = nfs4_wait_for_completion_rpc_task(task);
8113 if (status == 0)
8114 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008115 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008116 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008117out:
8118 dprintk("<-- %s status=%d\n", __func__, status);
8119 return status;
8120}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008121
8122static void
8123nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8124{
8125 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008126 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008127 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008128
8129 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008130 nfs41_setup_sequence(session, &lgp->args.seq_args,
8131 &lgp->res.seq_res, task);
8132 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008133}
8134
8135static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8136{
8137 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008138
8139 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008140 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008141 dprintk("<-- %s\n", __func__);
8142}
8143
8144static int
8145nfs4_layoutget_handle_exception(struct rpc_task *task,
8146 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8147{
Trond Myklebustee314c22012-10-01 17:25:48 -07008148 struct inode *inode = lgp->args.inode;
8149 struct nfs_server *server = NFS_SERVER(inode);
8150 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008151 int nfs4err = task->tk_status;
8152 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008153 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008154
Boaz Harroshed7e5422014-01-22 20:34:54 +02008155 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008156
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008157 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008158 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008159 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008160
8161 /*
8162 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8163 * on the file. set tk_status to -ENODATA to tell upper layer to
8164 * retry go inband.
8165 */
8166 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008167 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008168 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008169 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008170 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8171 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8172 */
8173 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008174 status = -EOVERFLOW;
8175 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008176 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008177 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008178 * (or clients) writing to the same RAID stripe except when
8179 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008180 *
8181 * Treat it like we would RECALLCONFLICT -- we retry for a little
8182 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008183 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008184 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008185 if (lgp->args.minlength == 0) {
8186 status = -EOVERFLOW;
8187 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008188 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008189 status = -EBUSY;
8190 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008191 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008192 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008193 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07008194 case -NFS4ERR_EXPIRED:
8195 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008196 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008197 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008198 lo = NFS_I(inode)->layout;
8199 /* If the open stateid was bad, then recover it. */
8200 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8201 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008202 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008203 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008204 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04008205 break;
8206 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008207
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008208 /*
8209 * Mark the bad layout state as invalid, then retry
8210 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008211 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008212 spin_unlock(&inode->i_lock);
8213 pnfs_free_lseg_list(&head);
8214 status = -EAGAIN;
8215 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008216 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008217
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008218 err = nfs4_handle_exception(server, nfs4err, exception);
8219 if (!status) {
8220 if (exception->retry)
8221 status = -EAGAIN;
8222 else
8223 status = err;
8224 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008225out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008226 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008227 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008228}
8229
Idan Kedar85541162012-08-02 11:47:10 +03008230static size_t max_response_pages(struct nfs_server *server)
8231{
8232 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8233 return nfs_page_array_len(0, max_resp_sz);
8234}
8235
8236static void nfs4_free_pages(struct page **pages, size_t size)
8237{
8238 int i;
8239
8240 if (!pages)
8241 return;
8242
8243 for (i = 0; i < size; i++) {
8244 if (!pages[i])
8245 break;
8246 __free_page(pages[i]);
8247 }
8248 kfree(pages);
8249}
8250
8251static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8252{
8253 struct page **pages;
8254 int i;
8255
8256 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8257 if (!pages) {
8258 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8259 return NULL;
8260 }
8261
8262 for (i = 0; i < size; i++) {
8263 pages[i] = alloc_page(gfp_flags);
8264 if (!pages[i]) {
8265 dprintk("%s: failed to allocate page\n", __func__);
8266 nfs4_free_pages(pages, size);
8267 return NULL;
8268 }
8269 }
8270
8271 return pages;
8272}
8273
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008274static void nfs4_layoutget_release(void *calldata)
8275{
8276 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008277 struct inode *inode = lgp->args.inode;
8278 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008279 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008280
8281 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008282 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008283 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008284 put_nfs_open_context(lgp->args.ctx);
8285 kfree(calldata);
8286 dprintk("<-- %s\n", __func__);
8287}
8288
8289static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8290 .rpc_call_prepare = nfs4_layoutget_prepare,
8291 .rpc_call_done = nfs4_layoutget_done,
8292 .rpc_release = nfs4_layoutget_release,
8293};
8294
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008295struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008296nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008297{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008298 struct inode *inode = lgp->args.inode;
8299 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008300 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008301 struct rpc_task *task;
8302 struct rpc_message msg = {
8303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8304 .rpc_argp = &lgp->args,
8305 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008306 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008307 };
8308 struct rpc_task_setup task_setup_data = {
8309 .rpc_client = server->client,
8310 .rpc_message = &msg,
8311 .callback_ops = &nfs4_layoutget_call_ops,
8312 .callback_data = lgp,
8313 .flags = RPC_TASK_ASYNC,
8314 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008315 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008316 struct nfs4_exception exception = {
8317 .inode = inode,
8318 .timeout = *timeout,
8319 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008320 int status = 0;
8321
8322 dprintk("--> %s\n", __func__);
8323
Peng Tao4bd5a9802014-11-17 11:05:17 +08008324 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8325 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8326
Idan Kedar85541162012-08-02 11:47:10 +03008327 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8328 if (!lgp->args.layout.pages) {
8329 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008330 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008331 }
8332 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8333
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008334 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008335 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008336 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008337
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008338 task = rpc_run_task(&task_setup_data);
8339 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008340 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008341 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008342 if (status == 0) {
8343 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8344 *timeout = exception.timeout;
8345 }
8346
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008347 trace_nfs4_layoutget(lgp->args.ctx,
8348 &lgp->args.range,
8349 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008350 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008351 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008352
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008353 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8354 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008355 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008356 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008357 rpc_put_task(task);
8358 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008359 if (status)
8360 return ERR_PTR(status);
8361 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008362}
8363
Benny Halevycbe82602011-05-22 19:52:37 +03008364static void
8365nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8366{
8367 struct nfs4_layoutreturn *lrp = calldata;
8368
8369 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008370 nfs41_setup_sequence(lrp->clp->cl_session,
8371 &lrp->args.seq_args,
8372 &lrp->res.seq_res,
8373 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008374}
8375
8376static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8377{
8378 struct nfs4_layoutreturn *lrp = calldata;
8379 struct nfs_server *server;
8380
8381 dprintk("--> %s\n", __func__);
8382
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008383 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008384 return;
8385
8386 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008387 switch (task->tk_status) {
8388 default:
8389 task->tk_status = 0;
8390 case 0:
8391 break;
8392 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008393 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008394 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008395 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008396 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008397 return;
8398 }
Benny Halevycbe82602011-05-22 19:52:37 +03008399 dprintk("<-- %s\n", __func__);
8400}
8401
8402static void nfs4_layoutreturn_release(void *calldata)
8403{
8404 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008405 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008406 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008407
8408 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008409 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008410 if (lrp->res.lrs_present) {
8411 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8412 &lrp->args.range,
8413 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008414 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008415 } else
8416 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008417 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008418 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008419 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008420 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008421 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008422 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008423 kfree(calldata);
8424 dprintk("<-- %s\n", __func__);
8425}
8426
8427static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8428 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8429 .rpc_call_done = nfs4_layoutreturn_done,
8430 .rpc_release = nfs4_layoutreturn_release,
8431};
8432
Peng Tao6c166052014-11-17 09:30:40 +08008433int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008434{
8435 struct rpc_task *task;
8436 struct rpc_message msg = {
8437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8438 .rpc_argp = &lrp->args,
8439 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008440 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008441 };
8442 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008443 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008444 .rpc_message = &msg,
8445 .callback_ops = &nfs4_layoutreturn_call_ops,
8446 .callback_data = lrp,
8447 };
Peng Tao6c166052014-11-17 09:30:40 +08008448 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008449
Andrew Elble99ade3c2015-12-02 09:39:51 -05008450 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8451 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8452 &task_setup_data.rpc_client, &msg);
8453
Benny Halevycbe82602011-05-22 19:52:37 +03008454 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008455 if (!sync) {
8456 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8457 if (!lrp->inode) {
8458 nfs4_layoutreturn_release(lrp);
8459 return -EAGAIN;
8460 }
8461 task_setup_data.flags |= RPC_TASK_ASYNC;
8462 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008463 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008464 task = rpc_run_task(&task_setup_data);
8465 if (IS_ERR(task))
8466 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008467 if (sync)
8468 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008469 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008470 dprintk("<-- %s status=%d\n", __func__, status);
8471 rpc_put_task(task);
8472 return status;
8473}
8474
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008475static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008476_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8477 struct pnfs_device *pdev,
8478 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008479{
8480 struct nfs4_getdeviceinfo_args args = {
8481 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008482 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8483 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008484 };
8485 struct nfs4_getdeviceinfo_res res = {
8486 .pdev = pdev,
8487 };
8488 struct rpc_message msg = {
8489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8490 .rpc_argp = &args,
8491 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008492 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008493 };
8494 int status;
8495
8496 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008497 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008498 if (res.notification & ~args.notify_types)
8499 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008500 if (res.notification != args.notify_types)
8501 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008502
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008503 dprintk("<-- %s status=%d\n", __func__, status);
8504
8505 return status;
8506}
8507
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008508int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8509 struct pnfs_device *pdev,
8510 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511{
8512 struct nfs4_exception exception = { };
8513 int err;
8514
8515 do {
8516 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008517 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518 &exception);
8519 } while (exception.retry);
8520 return err;
8521}
8522EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8523
Andy Adamson863a3c62011-03-23 13:27:54 +00008524static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8525{
8526 struct nfs4_layoutcommit_data *data = calldata;
8527 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008528 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008529
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008530 nfs41_setup_sequence(session,
8531 &data->args.seq_args,
8532 &data->res.seq_res,
8533 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008534}
8535
8536static void
8537nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8538{
8539 struct nfs4_layoutcommit_data *data = calldata;
8540 struct nfs_server *server = NFS_SERVER(data->args.inode);
8541
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008542 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008543 return;
8544
8545 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008546 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8547 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8548 case -NFS4ERR_BADLAYOUT: /* no layout */
8549 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008550 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008551 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008552 break;
8553 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008554 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008555 rpc_restart_call_prepare(task);
8556 return;
8557 }
8558 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008559}
8560
8561static void nfs4_layoutcommit_release(void *calldata)
8562{
8563 struct nfs4_layoutcommit_data *data = calldata;
8564
Andy Adamsondb29c082011-07-30 20:52:38 -04008565 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008566 nfs_post_op_update_inode_force_wcc(data->args.inode,
8567 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008568 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008569 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008570 kfree(data);
8571}
8572
8573static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8574 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8575 .rpc_call_done = nfs4_layoutcommit_done,
8576 .rpc_release = nfs4_layoutcommit_release,
8577};
8578
8579int
Andy Adamsonef311532011-03-12 02:58:10 -05008580nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008581{
8582 struct rpc_message msg = {
8583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8584 .rpc_argp = &data->args,
8585 .rpc_resp = &data->res,
8586 .rpc_cred = data->cred,
8587 };
8588 struct rpc_task_setup task_setup_data = {
8589 .task = &data->task,
8590 .rpc_client = NFS_CLIENT(data->args.inode),
8591 .rpc_message = &msg,
8592 .callback_ops = &nfs4_layoutcommit_ops,
8593 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008594 };
8595 struct rpc_task *task;
8596 int status = 0;
8597
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008598 dprintk("NFS: initiating layoutcommit call. sync %d "
8599 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008600 data->args.lastbytewritten,
8601 data->args.inode->i_ino);
8602
Trond Myklebust472e2592015-02-05 16:50:30 -05008603 if (!sync) {
8604 data->inode = nfs_igrab_and_active(data->args.inode);
8605 if (data->inode == NULL) {
8606 nfs4_layoutcommit_release(data);
8607 return -EAGAIN;
8608 }
8609 task_setup_data.flags = RPC_TASK_ASYNC;
8610 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008611 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008612 task = rpc_run_task(&task_setup_data);
8613 if (IS_ERR(task))
8614 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008615 if (sync)
8616 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008617 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008618 dprintk("%s: status %d\n", __func__, status);
8619 rpc_put_task(task);
8620 return status;
8621}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008622
Andy Adamson97431202013-08-08 10:57:56 -04008623/**
8624 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8625 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8626 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008627static int
8628_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008629 struct nfs_fsinfo *info,
8630 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008631{
8632 struct nfs41_secinfo_no_name_args args = {
8633 .style = SECINFO_STYLE_CURRENT_FH,
8634 };
8635 struct nfs4_secinfo_res res = {
8636 .flavors = flavors,
8637 };
8638 struct rpc_message msg = {
8639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8640 .rpc_argp = &args,
8641 .rpc_resp = &res,
8642 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008643 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008644 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008645 int status;
8646
8647 if (use_integrity) {
8648 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008649 cred = nfs4_get_clid_cred(server->nfs_client);
8650 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008651 }
8652
8653 dprintk("--> %s\n", __func__);
8654 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8655 &res.seq_res, 0);
8656 dprintk("<-- %s status=%d\n", __func__, status);
8657
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008658 if (cred)
8659 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008660
8661 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008662}
8663
8664static int
8665nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8666 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8667{
8668 struct nfs4_exception exception = { };
8669 int err;
8670 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008671 /* first try using integrity protection */
8672 err = -NFS4ERR_WRONGSEC;
8673
8674 /* try to use integrity protection with machine cred */
8675 if (_nfs4_is_integrity_protected(server->nfs_client))
8676 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8677 flavors, true);
8678
8679 /*
8680 * if unable to use integrity protection, or SECINFO with
8681 * integrity protection returns NFS4ERR_WRONGSEC (which is
8682 * disallowed by spec, but exists in deployed servers) use
8683 * the current filesystem's rpc_client and the user cred.
8684 */
8685 if (err == -NFS4ERR_WRONGSEC)
8686 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8687 flavors, false);
8688
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008689 switch (err) {
8690 case 0:
8691 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008692 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008693 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008694 default:
8695 err = nfs4_handle_exception(server, err, &exception);
8696 }
8697 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008698out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008699 return err;
8700}
8701
8702static int
8703nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8704 struct nfs_fsinfo *info)
8705{
8706 int err;
8707 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008708 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008709 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008710 struct nfs4_secinfo4 *secinfo;
8711 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008712
8713 page = alloc_page(GFP_KERNEL);
8714 if (!page) {
8715 err = -ENOMEM;
8716 goto out;
8717 }
8718
8719 flavors = page_address(page);
8720 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8721
8722 /*
8723 * Fall back on "guess and check" method if
8724 * the server doesn't support SECINFO_NO_NAME
8725 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008726 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008727 err = nfs4_find_root_sec(server, fhandle, info);
8728 goto out_freepage;
8729 }
8730 if (err)
8731 goto out_freepage;
8732
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008733 for (i = 0; i < flavors->num_flavors; i++) {
8734 secinfo = &flavors->flavors[i];
8735
8736 switch (secinfo->flavor) {
8737 case RPC_AUTH_NULL:
8738 case RPC_AUTH_UNIX:
8739 case RPC_AUTH_GSS:
8740 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8741 &secinfo->flavor_info);
8742 break;
8743 default:
8744 flavor = RPC_AUTH_MAXFLAVOR;
8745 break;
8746 }
8747
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008748 if (!nfs_auth_info_match(&server->auth_info, flavor))
8749 flavor = RPC_AUTH_MAXFLAVOR;
8750
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008751 if (flavor != RPC_AUTH_MAXFLAVOR) {
8752 err = nfs4_lookup_root_sec(server, fhandle,
8753 info, flavor);
8754 if (!err)
8755 break;
8756 }
8757 }
8758
8759 if (flavor == RPC_AUTH_MAXFLAVOR)
8760 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008761
8762out_freepage:
8763 put_page(page);
8764 if (err == -EACCES)
8765 return -EPERM;
8766out:
8767 return err;
8768}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008769
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008770static int _nfs41_test_stateid(struct nfs_server *server,
8771 nfs4_stateid *stateid,
8772 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008773{
8774 int status;
8775 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008776 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008777 };
8778 struct nfs41_test_stateid_res res;
8779 struct rpc_message msg = {
8780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8781 .rpc_argp = &args,
8782 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008783 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008784 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008785 struct rpc_clnt *rpc_client = server->client;
8786
8787 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8788 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008789
Chuck Lever38527b12012-07-11 16:30:23 -04008790 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008791 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008792 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008793 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008794 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008795 if (status != NFS_OK) {
8796 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008797 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008798 }
8799 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008800 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008801}
8802
Trond Myklebust43912bb2016-09-22 13:38:56 -04008803static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8804 int err, struct nfs4_exception *exception)
8805{
8806 exception->retry = 0;
8807 switch(err) {
8808 case -NFS4ERR_DELAY:
8809 nfs4_handle_exception(server, err, exception);
8810 break;
8811 case -NFS4ERR_BADSESSION:
8812 case -NFS4ERR_BADSLOT:
8813 case -NFS4ERR_BAD_HIGH_SLOT:
8814 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8815 case -NFS4ERR_DEADSESSION:
8816 nfs4_do_handle_exception(server, err, exception);
8817 }
8818}
8819
Chuck Lever38527b12012-07-11 16:30:23 -04008820/**
8821 * nfs41_test_stateid - perform a TEST_STATEID operation
8822 *
8823 * @server: server / transport on which to perform the operation
8824 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008825 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008826 *
8827 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8828 * Otherwise a negative NFS4ERR value is returned if the operation
8829 * failed or the state ID is not currently valid.
8830 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008831static int nfs41_test_stateid(struct nfs_server *server,
8832 nfs4_stateid *stateid,
8833 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008834{
8835 struct nfs4_exception exception = { };
8836 int err;
8837 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008838 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008839 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008840 } while (exception.retry);
8841 return err;
8842}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008843
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008844struct nfs_free_stateid_data {
8845 struct nfs_server *server;
8846 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008847 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008848};
8849
8850static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8851{
8852 struct nfs_free_stateid_data *data = calldata;
8853 nfs41_setup_sequence(nfs4_get_session(data->server),
8854 &data->args.seq_args,
8855 &data->res.seq_res,
8856 task);
8857}
8858
8859static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8860{
8861 struct nfs_free_stateid_data *data = calldata;
8862
8863 nfs41_sequence_done(task, &data->res.seq_res);
8864
8865 switch (task->tk_status) {
8866 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008867 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008868 rpc_restart_call_prepare(task);
8869 }
8870}
8871
8872static void nfs41_free_stateid_release(void *calldata)
8873{
8874 kfree(calldata);
8875}
8876
Trond Myklebust17f26b12013-08-21 15:48:42 -04008877static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008878 .rpc_call_prepare = nfs41_free_stateid_prepare,
8879 .rpc_call_done = nfs41_free_stateid_done,
8880 .rpc_release = nfs41_free_stateid_release,
8881};
8882
8883static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8884 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008885 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008886 bool privileged)
8887{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008888 struct rpc_message msg = {
8889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008890 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008891 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008892 struct rpc_task_setup task_setup = {
8893 .rpc_client = server->client,
8894 .rpc_message = &msg,
8895 .callback_ops = &nfs41_free_stateid_ops,
8896 .flags = RPC_TASK_ASYNC,
8897 };
8898 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008899
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008900 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8901 &task_setup.rpc_client, &msg);
8902
Chuck Lever38527b12012-07-11 16:30:23 -04008903 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008904 data = kmalloc(sizeof(*data), GFP_NOFS);
8905 if (!data)
8906 return ERR_PTR(-ENOMEM);
8907 data->server = server;
8908 nfs4_stateid_copy(&data->args.stateid, stateid);
8909
8910 task_setup.callback_data = data;
8911
8912 msg.rpc_argp = &data->args;
8913 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008914 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008915 if (privileged)
8916 nfs4_set_sequence_privileged(&data->args.seq_args);
8917
8918 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008919}
8920
Chuck Lever38527b12012-07-11 16:30:23 -04008921/**
8922 * nfs41_free_stateid - perform a FREE_STATEID operation
8923 *
8924 * @server: server / transport on which to perform the operation
8925 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008926 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008927 *
8928 * Returns NFS_OK if the server freed "stateid". Otherwise a
8929 * negative NFS4ERR value is returned.
8930 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008931static int nfs41_free_stateid(struct nfs_server *server,
8932 nfs4_stateid *stateid,
8933 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008934{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008935 struct rpc_task *task;
8936 int ret;
8937
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008938 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008939 if (IS_ERR(task))
8940 return PTR_ERR(task);
8941 ret = rpc_wait_for_completion_task(task);
8942 if (!ret)
8943 ret = task->tk_status;
8944 rpc_put_task(task);
8945 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008946}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008947
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008948static void
8949nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008950{
8951 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008952 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008953
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008954 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008955 nfs4_free_lock_state(server, lsp);
8956 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008957 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008958 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008959}
8960
Trond Myklebust36281ca2012-03-04 18:13:56 -05008961static bool nfs41_match_stateid(const nfs4_stateid *s1,
8962 const nfs4_stateid *s2)
8963{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008964 if (s1->type != s2->type)
8965 return false;
8966
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008967 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008968 return false;
8969
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008970 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008971 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008972 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008973 return true;
8974
8975 return false;
8976}
8977
Andy Adamson557134a2009-04-01 09:21:53 -04008978#endif /* CONFIG_NFS_V4_1 */
8979
Trond Myklebust36281ca2012-03-04 18:13:56 -05008980static bool nfs4_match_stateid(const nfs4_stateid *s1,
8981 const nfs4_stateid *s2)
8982{
Trond Myklebustf597c532012-03-04 18:13:56 -05008983 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008984}
8985
8986
Trond Myklebust17280172012-03-11 13:11:00 -04008987static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008988 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008989 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 .recover_open = nfs4_open_reclaim,
8991 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008992 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008993 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994};
8995
Andy Adamson591d71c2009-04-01 09:22:47 -04008996#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008997static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008998 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8999 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9000 .recover_open = nfs4_open_reclaim,
9001 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009002 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009003 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009004 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009005};
9006#endif /* CONFIG_NFS_V4_1 */
9007
Trond Myklebust17280172012-03-11 13:11:00 -04009008static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009009 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009010 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009011 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009013 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014};
9015
Andy Adamson591d71c2009-04-01 09:22:47 -04009016#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009017static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009018 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9019 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009020 .recover_open = nfs41_open_expired,
9021 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009022 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009023};
9024#endif /* CONFIG_NFS_V4_1 */
9025
Trond Myklebust17280172012-03-11 13:11:00 -04009026static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009027 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009028 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009029 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009030};
9031
9032#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009033static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009034 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009035 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009036 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009037};
9038#endif
9039
Chuck Leverec011fe2013-10-17 14:12:39 -04009040static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009041 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009042 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009043};
9044
9045#if defined(CONFIG_NFS_V4_1)
9046static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009047 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009048 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009049};
9050#endif /* CONFIG_NFS_V4_1 */
9051
Trond Myklebust97dc1352010-06-16 09:52:26 -04009052static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9053 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009054 .init_caps = NFS_CAP_READDIRPLUS
9055 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009056 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009057 .init_client = nfs40_init_client,
9058 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009059 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009060 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009061 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009062 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009063 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009064 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9065 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9066 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009067 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009068};
9069
9070#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009071static struct nfs_seqid *
9072nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9073{
9074 return NULL;
9075}
9076
Trond Myklebust97dc1352010-06-16 09:52:26 -04009077static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9078 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009079 .init_caps = NFS_CAP_READDIRPLUS
9080 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009081 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009082 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009083 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009084 .init_client = nfs41_init_client,
9085 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009086 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009087 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009088 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009089 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009090 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009091 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009092 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9093 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9094 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009095 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009096};
9097#endif
9098
Steve Dickson42c2c422013-05-22 12:50:38 -04009099#if defined(CONFIG_NFS_V4_2)
9100static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9101 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009102 .init_caps = NFS_CAP_READDIRPLUS
9103 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009104 | NFS_CAP_POSIX_LOCK
9105 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009106 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009107 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009108 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009109 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009110 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009111 | NFS_CAP_LAYOUTSTATS
9112 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009113 .init_client = nfs41_init_client,
9114 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009115 .match_stateid = nfs41_match_stateid,
9116 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009117 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009118 .call_sync_ops = &nfs41_call_sync_ops,
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,
Steve Dickson42c2c422013-05-22 12:50:38 -04009121 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9122 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9123 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009124 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009125};
9126#endif
9127
Trond Myklebust97dc1352010-06-16 09:52:26 -04009128const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9129 [0] = &nfs_v4_0_minor_ops,
9130#if defined(CONFIG_NFS_V4_1)
9131 [1] = &nfs_v4_1_minor_ops,
9132#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009133#if defined(CONFIG_NFS_V4_2)
9134 [2] = &nfs_v4_2_minor_ops,
9135#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009136};
9137
Trond Myklebust13997822016-07-24 17:10:52 -04009138static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009139{
9140 ssize_t error, error2;
9141
9142 error = generic_listxattr(dentry, list, size);
9143 if (error < 0)
9144 return error;
9145 if (list) {
9146 list += error;
9147 size -= error;
9148 }
9149
9150 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9151 if (error2 < 0)
9152 return error2;
9153 return error + error2;
9154}
9155
Trond Myklebust17f26b12013-08-21 15:48:42 -04009156static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009157 .create = nfs_create,
9158 .lookup = nfs_lookup,
9159 .atomic_open = nfs_atomic_open,
9160 .link = nfs_link,
9161 .unlink = nfs_unlink,
9162 .symlink = nfs_symlink,
9163 .mkdir = nfs_mkdir,
9164 .rmdir = nfs_rmdir,
9165 .mknod = nfs_mknod,
9166 .rename = nfs_rename,
9167 .permission = nfs_permission,
9168 .getattr = nfs_getattr,
9169 .setattr = nfs_setattr,
9170 .getxattr = generic_getxattr,
9171 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009172 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009173 .removexattr = generic_removexattr,
9174};
9175
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009176static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009177 .permission = nfs_permission,
9178 .getattr = nfs_getattr,
9179 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009180 .getxattr = generic_getxattr,
9181 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009182 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009183 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009184};
9185
David Howells509de812006-08-22 20:06:11 -04009186const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187 .version = 4, /* protocol version */
9188 .dentry_ops = &nfs4_dentry_operations,
9189 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009190 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009191 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009193 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009194 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195 .getattr = nfs4_proc_getattr,
9196 .setattr = nfs4_proc_setattr,
9197 .lookup = nfs4_proc_lookup,
9198 .access = nfs4_proc_access,
9199 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 .create = nfs4_proc_create,
9201 .remove = nfs4_proc_remove,
9202 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009203 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009205 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009206 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009207 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009208 .link = nfs4_proc_link,
9209 .symlink = nfs4_proc_symlink,
9210 .mkdir = nfs4_proc_mkdir,
9211 .rmdir = nfs4_proc_remove,
9212 .readdir = nfs4_proc_readdir,
9213 .mknod = nfs4_proc_mknod,
9214 .statfs = nfs4_proc_statfs,
9215 .fsinfo = nfs4_proc_fsinfo,
9216 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009217 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009219 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009221 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009223 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009225 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009226 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009228 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009229 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009230 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009231 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009232 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009233 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009234 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009235 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009236 .create_server = nfs4_create_server,
9237 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238};
9239
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009240static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009241 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009242 .list = nfs4_xattr_list_nfs4_acl,
9243 .get = nfs4_xattr_get_nfs4_acl,
9244 .set = nfs4_xattr_set_nfs4_acl,
9245};
9246
9247const struct xattr_handler *nfs4_xattr_handlers[] = {
9248 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009249#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9250 &nfs4_xattr_nfs4_label_handler,
9251#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009252 NULL
9253};
9254
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255/*
9256 * Local variables:
9257 * c-basic-offset: 8
9258 * End:
9259 */