blob: 251e48e7ba16466b7453dea554ac4f94ace5965a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400366 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500368 int ret = errorcode;
369
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400370 exception->delay = 0;
371 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 exception->retry = 0;
373 switch(errorcode) {
374 case 0:
375 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500376 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400380 if (inode) {
381 int err;
382
383 err = nfs_async_inode_return_delegation(inode,
384 stateid);
385 if (err == 0)
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
389 break;
390 }
391 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 if (state == NULL)
393 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400394 ret = nfs4_schedule_stateid_recovery(server, state);
395 if (ret < 0)
396 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400398 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400399 if (state != NULL) {
400 ret = nfs4_schedule_stateid_recovery(server, state);
401 if (ret < 0)
402 break;
403 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500405 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400408 case -NFS4ERR_MOVED:
409 ret = nfs4_schedule_migration_recovery(server);
410 if (ret < 0)
411 break;
412 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500416#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
425 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400427 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500428#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
432 * fail
433 */
434 ret = -EBUSY;
435 break;
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400438 nfs_inc_server_stats(server, NFSIOS_DELAY);
439 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400440 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400441 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400442 exception->delay = 1;
443 return 0;
444
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800448 break;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
457 "uid/gids. "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
460 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 }
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500464wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->recovering = 1;
466 return 0;
467}
468
469/* This is the error handling routine for processes that are allowed
470 * to sleep.
471 */
472int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
473{
474 struct nfs_client *clp = server->nfs_client;
475 int ret;
476
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
480 goto out_retry;
481 }
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
485 return -EIO;
486 goto out_retry;
487 }
488 return ret;
489out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500490 if (ret == 0)
491 exception->retry = 1;
492 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500493}
494
Trond Myklebust037fc982015-09-20 15:51:00 -0400495static int
496nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
498{
499 struct nfs_client *clp = server->nfs_client;
500 int ret;
501
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
505 goto out_retry;
506 }
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
511 goto out_retry;
512 }
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 ret = -EIO;
515 return ret;
516out_retry:
517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
520}
521
522static int
523nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
525{
526 struct nfs4_exception exception = {
527 .state = state,
528 };
529
530 if (task->tk_status >= 0)
531 return 0;
532 if (timeout)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
535 task->tk_status,
536 &exception);
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
539 if (exception.retry)
540 return -EAGAIN;
541 return 0;
542}
543
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400544/*
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
547 */
548static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
549{
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
551
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
554 return true;
555
556 return false;
557}
Trond Myklebust65de8722008-12-23 15:21:44 -0500558
Trond Myklebust452e9352010-07-31 14:29:06 -0400559static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
565}
566
Trond Myklebust452e9352010-07-31 14:29:06 -0400567static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
568{
Trond Myklebustbe824162015-07-05 14:50:46 -0400569 struct nfs_client *clp = server->nfs_client;
570
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400573}
574
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400575struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
579};
580
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800581void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400583{
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
587
588 res->sr_slot = NULL;
589}
590
591static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
592{
593 args->sa_privileged = 1;
594}
595
Peng Taocb04ad22014-06-11 05:24:15 +0800596int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400600{
Chuck Lever3bd23842013-08-09 12:49:19 -0400601 struct nfs4_slot *slot;
602
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
605 goto out_start;
606
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
609 goto out_sleep;
610
611 slot = nfs4_alloc_slot(tbl);
612 if (IS_ERR(slot)) {
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
615 goto out_sleep;
616 }
617 spin_unlock(&tbl->slot_tbl_lock);
618
619 args->sa_slot = slot;
620 res->sr_slot = slot;
621
622out_start:
623 rpc_call_start(task);
624 return 0;
625
626out_sleep:
627 if (args->sa_privileged)
628 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
629 NULL, RPC_PRIORITY_PRIVILEGED);
630 else
631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
633 return -EAGAIN;
634}
Peng Taocb04ad22014-06-11 05:24:15 +0800635EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400636
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400637static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400638{
639 struct nfs4_slot *slot = res->sr_slot;
640 struct nfs4_slot_table *tbl;
641
Chuck Lever3bd23842013-08-09 12:49:19 -0400642 tbl = slot->table;
643 spin_lock(&tbl->slot_tbl_lock);
644 if (!nfs41_wake_and_assign_slot(tbl, slot))
645 nfs4_free_slot(tbl, slot);
646 spin_unlock(&tbl->slot_tbl_lock);
647
648 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400649}
650
651static int nfs40_sequence_done(struct rpc_task *task,
652 struct nfs4_sequence_res *res)
653{
654 if (res->sr_slot != NULL)
655 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400656 return 1;
657}
658
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400659#if defined(CONFIG_NFS_V4_1)
660
Trond Myklebustd185a332010-06-16 09:52:25 -0400661static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400662{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500663 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400664 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500665 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500666 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400667
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500668 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500669 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500670
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400671 /* Bump the slot sequence number */
672 if (slot->seq_done)
673 slot->seq_nr++;
674 slot->seq_done = 0;
675
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500676 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 /* Be nice to the server: try to ensure that the last transmitted
678 * value for highest_user_slotid <= target_highest_slotid
679 */
680 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
681 send_new_highest_used_slotid = true;
682
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500683 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500684 send_new_highest_used_slotid = false;
685 goto out_unlock;
686 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500688
689 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
690 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500691out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500692 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400693 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500694 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400695 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400696 if (waitqueue_active(&tbl->slot_waitq))
697 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400698}
699
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400700static int nfs41_sequence_process(struct rpc_task *task,
701 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400702{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500703 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500704 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400705 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500706 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500707 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400708
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500709 if (slot == NULL)
710 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400711 /* don't increment the sequence number if the task wasn't sent */
712 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400713 goto out;
714
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500715 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500716
Trond Myklebustac20d162012-12-15 15:36:07 -0500717 if (slot->interrupted) {
718 slot->interrupted = 0;
719 interrupted = true;
720 }
721
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400722 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500723 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 switch (res->sr_status) {
725 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400726 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400727 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500728 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500729 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500730 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400731 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500732 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400733 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500734 case 1:
735 /*
736 * sr_status remains 1 if an RPC level error occurred.
737 * The server may or may not have processed the sequence
738 * operation..
739 * Mark the slot as having hosted an interrupted RPC call.
740 */
741 slot->interrupted = 1;
742 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 case -NFS4ERR_DELAY:
744 /* The server detected a resend of the RPC call and
745 * returned NFS4ERR_DELAY as per Section 2.10.6.2
746 * of RFC5661.
747 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500748 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400749 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500750 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500751 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400752 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500753 case -NFS4ERR_BADSLOT:
754 /*
755 * The slot id we used was probably retired. Try again
756 * using a different slot id.
757 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500758 goto retry_nowait;
759 case -NFS4ERR_SEQ_MISORDERED:
760 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500761 * Was the last operation on this sequence interrupted?
762 * If so, retry after bumping the sequence number.
763 */
764 if (interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
768 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500769 * Could this slot have been previously retired?
770 * If so, then the server may be expecting seq_nr = 1!
771 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500772 if (slot->seq_nr != 1) {
773 slot->seq_nr = 1;
774 goto retry_nowait;
775 }
776 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500777 case -NFS4ERR_SEQ_FALSE_RETRY:
778 ++slot->seq_nr;
779 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400780 default:
781 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400782 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400783 }
784out:
785 /* The session may be reset by one of the error handlers. */
786 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500787out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500788 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500789retry_nowait:
790 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400791 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500792 task->tk_status = 0;
793 ret = 0;
794 }
795 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400796out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400797 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 goto out;
799 rpc_delay(task, NFS4_POLL_RETRY_MAX);
800 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400802
803int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
804{
805 if (!nfs41_sequence_process(task, res))
806 return 0;
807 if (res->sr_slot != NULL)
808 nfs41_sequence_free_slot(res);
809 return 1;
810
811}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500812EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400813
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400814static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
815{
816 if (res->sr_slot == NULL)
817 return 1;
818 if (res->sr_slot->table->session != NULL)
819 return nfs41_sequence_process(task, res);
820 return nfs40_sequence_done(task, res);
821}
822
823static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
824{
825 if (res->sr_slot != NULL) {
826 if (res->sr_slot->table->session != NULL)
827 nfs41_sequence_free_slot(res);
828 else
829 nfs40_sequence_free_slot(res);
830 }
831}
832
Peng Tao2c4b1312014-06-11 05:24:16 +0800833int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400834{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500835 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400837 if (!res->sr_slot->table->session)
838 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400840}
Peng Tao2c4b1312014-06-11 05:24:16 +0800841EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000843int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844 struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846 struct rpc_task *task)
847{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400848 struct nfs4_slot *slot;
849 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400850
851 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400853 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400854 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400856 tbl = &session->fc_slot_table;
857
Trond Myklebust69d206b2012-11-22 13:21:02 -0500858 task->tk_timeout = 0;
859
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400860 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400861 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400862 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500863 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500864 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400865 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400866 }
867
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500868 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500869 if (IS_ERR(slot)) {
870 /* If out of memory, try again in 1/4 second */
871 if (slot == ERR_PTR(-ENOMEM))
872 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400873 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400874 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400875 }
876 spin_unlock(&tbl->slot_tbl_lock);
877
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500878 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400879
Chuck Levere8d92382013-08-09 12:47:51 -0400880 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500881 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400882
Benny Halevydfb4f3092010-09-24 09:17:01 -0400883 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500884 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400885 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400886 /*
887 * sr_status is only set in decode_sequence, and so will remain
888 * set to 1 if an rpc level failure occurs.
889 */
890 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400891 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400892out_success:
893 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400895out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400896 /* Privileged tasks are queued with top priority */
897 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400898 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
899 NULL, RPC_PRIORITY_PRIVILEGED);
900 else
901 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400902 spin_unlock(&tbl->slot_tbl_lock);
903 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400904}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000905EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400906
Chuck Lever5a580e02013-08-09 12:48:09 -0400907static int nfs4_setup_sequence(const struct nfs_server *server,
908 struct nfs4_sequence_args *args,
909 struct nfs4_sequence_res *res,
910 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400911{
Trond Myklebust035168a2010-06-16 09:52:26 -0400912 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400913 int ret = 0;
914
Chuck Lever3bd23842013-08-09 12:49:19 -0400915 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800916 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
917 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400918
Chuck Levere8d92382013-08-09 12:47:51 -0400919 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400920 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400921 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400922
Trond Myklebust9d12b212012-01-17 22:04:25 -0500923 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400924
Andy Adamsonce5039c2009-04-01 09:22:13 -0400925 dprintk("<-- %s status=%d\n", __func__, ret);
926 return ret;
927}
928
Andy Adamsonce5039c2009-04-01 09:22:13 -0400929static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
930{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400931 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400932 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400933
Trond Myklebust035168a2010-06-16 09:52:26 -0400934 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
935
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937}
938
Andy Adamson69ab40c2009-04-01 09:22:19 -0400939static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
940{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400941 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400942
Trond Myklebust14516c32010-07-31 14:29:06 -0400943 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400944}
945
Trond Myklebust17280172012-03-11 13:11:00 -0400946static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400948 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400949};
950
Chuck Lever3bd23842013-08-09 12:49:19 -0400951#else /* !CONFIG_NFS_V4_1 */
952
Chuck Lever5a580e02013-08-09 12:48:09 -0400953static int nfs4_setup_sequence(const struct nfs_server *server,
954 struct nfs4_sequence_args *args,
955 struct nfs4_sequence_res *res,
956 struct rpc_task *task)
957{
Peng Taocb04ad22014-06-11 05:24:15 +0800958 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
959 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400960}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400961
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400962static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
963{
964 return nfs40_sequence_done(task, res);
965}
966
967static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
968{
969 if (res->sr_slot != NULL)
970 nfs40_sequence_free_slot(res);
971}
972
Peng Tao2c4b1312014-06-11 05:24:16 +0800973int nfs4_sequence_done(struct rpc_task *task,
974 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400975{
Chuck Lever3bd23842013-08-09 12:49:19 -0400976 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400977}
Peng Tao2c4b1312014-06-11 05:24:16 +0800978EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400979
980#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400981
Chuck Lever9915ea72013-08-09 12:48:27 -0400982static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
983{
984 struct nfs4_call_sync_data *data = calldata;
985 nfs4_setup_sequence(data->seq_server,
986 data->seq_args, data->seq_res, task);
987}
988
989static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
990{
991 struct nfs4_call_sync_data *data = calldata;
992 nfs4_sequence_done(task, data->seq_res);
993}
994
995static const struct rpc_call_ops nfs40_call_sync_ops = {
996 .rpc_call_prepare = nfs40_call_sync_prepare,
997 .rpc_call_done = nfs40_call_sync_done,
998};
999
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001001 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 struct nfs4_sequence_args *args,
1004 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001005{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 int ret;
1007 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001008 struct nfs_client *clp = server->nfs_client;
1009 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 .seq_server = server,
1011 .seq_args = args,
1012 .seq_res = res,
1013 };
1014 struct rpc_task_setup task_setup = {
1015 .rpc_client = clnt,
1016 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001017 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 .callback_data = &data
1019 };
1020
1021 task = rpc_run_task(&task_setup);
1022 if (IS_ERR(task))
1023 ret = PTR_ERR(task);
1024 else {
1025 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001026 rpc_put_task(task);
1027 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001028 return ret;
1029}
1030
Bryan Schumaker7c513052011-03-24 17:12:24 +00001031int nfs4_call_sync(struct rpc_clnt *clnt,
1032 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001033 struct rpc_message *msg,
1034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1036 int cache_reply)
1037{
Chuck Levera9c92d62013-08-09 12:48:18 -04001038 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001039 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1043{
1044 struct nfs_inode *nfsi = NFS_I(dir);
1045
1046 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001047 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001048 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001050 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001051 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001052 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 spin_unlock(&dir->i_lock);
1054}
1055
1056struct nfs4_opendata {
1057 struct kref kref;
1058 struct nfs_openargs o_arg;
1059 struct nfs_openres o_res;
1060 struct nfs_open_confirmargs c_arg;
1061 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001062 struct nfs4_string owner_name;
1063 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001064 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001066 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001068 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct nfs4_state_owner *owner;
1070 struct nfs4_state *state;
1071 struct iattr attrs;
1072 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001073 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001074 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001075 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001076 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int cancelled;
1078};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001079
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001080static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1081 int err, struct nfs4_exception *exception)
1082{
1083 if (err != -EINVAL)
1084 return false;
1085 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1086 return false;
1087 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1088 exception->retry = 1;
1089 return true;
1090}
1091
Trond Myklebust6ae37332015-01-30 14:21:14 -05001092static u32
1093nfs4_map_atomic_open_share(struct nfs_server *server,
1094 fmode_t fmode, int openflags)
1095{
1096 u32 res = 0;
1097
1098 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1099 case FMODE_READ:
1100 res = NFS4_SHARE_ACCESS_READ;
1101 break;
1102 case FMODE_WRITE:
1103 res = NFS4_SHARE_ACCESS_WRITE;
1104 break;
1105 case FMODE_READ|FMODE_WRITE:
1106 res = NFS4_SHARE_ACCESS_BOTH;
1107 }
1108 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1109 goto out;
1110 /* Want no delegation if we're using O_DIRECT */
1111 if (openflags & O_DIRECT)
1112 res |= NFS4_SHARE_WANT_NO_DELEG;
1113out:
1114 return res;
1115}
1116
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001117static enum open_claim_type4
1118nfs4_map_atomic_open_claim(struct nfs_server *server,
1119 enum open_claim_type4 claim)
1120{
1121 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1122 return claim;
1123 switch (claim) {
1124 default:
1125 return claim;
1126 case NFS4_OPEN_CLAIM_FH:
1127 return NFS4_OPEN_CLAIM_NULL;
1128 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1129 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1130 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1131 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1132 }
1133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001136{
1137 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001138 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139 p->o_res.seqid = p->o_arg.seqid;
1140 p->c_res.seqid = p->c_arg.seqid;
1141 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001142 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001144 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145}
1146
Al Viro82a2c1b2011-06-22 18:30:55 -04001147static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001148 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001149 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001150 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001151 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001152 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153{
Al Viro82a2c1b2011-06-22 18:30:55 -04001154 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001155 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001157 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001158 struct nfs4_opendata *p;
1159
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001160 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 if (p == NULL)
1162 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001163
1164 p->f_label = nfs4_label_alloc(server, gfp_mask);
1165 if (IS_ERR(p->f_label))
1166 goto err_free_p;
1167
Kinglong Meea49c2692015-07-27 15:31:38 +08001168 p->a_label = nfs4_label_alloc(server, gfp_mask);
1169 if (IS_ERR(p->a_label))
1170 goto err_free_f;
1171
Trond Myklebust63f5f792015-01-23 19:19:25 -05001172 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1173 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001174 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001175 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001176 nfs_sb_active(dentry->d_sb);
1177 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 p->dir = parent;
1179 p->owner = sp;
1180 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001181 p->o_arg.open_flags = flags;
1182 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001183 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1184 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001185 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1186 * will return permission denied for all bits until close */
1187 if (!(flags & O_EXCL)) {
1188 /* ask server to check for all possible rights as results
1189 * are cached */
1190 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1191 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001192 }
David Howells7539bba2006-08-22 20:06:09 -04001193 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001194 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1195 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001196 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001198 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001199 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001200 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001201 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1202 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001203 case NFS4_OPEN_CLAIM_NULL:
1204 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1205 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1206 p->o_arg.fh = NFS_FH(dir);
1207 break;
1208 case NFS4_OPEN_CLAIM_PREVIOUS:
1209 case NFS4_OPEN_CLAIM_FH:
1210 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1211 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001212 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001213 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001214 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001215 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001216
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 p->o_arg.u.attrs = &p->attrs;
1218 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001219
1220 verf[0] = jiffies;
1221 verf[1] = current->pid;
1222 memcpy(p->o_arg.u.verifier.data, verf,
1223 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001225 p->c_arg.fh = &p->o_res.fh;
1226 p->c_arg.stateid = &p->o_res.stateid;
1227 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001230 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001231
1232err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001233 nfs4_label_free(p->a_label);
1234err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001235 nfs4_label_free(p->f_label);
1236err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 kfree(p);
1238err:
1239 dput(parent);
1240 return NULL;
1241}
1242
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001243static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001244{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001245 struct nfs4_opendata *p = container_of(kref,
1246 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001247 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248
1249 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001250 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001251 if (p->state != NULL)
1252 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001253 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001254
Kinglong Meea49c2692015-07-27 15:31:38 +08001255 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001256 nfs4_label_free(p->f_label);
1257
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001258 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001259 dput(p->dentry);
1260 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001261 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001262 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 kfree(p);
1264}
1265
1266static void nfs4_opendata_put(struct nfs4_opendata *p)
1267{
1268 if (p != NULL)
1269 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270}
1271
Trond Myklebust06f814a2006-01-03 09:55:07 +01001272static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1273{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001274 int ret;
1275
Trond Myklebust06f814a2006-01-03 09:55:07 +01001276 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001277 return ret;
1278}
1279
Trond Myklebust24311f82015-09-20 10:50:17 -04001280static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1281 fmode_t fmode)
1282{
1283 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1284 case FMODE_READ|FMODE_WRITE:
1285 return state->n_rdwr != 0;
1286 case FMODE_WRITE:
1287 return state->n_wronly != 0;
1288 case FMODE_READ:
1289 return state->n_rdonly != 0;
1290 }
1291 WARN_ON_ONCE(1);
1292 return false;
1293}
1294
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001295static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001296{
1297 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298
Trond Myklebust536e43d2012-01-17 22:04:26 -05001299 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001300 goto out;
1301 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001302 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001303 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1304 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 break;
1306 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001307 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1308 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001309 break;
1310 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001311 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1312 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001315 return ret;
1316}
1317
Trond Myklebust2a606182015-08-19 22:30:00 -05001318static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1319 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001320{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001321 if (delegation == NULL)
1322 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001324 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001325 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1326 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001327 switch (claim) {
1328 case NFS4_OPEN_CLAIM_NULL:
1329 case NFS4_OPEN_CLAIM_FH:
1330 break;
1331 case NFS4_OPEN_CLAIM_PREVIOUS:
1332 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1333 break;
1334 default:
1335 return 0;
1336 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001337 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 return 1;
1339}
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001342{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001343 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001344 case FMODE_WRITE:
1345 state->n_wronly++;
1346 break;
1347 case FMODE_READ:
1348 state->n_rdonly++;
1349 break;
1350 case FMODE_READ|FMODE_WRITE:
1351 state->n_rdwr++;
1352 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001354}
1355
Trond Myklebust4f14c192014-02-12 19:15:06 -05001356static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001357{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001358 struct nfs_client *clp = state->owner->so_server->nfs_client;
1359 bool need_recover = false;
1360
1361 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1362 need_recover = true;
1363 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1364 need_recover = true;
1365 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1366 need_recover = true;
1367 if (need_recover)
1368 nfs4_state_mark_reclaim_nograce(clp, state);
1369}
1370
Trond Myklebuste999e802014-02-10 18:20:47 -05001371static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1372 nfs4_stateid *stateid)
1373{
1374 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1375 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001376 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1377 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001378 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001379 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001380 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1381 return true;
1382 return false;
1383}
1384
Trond Myklebustf95549c2015-01-23 18:06:09 -05001385static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1386{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001387 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1388 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001389 if (state->n_wronly)
1390 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1391 if (state->n_rdonly)
1392 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 if (state->n_rdwr)
1394 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001395 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001396}
1397
Trond Myklebust226056c2014-02-11 10:41:07 -05001398static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001399 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001400 nfs4_stateid *stateid, fmode_t fmode)
1401{
1402 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1403 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1404 case FMODE_WRITE:
1405 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1406 break;
1407 case FMODE_READ:
1408 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1409 break;
1410 case 0:
1411 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1412 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1413 clear_bit(NFS_OPEN_STATE, &state->flags);
1414 }
1415 if (stateid == NULL)
1416 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001417 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001418 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1419 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1420 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001421 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001422 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001423 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001424 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001425 nfs4_stateid_copy(&state->stateid, stateid);
1426 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001427}
1428
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001429static void nfs_clear_open_stateid(struct nfs4_state *state,
1430 nfs4_stateid *arg_stateid,
1431 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001432{
1433 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001434 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001435 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001436 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1437 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001438}
1439
Trond Myklebust003707c2007-07-05 18:07:55 -04001440static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1441{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001442 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001443 case FMODE_READ:
1444 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1445 break;
1446 case FMODE_WRITE:
1447 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1448 break;
1449 case FMODE_READ|FMODE_WRITE:
1450 set_bit(NFS_O_RDWR_STATE, &state->flags);
1451 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001452 if (!nfs_need_update_open_stateid(state, stateid))
1453 return;
1454 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1455 nfs4_stateid_copy(&state->stateid, stateid);
1456 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457}
1458
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459static 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 -07001460{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 /*
1462 * Protect the call to nfs4_state_set_mode_locked and
1463 * serialise the stateid update
1464 */
Andrew Elble361cad32015-12-02 09:20:57 -05001465 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001466 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001467 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001468 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001469 set_bit(NFS_DELEGATED_STATE, &state->flags);
1470 }
1471 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001472 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001473 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001474 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001475 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478static 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 -05001479{
1480 struct nfs_inode *nfsi = NFS_I(state->inode);
1481 struct nfs_delegation *deleg_cur;
1482 int ret = 0;
1483
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001485
1486 rcu_read_lock();
1487 deleg_cur = rcu_dereference(nfsi->delegation);
1488 if (deleg_cur == NULL)
1489 goto no_delegation;
1490
1491 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001492 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001493 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001495 goto no_delegation_unlock;
1496
1497 if (delegation == NULL)
1498 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001499 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001500 goto no_delegation_unlock;
1501
Trond Myklebustb7391f42008-12-23 15:21:52 -05001502 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001504 ret = 1;
1505no_delegation_unlock:
1506 spin_unlock(&deleg_cur->lock);
1507no_delegation:
1508 rcu_read_unlock();
1509
1510 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001511 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001512 ret = 1;
1513 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001514 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1515 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001516
1517 return ret;
1518}
1519
Trond Myklebust39071e62015-01-24 15:07:56 -05001520static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1521 const nfs4_stateid *stateid)
1522{
1523 struct nfs4_state *state = lsp->ls_state;
1524 bool ret = false;
1525
1526 spin_lock(&state->state_lock);
1527 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1530 goto out_noupdate;
1531 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1532 ret = true;
1533out_noupdate:
1534 spin_unlock(&state->state_lock);
1535 return ret;
1536}
Trond Myklebust34310432008-12-23 15:21:38 -05001537
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001539{
1540 struct nfs_delegation *delegation;
1541
1542 rcu_read_lock();
1543 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001544 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001545 rcu_read_unlock();
1546 return;
1547 }
1548 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001549 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001550}
1551
Trond Myklebust6ee41262007-07-08 14:11:36 -04001552static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553{
1554 struct nfs4_state *state = opendata->state;
1555 struct nfs_inode *nfsi = NFS_I(state->inode);
1556 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001557 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001559 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001560 nfs4_stateid stateid;
1561 int ret = -EAGAIN;
1562
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001564 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001565 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001566 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001568 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001569 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001570 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001571 rcu_read_lock();
1572 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001573 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001574 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001575 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001576 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001578 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001579 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001580 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001581 if (!opendata->is_recover) {
1582 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1583 if (ret != 0)
1584 goto out;
1585 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001586 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001587
1588 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001589 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001590 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001591 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001592out:
1593 return ERR_PTR(ret);
1594out_return_state:
1595 atomic_inc(&state->count);
1596 return state;
1597}
1598
Andy Adamsone23008e2012-10-02 21:07:32 -04001599static void
1600nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1601{
1602 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1603 struct nfs_delegation *delegation;
1604 int delegation_flags = 0;
1605
1606 rcu_read_lock();
1607 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1608 if (delegation)
1609 delegation_flags = delegation->flags;
1610 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001611 switch (data->o_arg.claim) {
1612 default:
1613 break;
1614 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1615 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001616 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1617 "returning a delegation for "
1618 "OPEN(CLAIM_DELEGATE_CUR)\n",
1619 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001620 return;
1621 }
1622 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001623 nfs_inode_set_delegation(state->inode,
1624 data->owner->so_cred,
1625 &data->o_res);
1626 else
1627 nfs_inode_reclaim_delegation(state->inode,
1628 data->owner->so_cred,
1629 &data->o_res);
1630}
1631
1632/*
1633 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1634 * and update the nfs4_state.
1635 */
1636static struct nfs4_state *
1637_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1638{
1639 struct inode *inode = data->state->inode;
1640 struct nfs4_state *state = data->state;
1641 int ret;
1642
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001643 if (!data->rpc_done) {
1644 if (data->rpc_status) {
1645 ret = data->rpc_status;
1646 goto err;
1647 }
1648 /* cached opens have already been processed */
1649 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001650 }
1651
Andy Adamsone23008e2012-10-02 21:07:32 -04001652 ret = nfs_refresh_inode(inode, &data->f_attr);
1653 if (ret)
1654 goto err;
1655
1656 if (data->o_res.delegation_type != 0)
1657 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001658update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001659 update_open_stateid(state, &data->o_res.stateid, NULL,
1660 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001661 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001662
1663 return state;
1664err:
1665 return ERR_PTR(ret);
1666
1667}
1668
1669static struct nfs4_state *
1670_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671{
1672 struct inode *inode;
1673 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001674 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001675
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001677 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001678 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 goto out;
1680 }
1681
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001682 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001684 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001685 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001686 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001687 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001688 goto err;
1689 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 state = nfs4_get_open_state(inode, data->owner);
1691 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001692 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001693 if (data->o_res.delegation_type != 0)
1694 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001695 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001698out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001699 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001701err_put_inode:
1702 iput(inode);
1703err:
1704 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705}
1706
Andy Adamsone23008e2012-10-02 21:07:32 -04001707static struct nfs4_state *
1708nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1709{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001710 struct nfs4_state *ret;
1711
Andy Adamsone23008e2012-10-02 21:07:32 -04001712 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001713 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1714 else
1715 ret = _nfs4_opendata_to_nfs4_state(data);
1716 nfs4_sequence_free_slot(&data->o_res.seq_res);
1717 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001718}
1719
Trond Myklebust864472e2006-01-03 09:55:15 +01001720static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1721{
1722 struct nfs_inode *nfsi = NFS_I(state->inode);
1723 struct nfs_open_context *ctx;
1724
1725 spin_lock(&state->inode->i_lock);
1726 list_for_each_entry(ctx, &nfsi->open_files, list) {
1727 if (ctx->state != state)
1728 continue;
1729 get_nfs_open_context(ctx);
1730 spin_unlock(&state->inode->i_lock);
1731 return ctx;
1732 }
1733 spin_unlock(&state->inode->i_lock);
1734 return ERR_PTR(-ENOENT);
1735}
1736
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001737static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1738 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001739{
1740 struct nfs4_opendata *opendata;
1741
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001742 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001743 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001744 if (opendata == NULL)
1745 return ERR_PTR(-ENOMEM);
1746 opendata->state = state;
1747 atomic_inc(&state->count);
1748 return opendata;
1749}
1750
Trond Myklebust24311f82015-09-20 10:50:17 -04001751static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1752 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001753{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001754 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001755 int ret;
1756
Trond Myklebust24311f82015-09-20 10:50:17 -04001757 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001758 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001759 opendata->o_arg.open_flags = 0;
1760 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001761 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1762 NFS_SB(opendata->dentry->d_sb),
1763 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001764 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1765 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1766 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001767 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001768 if (ret != 0)
1769 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001770 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001771 if (IS_ERR(newstate))
1772 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001773 if (newstate != opendata->state)
1774 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001775 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001776 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001777}
1778
1779static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1780{
Trond Myklebust864472e2006-01-03 09:55:15 +01001781 int ret;
1782
Trond Myklebust4f14c192014-02-12 19:15:06 -05001783 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1784 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1785 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1786 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001787 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001788 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001789 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001791 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1792 if (ret != 0)
1793 return ret;
1794 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1795 if (ret != 0)
1796 return ret;
1797 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1798 if (ret != 0)
1799 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 /*
1801 * We may have performed cached opens for all three recoveries.
1802 * Check if we need to update the current stateid.
1803 */
1804 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001805 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001806 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001808 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001809 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001810 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001811 return 0;
1812}
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/*
1815 * OPEN_RECLAIM:
1816 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001818static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001820 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001821 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001822 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int status;
1824
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001825 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1826 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001827 if (IS_ERR(opendata))
1828 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001829 rcu_read_lock();
1830 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001831 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001832 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001833 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001834 opendata->o_arg.u.delegation_type = delegation_type;
1835 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001836 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return status;
1838}
1839
Trond Myklebust539cd032007-06-05 11:46:42 -04001840static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct nfs_server *server = NFS_SERVER(state->inode);
1843 struct nfs4_exception exception = { };
1844 int err;
1845 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001846 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001847 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001848 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1849 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001850 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001851 break;
1852 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 } while (exception.retry);
1854 return err;
1855}
1856
Trond Myklebust864472e2006-01-03 09:55:15 +01001857static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1858{
1859 struct nfs_open_context *ctx;
1860 int ret;
1861
1862 ctx = nfs4_state_find_open_context(state);
1863 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001864 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001865 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001866 put_nfs_open_context(ctx);
1867 return ret;
1868}
1869
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001870static 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 -07001871{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 switch (err) {
1873 default:
1874 printk(KERN_ERR "NFS: %s: unhandled error "
1875 "%d.\n", __func__, err);
1876 case 0:
1877 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001878 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001879 case -ESTALE:
1880 break;
1881 case -NFS4ERR_BADSESSION:
1882 case -NFS4ERR_BADSLOT:
1883 case -NFS4ERR_BAD_HIGH_SLOT:
1884 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1885 case -NFS4ERR_DEADSESSION:
1886 set_bit(NFS_DELEGATED_STATE, &state->flags);
1887 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1888 return -EAGAIN;
1889 case -NFS4ERR_STALE_CLIENTID:
1890 case -NFS4ERR_STALE_STATEID:
1891 set_bit(NFS_DELEGATED_STATE, &state->flags);
1892 case -NFS4ERR_EXPIRED:
1893 /* Don't recall a delegation if it was lost */
1894 nfs4_schedule_lease_recovery(server->nfs_client);
1895 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001896 case -NFS4ERR_MOVED:
1897 nfs4_schedule_migration_recovery(server);
1898 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001899 case -NFS4ERR_LEASE_MOVED:
1900 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1901 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001902 case -NFS4ERR_DELEG_REVOKED:
1903 case -NFS4ERR_ADMIN_REVOKED:
1904 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001905 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001906 nfs_inode_find_state_and_recover(state->inode,
1907 stateid);
1908 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001909 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001910 case -NFS4ERR_DELAY:
1911 case -NFS4ERR_GRACE:
1912 set_bit(NFS_DELEGATED_STATE, &state->flags);
1913 ssleep(1);
1914 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001915 case -ENOMEM:
1916 case -NFS4ERR_DENIED:
1917 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1918 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return err;
1921}
1922
Trond Myklebust24311f82015-09-20 10:50:17 -04001923int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1924 struct nfs4_state *state, const nfs4_stateid *stateid,
1925 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001926{
1927 struct nfs_server *server = NFS_SERVER(state->inode);
1928 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001929 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001930
1931 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1932 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1933 if (IS_ERR(opendata))
1934 return PTR_ERR(opendata);
1935 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001936 write_seqlock(&state->seqlock);
1937 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1938 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001939 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1940 switch (type & (FMODE_READ|FMODE_WRITE)) {
1941 case FMODE_READ|FMODE_WRITE:
1942 case FMODE_WRITE:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1944 if (err)
1945 break;
1946 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1947 if (err)
1948 break;
1949 case FMODE_READ:
1950 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1951 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001952 nfs4_opendata_put(opendata);
1953 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1954}
1955
Chuck Leverbe05c862013-08-09 12:49:47 -04001956static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1957{
1958 struct nfs4_opendata *data = calldata;
1959
Peng Taocb04ad22014-06-11 05:24:15 +08001960 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1961 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962}
1963
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001964static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1965{
1966 struct nfs4_opendata *data = calldata;
1967
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001968 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001969
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001971 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001972 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001973 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001974 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001975 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001976 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001977}
1978
1979static void nfs4_open_confirm_release(void *calldata)
1980{
1981 struct nfs4_opendata *data = calldata;
1982 struct nfs4_state *state = NULL;
1983
1984 /* If this request hasn't been cancelled, do nothing */
1985 if (data->cancelled == 0)
1986 goto out_free;
1987 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001988 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001989 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001990 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001991 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001992 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001993out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001994 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001995}
1996
1997static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001998 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001999 .rpc_call_done = nfs4_open_confirm_done,
2000 .rpc_release = nfs4_open_confirm_release,
2001};
2002
2003/*
2004 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2005 */
2006static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2007{
David Howells2b0143b2015-03-17 22:25:59 +00002008 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002009 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002010 struct rpc_message msg = {
2011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2012 .rpc_argp = &data->c_arg,
2013 .rpc_resp = &data->c_res,
2014 .rpc_cred = data->owner->so_cred,
2015 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002016 struct rpc_task_setup task_setup_data = {
2017 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002018 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002019 .callback_ops = &nfs4_open_confirm_ops,
2020 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002021 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 .flags = RPC_TASK_ASYNC,
2023 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int status;
2025
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002026 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002027 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002028 data->rpc_done = 0;
2029 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002030 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002031 if (data->is_recover)
2032 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002033 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002034 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002036 status = nfs4_wait_for_completion_rpc_task(task);
2037 if (status != 0) {
2038 data->cancelled = 1;
2039 smp_wmb();
2040 } else
2041 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002042 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return status;
2044}
2045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002048 struct nfs4_opendata *data = calldata;
2049 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002050 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002051 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002052
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002053 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002054 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002055 /*
2056 * Check if we still need to send an OPEN call, or if we can use
2057 * a delegation instead.
2058 */
2059 if (data->state != NULL) {
2060 struct nfs_delegation *delegation;
2061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002062 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002063 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002064 rcu_read_lock();
2065 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002066 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002067 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002068 rcu_read_unlock();
2069 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002070 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002071 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002072 switch (claim) {
2073 default:
2074 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002075 case NFS4_OPEN_CLAIM_PREVIOUS:
2076 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2077 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002078 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002079 case NFS4_OPEN_CLAIM_FH:
2080 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002081 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2082 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002083 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002084 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002085 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002086 &data->o_res.seq_res,
2087 task) != 0)
2088 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002089
2090 /* Set the create mode (note dependency on the session type) */
2091 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2092 if (data->o_arg.open_flags & O_EXCL) {
2093 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2094 if (nfs4_has_persistent_session(clp))
2095 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2096 else if (clp->cl_mvops->minor_version > 0)
2097 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2098 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002099 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002100unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002101 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002102 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002103out_no_action:
2104 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002105out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002106 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002107}
2108
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002109static void nfs4_open_done(struct rpc_task *task, void *calldata)
2110{
2111 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002114
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002115 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002116 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002118 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002119 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2120 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 case S_IFREG:
2122 break;
2123 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 break;
2126 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128 break;
2129 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002131 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002133 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002134 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2135 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002136 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002137 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002139
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140static void nfs4_open_release(void *calldata)
2141{
2142 struct nfs4_opendata *data = calldata;
2143 struct nfs4_state *state = NULL;
2144
2145 /* If this request hasn't been cancelled, do nothing */
2146 if (data->cancelled == 0)
2147 goto out_free;
2148 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002149 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150 goto out_free;
2151 /* In case we need an open_confirm, no cleanup! */
2152 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2153 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002154 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002155 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002156 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002158 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002159}
2160
2161static const struct rpc_call_ops nfs4_open_ops = {
2162 .rpc_call_prepare = nfs4_open_prepare,
2163 .rpc_call_done = nfs4_open_done,
2164 .rpc_release = nfs4_open_release,
2165};
2166
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002167static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002168{
David Howells2b0143b2015-03-17 22:25:59 +00002169 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002170 struct nfs_server *server = NFS_SERVER(dir);
2171 struct nfs_openargs *o_arg = &data->o_arg;
2172 struct nfs_openres *o_res = &data->o_res;
2173 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002174 struct rpc_message msg = {
2175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2176 .rpc_argp = o_arg,
2177 .rpc_resp = o_res,
2178 .rpc_cred = data->owner->so_cred,
2179 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002180 struct rpc_task_setup task_setup_data = {
2181 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 .callback_ops = &nfs4_open_ops,
2184 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002185 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 .flags = RPC_TASK_ASYNC,
2187 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188 int status;
2189
Chuck Levera9c92d62013-08-09 12:48:18 -04002190 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002191 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002192 data->rpc_done = 0;
2193 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002194 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002195 data->is_recover = 0;
2196 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002197 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002198 data->is_recover = 1;
2199 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002200 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002201 if (IS_ERR(task))
2202 return PTR_ERR(task);
2203 status = nfs4_wait_for_completion_rpc_task(task);
2204 if (status != 0) {
2205 data->cancelled = 1;
2206 smp_wmb();
2207 } else
2208 status = data->rpc_status;
2209 rpc_put_task(task);
2210
2211 return status;
2212}
2213
2214static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2215{
David Howells2b0143b2015-03-17 22:25:59 +00002216 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002217 struct nfs_openres *o_res = &data->o_res;
2218 int status;
2219
2220 status = nfs4_run_open_task(data, 1);
2221 if (status != 0 || !data->rpc_done)
2222 return status;
2223
Trond Myklebust6926afd2012-01-07 13:22:46 -05002224 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2225
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002226 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2227 status = _nfs4_proc_open_confirm(data);
2228 if (status != 0)
2229 return status;
2230 }
2231
2232 return status;
2233}
2234
Trond Myklebustf3792d62014-07-10 08:54:32 -04002235/*
2236 * Additional permission checks in order to distinguish between an
2237 * open for read, and an open for execute. This works around the
2238 * fact that NFSv4 OPEN treats read and execute permissions as being
2239 * the same.
2240 * Note that in the non-execute case, we want to turn off permission
2241 * checking if we just created a new file (POSIX open() semantics).
2242 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002243static int nfs4_opendata_access(struct rpc_cred *cred,
2244 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002245 struct nfs4_state *state, fmode_t fmode,
2246 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002247{
2248 struct nfs_access_entry cache;
2249 u32 mask;
2250
2251 /* access call failed or for some reason the server doesn't
2252 * support any access modes -- defer access call until later */
2253 if (opendata->o_res.access_supported == 0)
2254 return 0;
2255
2256 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002257 /*
2258 * Use openflags to check for exec, because fmode won't
2259 * always have FMODE_EXEC set when file open for exec.
2260 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002261 if (openflags & __FMODE_EXEC) {
2262 /* ONLY check for exec rights */
2263 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002264 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002265 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002266
2267 cache.cred = cred;
2268 cache.jiffies = jiffies;
2269 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2270 nfs_access_add_cache(state->inode, &cache);
2271
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002272 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002273 return 0;
2274
2275 /* even though OPEN succeeded, access is denied. Close the file */
2276 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002277 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002278}
2279
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002280/*
2281 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2282 */
2283static int _nfs4_proc_open(struct nfs4_opendata *data)
2284{
David Howells2b0143b2015-03-17 22:25:59 +00002285 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002286 struct nfs_server *server = NFS_SERVER(dir);
2287 struct nfs_openargs *o_arg = &data->o_arg;
2288 struct nfs_openres *o_res = &data->o_res;
2289 int status;
2290
2291 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002292 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002294 if (status != 0) {
2295 if (status == -NFS4ERR_BADNAME &&
2296 !(o_arg->open_flags & O_CREAT))
2297 return -ENOENT;
2298 return status;
2299 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002300
Trond Myklebust6926afd2012-01-07 13:22:46 -05002301 nfs_fattr_map_and_free_names(server, &data->f_attr);
2302
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002303 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002304 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002305 if (o_arg->open_flags & O_EXCL)
2306 data->file_created = 1;
2307 else if (o_res->cinfo.before != o_res->cinfo.after)
2308 data->file_created = 1;
2309 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002310 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2311 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002318 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Andy Adamsond83217c2011-03-01 01:34:17 +00002322static int nfs4_recover_expired_lease(struct nfs_server *server)
2323{
2324 return nfs4_client_recover_expired_lease(server->nfs_client);
2325}
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/*
2328 * OPEN_EXPIRED:
2329 * reclaim state on the server after a network partition.
2330 * Assumes caller holds the appropriate lock
2331 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002332static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002334 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002335 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002337 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002338 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002339 if (IS_ERR(opendata))
2340 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002341 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002342 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002343 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002344 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002345 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002348static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002349{
Trond Myklebust539cd032007-06-05 11:46:42 -04002350 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002351 struct nfs4_exception exception = { };
2352 int err;
2353
2354 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002355 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002356 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002357 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2358 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002359 switch (err) {
2360 default:
2361 goto out;
2362 case -NFS4ERR_GRACE:
2363 case -NFS4ERR_DELAY:
2364 nfs4_handle_exception(server, err, &exception);
2365 err = 0;
2366 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002367 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002368out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002369 return err;
2370}
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002375 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Trond Myklebust864472e2006-01-03 09:55:15 +01002377 ctx = nfs4_state_find_open_context(state);
2378 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002379 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002380 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002381 put_nfs_open_context(ctx);
2382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002385static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2386{
2387 nfs_remove_bad_delegation(state->inode);
2388 write_seqlock(&state->seqlock);
2389 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2390 write_sequnlock(&state->seqlock);
2391 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2392}
2393
2394static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2395{
2396 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2397 nfs_finish_clear_delegation_stateid(state);
2398}
2399
2400static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2401{
2402 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2403 nfs40_clear_delegation_stateid(state);
2404 return nfs4_open_expired(sp, state);
2405}
2406
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002407#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002408static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002409{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002410 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002411 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002412 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002413 struct rpc_cred *cred;
2414 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002415
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002416 /* Get the delegation credential for use by test/free_stateid */
2417 rcu_read_lock();
2418 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002419 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002420 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002421 return;
2422 }
2423
2424 nfs4_stateid_copy(&stateid, &delegation->stateid);
2425 cred = get_rpccred(delegation->cred);
2426 rcu_read_unlock();
2427 status = nfs41_test_stateid(server, &stateid, cred);
2428 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002429
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002430 if (status != NFS_OK) {
2431 /* Free the stateid unless the server explicitly
2432 * informs us the stateid is unrecognized. */
2433 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434 nfs41_free_stateid(server, &stateid, cred);
2435 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002436 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002437
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002438 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002439}
2440
2441/**
2442 * nfs41_check_open_stateid - possibly free an open stateid
2443 *
2444 * @state: NFSv4 state for an inode
2445 *
2446 * Returns NFS_OK if recovery for this stateid is now finished.
2447 * Otherwise a negative NFS4ERR value is returned.
2448 */
2449static int nfs41_check_open_stateid(struct nfs4_state *state)
2450{
2451 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002452 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002453 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002454 int status;
2455
2456 /* If a state reset has been done, test_stateid is unneeded */
2457 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2458 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2459 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2460 return -NFS4ERR_BAD_STATEID;
2461
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002462 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002463 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002464 if (status != NFS_OK) {
2465 /* Free the stateid unless the server explicitly
2466 * informs us the stateid is unrecognized. */
2467 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002468 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002469
2470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2471 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2472 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002473 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002474 }
2475 return status;
2476}
2477
2478static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2479{
Chuck Levereb64cf92012-07-11 16:30:05 -04002480 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002481
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002482 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002483 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002484 if (status != NFS_OK)
2485 status = nfs4_open_expired(sp, state);
2486 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002487}
2488#endif
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002491 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2492 * fields corresponding to attributes that were used to store the verifier.
2493 * Make sure we clobber those fields in the later setattr call
2494 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002495static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2496 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002497{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002498 const u32 *attrset = opendata->o_res.attrset;
2499
2500 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002501 !(sattr->ia_valid & ATTR_ATIME_SET))
2502 sattr->ia_valid |= ATTR_ATIME;
2503
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002504 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002505 !(sattr->ia_valid & ATTR_MTIME_SET))
2506 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002507
2508 /* Except MODE, it seems harmless of setting twice. */
2509 if ((attrset[1] & FATTR4_WORD1_MODE))
2510 sattr->ia_valid &= ~ATTR_MODE;
2511
2512 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2513 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002514}
2515
Trond Myklebustc21443c2013-02-07 14:26:21 -05002516static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2517 fmode_t fmode,
2518 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002519 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002520{
2521 struct nfs4_state_owner *sp = opendata->owner;
2522 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002523 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002524 struct nfs4_state *state;
2525 unsigned int seq;
2526 int ret;
2527
2528 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2529
2530 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002531 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002532 goto out;
2533
2534 state = nfs4_opendata_to_nfs4_state(opendata);
2535 ret = PTR_ERR(state);
2536 if (IS_ERR(state))
2537 goto out;
2538 if (server->caps & NFS_CAP_POSIX_LOCK)
2539 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2540
Trond Myklebust275bb302013-05-29 13:11:28 -04002541 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002542 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002543 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002544 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002545 alias = d_exact_alias(dentry, state->inode);
2546 if (!alias)
2547 alias = d_splice_alias(igrab(state->inode), dentry);
2548 /* d_splice_alias() can't fail here - it's a non-directory */
2549 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002550 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002551 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002552 }
2553 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002554 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002555 }
2556
Trond Myklebustc21443c2013-02-07 14:26:21 -05002557 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2558 if (ret != 0)
2559 goto out;
2560
Trond Myklebust3efb9722013-05-29 13:17:04 -04002561 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002562 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002563 nfs_inode_attach_open_context(ctx);
2564 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2565 nfs4_schedule_stateid_recovery(server, state);
2566 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002567out:
2568 return ret;
2569}
2570
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002571/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002572 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 */
Andy Adamson82be4172012-05-23 05:02:35 -04002574static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002575 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002576 int flags,
2577 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002578 struct nfs4_label *label,
2579 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 struct nfs4_state_owner *sp;
2582 struct nfs4_state *state = NULL;
2583 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002584 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002585 struct dentry *dentry = ctx->dentry;
2586 struct rpc_cred *cred = ctx->cred;
2587 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2588 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002589 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002590 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002591 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002595 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2596 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2598 goto out_err;
2599 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002600 status = nfs4_recover_expired_lease(server);
2601 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002602 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002603 if (d_really_is_positive(dentry))
2604 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002605 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002606 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002607 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002608 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002609 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002610 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002611 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
David Quigley14c43f72013-05-22 12:50:43 -04002613 if (label) {
2614 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2615 if (IS_ERR(olabel)) {
2616 status = PTR_ERR(olabel);
2617 goto err_opendata_put;
2618 }
2619 }
2620
Trond Myklebuste911b812014-03-26 13:24:37 -07002621 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2622 if (!opendata->f_attr.mdsthreshold) {
2623 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2624 if (!opendata->f_attr.mdsthreshold)
2625 goto err_free_label;
2626 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002627 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002628 }
David Howells2b0143b2015-03-17 22:25:59 +00002629 if (d_really_is_positive(dentry))
2630 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002631
Trond Myklebust3efb9722013-05-29 13:17:04 -04002632 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002633 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002634 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002635 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002636
NeilBrownefcbc042015-07-30 13:00:56 +10002637 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002638 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002639 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002640 /*
2641 * send create attributes which was not set by open
2642 * with an extra setattr.
2643 */
2644 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2645 nfs_fattr_init(opendata->o_res.f_attr);
2646 status = nfs4_do_setattr(state->inode, cred,
2647 opendata->o_res.f_attr, sattr,
2648 state, label, olabel);
2649 if (status == 0) {
2650 nfs_setattr_update_inode(state->inode, sattr,
2651 opendata->o_res.f_attr);
2652 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2653 }
David Quigley1775fd32013-05-22 12:50:42 -04002654 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002655 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002656 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002657 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002658
Trond Myklebuste911b812014-03-26 13:24:37 -07002659 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002660 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002661 opendata->f_attr.mdsthreshold = NULL;
2662 }
Andy Adamson82be4172012-05-23 05:02:35 -04002663
David Quigley14c43f72013-05-22 12:50:43 -04002664 nfs4_label_free(olabel);
2665
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002666 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002669err_free_label:
2670 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002671err_opendata_put:
2672 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002673err_put_state_owner:
2674 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return status;
2677}
2678
2679
Andy Adamson82be4172012-05-23 05:02:35 -04002680static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002681 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002682 int flags,
2683 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002684 struct nfs4_label *label,
2685 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002687 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct nfs4_exception exception = { };
2689 struct nfs4_state *res;
2690 int status;
2691
2692 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002693 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002694 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002695 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (status == 0)
2697 break;
2698 /* NOTE: BAD_SEQID means the server and client disagree about the
2699 * book-keeping w.r.t. state-changing operations
2700 * (OPEN/CLOSE/LOCK/LOCKU...)
2701 * It is actually a sign of a bug on the client or on the server.
2702 *
2703 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002704 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * have unhashed the old state_owner for us, and that we can
2706 * therefore safely retry using a new one. We should still warn
2707 * the user though...
2708 */
2709 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002710 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002711 " returned a bad sequence-id error!\n",
2712 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 exception.retry = 1;
2714 continue;
2715 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002716 /*
2717 * BAD_STATEID on OPEN means that the server cancelled our
2718 * state before it received the OPEN_CONFIRM.
2719 * Recover by retrying the request as per the discussion
2720 * on Page 181 of RFC3530.
2721 */
2722 if (status == -NFS4ERR_BAD_STATEID) {
2723 exception.retry = 1;
2724 continue;
2725 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002726 if (status == -EAGAIN) {
2727 /* We must have found a delegation */
2728 exception.retry = 1;
2729 continue;
2730 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002731 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2732 continue;
2733 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 status, &exception));
2735 } while (exception.retry);
2736 return res;
2737}
2738
Trond Myklebust8487c472016-06-26 08:44:35 -04002739static int _nfs4_do_setattr(struct inode *inode,
2740 struct nfs_setattrargs *arg,
2741 struct nfs_setattrres *res,
2742 struct rpc_cred *cred,
2743 struct nfs4_state *state)
2744{
2745 struct nfs_server *server = NFS_SERVER(inode);
2746 struct rpc_message msg = {
2747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2748 .rpc_argp = arg,
2749 .rpc_resp = res,
2750 .rpc_cred = cred,
2751 };
2752 struct rpc_cred *delegation_cred = NULL;
2753 unsigned long timestamp = jiffies;
2754 fmode_t fmode;
2755 bool truncate;
2756 int status;
2757
2758 nfs_fattr_init(res->fattr);
2759
2760 /* Servers should only apply open mode checks for file size changes */
2761 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2762 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2763
2764 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2765 /* Use that stateid */
2766 } else if (truncate && state != NULL) {
2767 struct nfs_lockowner lockowner = {
2768 .l_owner = current->files,
2769 .l_pid = current->tgid,
2770 };
2771 if (!nfs4_valid_open_stateid(state))
2772 return -EBADF;
2773 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2774 &arg->stateid, &delegation_cred) == -EIO)
2775 return -EBADF;
2776 } else
2777 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2778 if (delegation_cred)
2779 msg.rpc_cred = delegation_cred;
2780
2781 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2782
2783 put_rpccred(delegation_cred);
2784 if (status == 0 && state != NULL)
2785 renew_lease(server, timestamp);
2786 trace_nfs4_setattr(inode, &arg->stateid, status);
2787 return status;
2788}
2789
2790static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2791 struct nfs_fattr *fattr, struct iattr *sattr,
2792 struct nfs4_state *state, struct nfs4_label *ilabel,
2793 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002795 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002797 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 .iap = sattr,
2799 .server = server,
2800 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002801 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 };
2803 struct nfs_setattrres res = {
2804 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002805 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .server = server,
2807 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002808 struct nfs4_exception exception = {
2809 .state = state,
2810 .inode = inode,
2811 .stateid = &arg.stateid,
2812 };
2813 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
David Quigleyaa9c2662013-05-22 12:50:44 -04002815 arg.bitmask = nfs4_bitmask(server, ilabel);
2816 if (ilabel)
2817 arg.bitmask = nfs4_bitmask(server, olabel);
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002820 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002821 switch (err) {
2822 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002823 if (!(sattr->ia_valid & ATTR_SIZE)) {
2824 pr_warn_once("NFSv4: server %s is incorrectly "
2825 "applying open mode checks to "
2826 "a SETATTR that is not "
2827 "changing file size.\n",
2828 server->nfs_client->cl_hostname);
2829 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002830 if (state && !(state->state & FMODE_WRITE)) {
2831 err = -EBADF;
2832 if (sattr->ia_valid & ATTR_OPEN)
2833 err = -EACCES;
2834 goto out;
2835 }
2836 }
2837 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002839out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return err;
2841}
2842
Peng Tao500d7012015-09-22 11:35:22 +08002843static bool
2844nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2845{
2846 if (inode == NULL || !nfs_have_layout(inode))
2847 return false;
2848
2849 return pnfs_wait_on_layoutreturn(inode, task);
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852struct nfs4_closedata {
2853 struct inode *inode;
2854 struct nfs4_state *state;
2855 struct nfs_closeargs arg;
2856 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002857 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002858 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002859 bool roc;
2860 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861};
2862
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002863static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002864{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002865 struct nfs4_closedata *calldata = data;
2866 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002867 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002868
Fred Isamanf7e89172011-01-06 11:36:32 +00002869 if (calldata->roc)
2870 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002871 nfs4_put_open_state(calldata->state);
2872 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002873 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002874 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002875 kfree(calldata);
2876}
2877
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002878static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002880 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002883 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Chuck Levera3ca5652012-03-01 17:00:40 -05002885 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002886 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2887 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002888 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /* hmm. we are done with the inode, and in the process of freeing
2890 * the state_owner. we keep this around to process errors
2891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 switch (task->tk_status) {
2893 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002894 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002895 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002896 pnfs_roc_set_barrier(state->inode,
2897 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002898 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002899 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002900 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002902 case -NFS4ERR_OLD_STATEID:
2903 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002905 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002906 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002907 rpc_restart_call_prepare(task);
2908 goto out_release;
2909 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002910 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002913 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002914 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002915 goto out_release;
2916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002918 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2919 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002920out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002921 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002922 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002923 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002926static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002928 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002929 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002930 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002931 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002932 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002933
Chuck Levera3ca5652012-03-01 17:00:40 -05002934 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002935 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002936 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002937
Trond Myklebust88069f72009-12-08 08:33:16 -05002938 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002939 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002940 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2941 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2942 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002943 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002944 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002945 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002946 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002947 if (state->n_rdonly == 0)
2948 call_close |= is_rdonly;
2949 else if (is_rdonly)
2950 calldata->arg.fmode |= FMODE_READ;
2951 if (state->n_wronly == 0)
2952 call_close |= is_wronly;
2953 else if (is_wronly)
2954 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002955 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2956 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002957 } else if (is_rdwr)
2958 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2959
Trond Myklebust5d422302013-03-14 16:57:48 -04002960 if (!nfs4_valid_open_stateid(state))
2961 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002962 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002963
2964 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002965 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002966 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002967 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002968
Peng Tao500d7012015-09-22 11:35:22 +08002969 if (nfs4_wait_on_layoutreturn(inode, task)) {
2970 nfs_release_seqid(calldata->arg.seqid);
2971 goto out_wait;
2972 }
2973
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002974 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002975 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002976 if (calldata->roc)
2977 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2978
Trond Myklebust6ae37332015-01-30 14:21:14 -05002979 calldata->arg.share_access =
2980 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2981 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002982
Trond Myklebust516a6af2005-10-27 22:12:41 -04002983 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002984 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002985 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002986 &calldata->arg.seq_args,
2987 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002988 task) != 0)
2989 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002990 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002991 return;
2992out_no_action:
2993 task->tk_action = NULL;
2994out_wait:
2995 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002998static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002999 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003000 .rpc_call_done = nfs4_close_done,
3001 .rpc_release = nfs4_free_closedata,
3002};
3003
Peng Taofe08c542014-07-03 13:05:01 +08003004static bool nfs4_roc(struct inode *inode)
3005{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003006 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003007 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003008 return pnfs_roc(inode);
3009}
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011/*
3012 * It is possible for data to be read/written from a mem-mapped file
3013 * after the sys_close call (which hits the vfs layer as a flush).
3014 * This means that we can't safely call nfsv4 close on a file until
3015 * the inode is cleared. This in turn means that we are not good
3016 * NFSv4 citizens - we do not indicate to the server to update the file's
3017 * share state even when we are done with one of the three share
3018 * stateid's in the inode.
3019 *
3020 * NOTE: Caller must be holding the sp->so_owner semaphore!
3021 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003022int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003024 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003025 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003027 struct nfs4_state_owner *sp = state->owner;
3028 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003029 struct rpc_message msg = {
3030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3031 .rpc_cred = state->owner->so_cred,
3032 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003033 struct rpc_task_setup task_setup_data = {
3034 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003035 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003036 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003037 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003038 .flags = RPC_TASK_ASYNC,
3039 };
Trond Myklebust95121352005-10-18 14:20:12 -07003040 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003042 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3043 &task_setup_data.rpc_client, &msg);
3044
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003045 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003047 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003048 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003049 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003051 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003053 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3054 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003055 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003056 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003057 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003058 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003059 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003060 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003061 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003062 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003063 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003064
Trond Myklebust1174dd12010-12-21 10:52:24 -05003065 msg.rpc_argp = &calldata->arg;
3066 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003067 task_setup_data.callback_data = calldata;
3068 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003069 if (IS_ERR(task))
3070 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003071 status = 0;
3072 if (wait)
3073 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003074 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003075 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003076out_free_calldata:
3077 kfree(calldata);
3078out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003079 nfs4_put_open_state(state);
3080 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003081 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Trond Myklebust2b484292010-09-17 10:56:51 -04003084static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003085nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3086 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003089 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3090
3091 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Trond Myklebust565277f2007-10-15 18:17:53 -04003093 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003094 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003095
3096 nfs4_label_release_security(label);
3097
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003098 if (IS_ERR(state))
3099 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003100 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Trond Myklebust1185a552009-12-03 15:54:02 -05003103static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003104{
3105 if (ctx->state == NULL)
3106 return;
3107 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003108 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003109 else
Al Viro643168c2011-06-22 18:20:23 -04003110 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Trond Myklebustb944dba2013-11-04 15:20:20 -05003113#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3114#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003115#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3118{
Kinglong Mee8c612822015-08-26 21:12:58 +08003119 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003120 struct nfs4_server_caps_arg args = {
3121 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003122 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct nfs4_server_caps_res res = {};
3125 struct rpc_message msg = {
3126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003127 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 .rpc_resp = &res,
3129 };
3130 int status;
3131
Kinglong Mee8c612822015-08-26 21:12:58 +08003132 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3133 FATTR4_WORD0_FH_EXPIRE_TYPE |
3134 FATTR4_WORD0_LINK_SUPPORT |
3135 FATTR4_WORD0_SYMLINK_SUPPORT |
3136 FATTR4_WORD0_ACLSUPPORT;
3137 if (minorversion)
3138 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3139
Bryan Schumaker7c513052011-03-24 17:12:24 +00003140 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003142 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003143 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003144 case 0:
3145 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3146 res.attr_bitmask[2] = 0;
3147 break;
3148 case 1:
3149 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3150 break;
3151 case 2:
3152 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003155 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3156 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3157 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3158 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003159 NFS_CAP_CTIME|NFS_CAP_MTIME|
3160 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003161 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3162 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 server->caps |= NFS_CAP_ACLS;
3164 if (res.has_links != 0)
3165 server->caps |= NFS_CAP_HARDLINKS;
3166 if (res.has_symlinks != 0)
3167 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003168 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3169 server->caps |= NFS_CAP_FILEID;
3170 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3171 server->caps |= NFS_CAP_MODE;
3172 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3173 server->caps |= NFS_CAP_NLINK;
3174 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3175 server->caps |= NFS_CAP_OWNER;
3176 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3177 server->caps |= NFS_CAP_OWNER_GROUP;
3178 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3179 server->caps |= NFS_CAP_ATIME;
3180 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3181 server->caps |= NFS_CAP_CTIME;
3182 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3183 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003184#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3185 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3186 server->caps |= NFS_CAP_SECURITY_LABEL;
3187#endif
3188 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3189 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003190 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003191
Trond Myklebusta65318b2009-03-11 14:10:28 -04003192 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3193 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3194 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003195 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003196 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3197 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003199 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return status;
3203}
3204
Trond Myklebust55a97592006-06-09 09:34:19 -04003205int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 struct nfs4_exception exception = { };
3208 int err;
3209 do {
3210 err = nfs4_handle_exception(server,
3211 _nfs4_server_capabilities(server, fhandle),
3212 &exception);
3213 } while (exception.retry);
3214 return err;
3215}
3216
3217static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3218 struct nfs_fsinfo *info)
3219{
David Quigleyaa9c2662013-05-22 12:50:44 -04003220 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003222 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 };
3224 struct nfs4_lookup_res res = {
3225 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003226 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 .fh = fhandle,
3228 };
3229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3231 .rpc_argp = &args,
3232 .rpc_resp = &res,
3233 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003234
David Quigleyaa9c2662013-05-22 12:50:44 -04003235 bitmask[0] = nfs4_fattr_bitmap[0];
3236 bitmask[1] = nfs4_fattr_bitmap[1];
3237 /*
3238 * Process the label in the upcoming getfattr
3239 */
3240 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3241
Trond Myklebust0e574af2005-10-27 22:12:38 -04003242 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003243 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
3246static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3247 struct nfs_fsinfo *info)
3248{
3249 struct nfs4_exception exception = { };
3250 int err;
3251 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003252 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003253 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003254 switch (err) {
3255 case 0:
3256 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003257 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003258 default:
3259 err = nfs4_handle_exception(server, err, &exception);
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003262out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return err;
3264}
3265
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003266static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3267 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3268{
Trond Myklebustc2190662013-08-26 19:23:04 -04003269 struct rpc_auth_create_args auth_args = {
3270 .pseudoflavor = flavor,
3271 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003272 struct rpc_auth *auth;
3273 int ret;
3274
Trond Myklebustc2190662013-08-26 19:23:04 -04003275 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003276 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003277 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003278 goto out;
3279 }
3280 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003281out:
3282 return ret;
3283}
3284
Chuck Lever9a744ba2013-03-16 15:56:02 -04003285/*
3286 * Retry pseudoroot lookup with various security flavors. We do this when:
3287 *
3288 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3289 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3290 *
3291 * Returns zero on success, or a negative NFS4ERR value, or a
3292 * negative errno value.
3293 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003294static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003295 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003297 /* Per 3530bis 15.33.5 */
3298 static const rpc_authflavor_t flav_array[] = {
3299 RPC_AUTH_GSS_KRB5P,
3300 RPC_AUTH_GSS_KRB5I,
3301 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003302 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003303 RPC_AUTH_NULL,
3304 };
3305 int status = -EPERM;
3306 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003308 if (server->auth_info.flavor_len > 0) {
3309 /* try each flavor specified by user */
3310 for (i = 0; i < server->auth_info.flavor_len; i++) {
3311 status = nfs4_lookup_root_sec(server, fhandle, info,
3312 server->auth_info.flavors[i]);
3313 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3314 continue;
3315 break;
3316 }
3317 } else {
3318 /* no flavors specified by user, try default list */
3319 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3320 status = nfs4_lookup_root_sec(server, fhandle, info,
3321 flav_array[i]);
3322 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3323 continue;
3324 break;
3325 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003326 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003327
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003328 /*
3329 * -EACCESS could mean that the user doesn't have correct permissions
3330 * to access the mount. It could also mean that we tried to mount
3331 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3332 * existing mount programs don't handle -EACCES very well so it should
3333 * be mapped to -EPERM instead.
3334 */
3335 if (status == -EACCES)
3336 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003337 return status;
3338}
3339
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003340/**
3341 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3342 * @server: initialized nfs_server handle
3343 * @fhandle: we fill in the pseudo-fs root file handle
3344 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003345 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003346 *
3347 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003348 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003349int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003350 struct nfs_fsinfo *info,
3351 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003352{
Andre Przywarac7757072015-04-23 17:17:40 +01003353 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003354
Andre Przywarac7757072015-04-23 17:17:40 +01003355 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003356 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003357
3358 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003359 status = server->nfs_client->cl_mvops->find_root_sec(server,
3360 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (status == 0)
3363 status = nfs4_server_capabilities(server, fhandle);
3364 if (status == 0)
3365 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003366
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003367 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Bryan Schumakerbae36242012-05-10 15:07:31 -04003370static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3371 struct nfs_fsinfo *info)
3372{
3373 int error;
3374 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003375 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003376
3377 error = nfs4_server_capabilities(server, mntfh);
3378 if (error < 0) {
3379 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3380 return error;
3381 }
3382
David Quigley14c43f72013-05-22 12:50:43 -04003383 label = nfs4_label_alloc(server, GFP_KERNEL);
3384 if (IS_ERR(label))
3385 return PTR_ERR(label);
3386
David Quigley1775fd32013-05-22 12:50:42 -04003387 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003388 if (error < 0) {
3389 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003390 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003391 }
3392
3393 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3394 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3395 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3396
David Quigley14c43f72013-05-22 12:50:43 -04003397err_free_label:
3398 nfs4_label_free(label);
3399
Bryan Schumakerbae36242012-05-10 15:07:31 -04003400 return error;
3401}
3402
Manoj Naik6b97fd32006-06-09 09:34:29 -04003403/*
3404 * Get locations and (maybe) other attributes of a referral.
3405 * Note that we'll actually follow the referral later when
3406 * we detect fsid mismatch in inode revalidation
3407 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003408static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3409 const struct qstr *name, struct nfs_fattr *fattr,
3410 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003411{
3412 int status = -ENOMEM;
3413 struct page *page = NULL;
3414 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003415
3416 page = alloc_page(GFP_KERNEL);
3417 if (page == NULL)
3418 goto out;
3419 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3420 if (locations == NULL)
3421 goto out;
3422
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003423 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003424 if (status != 0)
3425 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003426
3427 /*
3428 * If the fsid didn't change, this is a migration event, not a
3429 * referral. Cause us to drop into the exception handler, which
3430 * will kick off migration recovery.
3431 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003432 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003433 dprintk("%s: server did not return a different fsid for"
3434 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003435 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003436 goto out;
3437 }
Andy Adamson533eb462011-06-13 18:25:56 -04003438 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3439 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003440
Andy Adamson533eb462011-06-13 18:25:56 -04003441 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003442 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003443 memset(fhandle, 0, sizeof(struct nfs_fh));
3444out:
3445 if (page)
3446 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003447 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003448 return status;
3449}
3450
David Quigley1775fd32013-05-22 12:50:42 -04003451static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3452 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
3454 struct nfs4_getattr_arg args = {
3455 .fh = fhandle,
3456 .bitmask = server->attr_bitmask,
3457 };
3458 struct nfs4_getattr_res res = {
3459 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003460 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 .server = server,
3462 };
3463 struct rpc_message msg = {
3464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3465 .rpc_argp = &args,
3466 .rpc_resp = &res,
3467 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003468
3469 args.bitmask = nfs4_bitmask(server, label);
3470
Trond Myklebust0e574af2005-10-27 22:12:38 -04003471 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003472 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
David Quigley1775fd32013-05-22 12:50:42 -04003475static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3476 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
3478 struct nfs4_exception exception = { };
3479 int err;
3480 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003481 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3482 trace_nfs4_getattr(server, fhandle, fattr, err);
3483 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 &exception);
3485 } while (exception.retry);
3486 return err;
3487}
3488
3489/*
3490 * The file is not closed if it is opened due to the a request to change
3491 * the size of the file. The open call will not be needed once the
3492 * VFS layer lookup-intents are implemented.
3493 *
3494 * Close is called when the inode is destroyed.
3495 * If we haven't opened the file for O_WRONLY, we
3496 * need to in the size_change case to obtain a stateid.
3497 *
3498 * Got race?
3499 * Because OPEN is always done by name in nfsv4, it is
3500 * possible that we opened a different file by the same
3501 * name. We can recognize this race condition, but we
3502 * can't do anything about it besides returning an error.
3503 *
3504 * This will be fixed with VFS changes (lookup-intent).
3505 */
3506static int
3507nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3508 struct iattr *sattr)
3509{
David Howells2b0143b2015-03-17 22:25:59 +00003510 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003511 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003512 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003513 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 int status;
3515
Peng Tao88ac8152014-09-12 11:04:10 +08003516 if (pnfs_ld_layoutret_on_setattr(inode) &&
3517 sattr->ia_valid & ATTR_SIZE &&
3518 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003519 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003520
Trond Myklebust0e574af2005-10-27 22:12:38 -04003521 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Andy Adamson26699402012-05-30 16:12:24 -04003523 /* Deal with open(O_TRUNC) */
3524 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003525 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003526
3527 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003528 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003529 return 0;
3530
Trond Myklebustd5308382005-11-04 15:33:38 -05003531 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003532 if (sattr->ia_valid & ATTR_FILE) {
3533 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003534
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003535 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003536 if (ctx) {
3537 cred = ctx->cred;
3538 state = ctx->state;
3539 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003540 }
3541
David Quigley14c43f72013-05-22 12:50:43 -04003542 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3543 if (IS_ERR(label))
3544 return PTR_ERR(label);
3545
3546 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003547 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003548 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 nfs_setsecurity(inode, fattr, label);
3550 }
David Quigley14c43f72013-05-22 12:50:43 -04003551 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return status;
3553}
3554
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003555static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3556 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003557 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003558{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003559 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003560 int status;
3561 struct nfs4_lookup_arg args = {
3562 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003563 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003564 .name = name,
3565 };
3566 struct nfs4_lookup_res res = {
3567 .server = server,
3568 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003569 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003570 .fh = fhandle,
3571 };
3572 struct rpc_message msg = {
3573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3574 .rpc_argp = &args,
3575 .rpc_resp = &res,
3576 };
3577
David Quigleyaa9c2662013-05-22 12:50:44 -04003578 args.bitmask = nfs4_bitmask(server, label);
3579
David Howells2b3de442006-08-22 20:06:09 -04003580 nfs_fattr_init(fattr);
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003583 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 dprintk("NFS reply lookup: %d\n", status);
3585 return status;
3586}
3587
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003588static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003589{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003590 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003591 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003592 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3593 fattr->nlink = 2;
3594}
3595
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003596static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003597 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003598 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003599{
3600 struct nfs4_exception exception = { };
3601 struct rpc_clnt *client = *clnt;
3602 int err;
3603 do {
David Quigley1775fd32013-05-22 12:50:42 -04003604 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003605 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003606 switch (err) {
3607 case -NFS4ERR_BADNAME:
3608 err = -ENOENT;
3609 goto out;
3610 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003611 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003612 if (err == -NFS4ERR_MOVED)
3613 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003614 goto out;
3615 case -NFS4ERR_WRONGSEC:
3616 err = -EPERM;
3617 if (client != *clnt)
3618 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003619 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003620 if (IS_ERR(client))
3621 return PTR_ERR(client);
3622
3623 exception.retry = 1;
3624 break;
3625 default:
3626 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3627 }
3628 } while (exception.retry);
3629
3630out:
3631 if (err == 0)
3632 *clnt = client;
3633 else if (client != *clnt)
3634 rpc_shutdown_client(client);
3635
3636 return err;
3637}
3638
Al Virobeffb8f2016-07-20 16:34:42 -04003639static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003640 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3641 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003643 int status;
3644 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003645
David Quigley1775fd32013-05-22 12:50:42 -04003646 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003647 if (client != NFS_CLIENT(dir)) {
3648 rpc_shutdown_client(client);
3649 nfs_fixup_secinfo_attributes(fattr);
3650 }
3651 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003654struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003655nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003656 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3657{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003658 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003659 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003660
David Quigley1775fd32013-05-22 12:50:42 -04003661 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003662 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003663 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003664 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003665}
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3668{
Trond Myklebust76b32992007-08-10 17:45:11 -04003669 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 struct nfs4_accessargs args = {
3671 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003672 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003674 struct nfs4_accessres res = {
3675 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003676 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 struct rpc_message msg = {
3678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3679 .rpc_argp = &args,
3680 .rpc_resp = &res,
3681 .rpc_cred = entry->cred,
3682 };
3683 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003684 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 /*
3687 * Determine which access bits we want to ask for...
3688 */
3689 if (mode & MAY_READ)
3690 args.access |= NFS4_ACCESS_READ;
3691 if (S_ISDIR(inode->i_mode)) {
3692 if (mode & MAY_WRITE)
3693 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3694 if (mode & MAY_EXEC)
3695 args.access |= NFS4_ACCESS_LOOKUP;
3696 } else {
3697 if (mode & MAY_WRITE)
3698 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3699 if (mode & MAY_EXEC)
3700 args.access |= NFS4_ACCESS_EXECUTE;
3701 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003702
3703 res.fattr = nfs_alloc_fattr();
3704 if (res.fattr == NULL)
3705 return -ENOMEM;
3706
Bryan Schumaker7c513052011-03-24 17:12:24 +00003707 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003709 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003710 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003712 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return status;
3714}
3715
3716static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3717{
3718 struct nfs4_exception exception = { };
3719 int err;
3720 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003721 err = _nfs4_proc_access(inode, entry);
3722 trace_nfs4_access(inode, err);
3723 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 &exception);
3725 } while (exception.retry);
3726 return err;
3727}
3728
3729/*
3730 * TODO: For the time being, we don't try to get any attributes
3731 * along with any of the zero-copy operations READ, READDIR,
3732 * READLINK, WRITE.
3733 *
3734 * In the case of the first three, we want to put the GETATTR
3735 * after the read-type operation -- this is because it is hard
3736 * to predict the length of a GETATTR response in v4, and thus
3737 * align the READ data correctly. This means that the GETATTR
3738 * may end up partially falling into the page cache, and we should
3739 * shift it into the 'tail' of the xdr_buf before processing.
3740 * To do this efficiently, we need to know the total length
3741 * of data received, which doesn't seem to be available outside
3742 * of the RPC layer.
3743 *
3744 * In the case of WRITE, we also want to put the GETATTR after
3745 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003746 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 *
3748 * Both of these changes to the XDR layer would in fact be quite
3749 * minor, but I decided to leave them for a subsequent patch.
3750 */
3751static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3752 unsigned int pgbase, unsigned int pglen)
3753{
3754 struct nfs4_readlink args = {
3755 .fh = NFS_FH(inode),
3756 .pgbase = pgbase,
3757 .pglen = pglen,
3758 .pages = &page,
3759 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003760 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 struct rpc_message msg = {
3762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3763 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003764 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 };
3766
Bryan Schumaker7c513052011-03-24 17:12:24 +00003767 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 -07003768}
3769
3770static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3771 unsigned int pgbase, unsigned int pglen)
3772{
3773 struct nfs4_exception exception = { };
3774 int err;
3775 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003776 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3777 trace_nfs4_readlink(inode, err);
3778 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 &exception);
3780 } while (exception.retry);
3781 return err;
3782}
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003785 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787static int
3788nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003789 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
David Quigleyaa9c2662013-05-22 12:50:44 -04003791 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003792 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 int status = 0;
3795
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003796 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3797 if (IS_ERR(ctx))
3798 return PTR_ERR(ctx);
3799
David Quigleyaa9c2662013-05-22 12:50:44 -04003800 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3801
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003802 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003803 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 if (IS_ERR(state)) {
3805 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003810 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return status;
3812}
3813
Al Virobeffb8f2016-07-20 16:34:42 -04003814static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
Trond Myklebust16e42952005-10-27 22:12:44 -04003816 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003817 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003819 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003821 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003822 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003823 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3826 .rpc_argp = &args,
3827 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003829 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003830
Bryan Schumaker7c513052011-03-24 17:12:24 +00003831 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003832 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003833 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return status;
3835}
3836
Al Virobeffb8f2016-07-20 16:34:42 -04003837static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 struct nfs4_exception exception = { };
3840 int err;
3841 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003842 err = _nfs4_proc_remove(dir, name);
3843 trace_nfs4_remove(dir, name, err);
3844 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 &exception);
3846 } while (exception.retry);
3847 return err;
3848}
3849
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003850static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003852 struct nfs_server *server = NFS_SERVER(dir);
3853 struct nfs_removeargs *args = msg->rpc_argp;
3854 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003856 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003858 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003859
3860 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861}
3862
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003863static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3864{
Al Viro884be172016-04-28 23:56:31 -04003865 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003866 &data->args.seq_args,
3867 &data->res.seq_res,
3868 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003871static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003873 struct nfs_unlinkdata *data = task->tk_calldata;
3874 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003875
Trond Myklebust14516c32010-07-31 14:29:06 -04003876 if (!nfs4_sequence_done(task, &res->seq_res))
3877 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003878 if (nfs4_async_handle_error(task, res->server, NULL,
3879 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003880 return 0;
3881 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003882 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
Jeff Laytond3d41522010-09-17 17:31:57 -04003885static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3886{
3887 struct nfs_server *server = NFS_SERVER(dir);
3888 struct nfs_renameargs *arg = msg->rpc_argp;
3889 struct nfs_renameres *res = msg->rpc_resp;
3890
3891 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003892 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003893 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003894}
3895
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003896static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3897{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003898 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3899 &data->args.seq_args,
3900 &data->res.seq_res,
3901 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003902}
3903
3904static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3905 struct inode *new_dir)
3906{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003907 struct nfs_renamedata *data = task->tk_calldata;
3908 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003909
3910 if (!nfs4_sequence_done(task, &res->seq_res))
3911 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003912 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003913 return 0;
3914
3915 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003916 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003917 return 1;
3918}
3919
Al Virobeffb8f2016-07-20 16:34:42 -04003920static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003922 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 struct nfs4_link_arg arg = {
3924 .fh = NFS_FH(inode),
3925 .dir_fh = NFS_FH(dir),
3926 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003927 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003929 struct nfs4_link_res res = {
3930 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003931 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003932 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct rpc_message msg = {
3934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3935 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003936 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003938 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Trond Myklebust136f2622010-04-16 16:22:49 -04003940 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003941 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003942 goto out;
3943
David Quigley14c43f72013-05-22 12:50:43 -04003944 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3945 if (IS_ERR(res.label)) {
3946 status = PTR_ERR(res.label);
3947 goto out;
3948 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003949 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003950
Bryan Schumaker7c513052011-03-24 17:12:24 +00003951 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003952 if (!status) {
3953 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003954 status = nfs_post_op_update_inode(inode, res.fattr);
3955 if (!status)
3956 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003957 }
David Quigley14c43f72013-05-22 12:50:43 -04003958
3959
3960 nfs4_label_free(res.label);
3961
Trond Myklebust136f2622010-04-16 16:22:49 -04003962out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003963 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return status;
3965}
3966
Al Virobeffb8f2016-07-20 16:34:42 -04003967static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
3969 struct nfs4_exception exception = { };
3970 int err;
3971 do {
3972 err = nfs4_handle_exception(NFS_SERVER(inode),
3973 _nfs4_proc_link(inode, dir, name),
3974 &exception);
3975 } while (exception.retry);
3976 return err;
3977}
3978
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003979struct nfs4_createdata {
3980 struct rpc_message msg;
3981 struct nfs4_create_arg arg;
3982 struct nfs4_create_res res;
3983 struct nfs_fh fh;
3984 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003985 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003986};
3987
3988static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003989 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003990{
3991 struct nfs4_createdata *data;
3992
3993 data = kzalloc(sizeof(*data), GFP_KERNEL);
3994 if (data != NULL) {
3995 struct nfs_server *server = NFS_SERVER(dir);
3996
David Quigley14c43f72013-05-22 12:50:43 -04003997 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3998 if (IS_ERR(data->label))
3999 goto out_free;
4000
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004001 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4002 data->msg.rpc_argp = &data->arg;
4003 data->msg.rpc_resp = &data->res;
4004 data->arg.dir_fh = NFS_FH(dir);
4005 data->arg.server = server;
4006 data->arg.name = name;
4007 data->arg.attrs = sattr;
4008 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004009 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004010 data->res.server = server;
4011 data->res.fh = &data->fh;
4012 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004013 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004014 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004015 }
4016 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004017out_free:
4018 kfree(data);
4019 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004020}
4021
4022static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4023{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004024 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004025 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004026 if (status == 0) {
4027 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004028 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004029 }
4030 return status;
4031}
4032
4033static void nfs4_free_createdata(struct nfs4_createdata *data)
4034{
David Quigley14c43f72013-05-22 12:50:43 -04004035 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004036 kfree(data);
4037}
4038
Chuck Lever4f390c12006-08-22 20:06:22 -04004039static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004040 struct page *page, unsigned int len, struct iattr *sattr,
4041 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004043 struct nfs4_createdata *data;
4044 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Chuck Lever94a6d752006-08-22 20:06:23 -04004046 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004047 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004048
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004049 status = -ENOMEM;
4050 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4051 if (data == NULL)
4052 goto out;
4053
4054 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4055 data->arg.u.symlink.pages = &page;
4056 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004057 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004059 status = nfs4_do_create(dir, dentry, data);
4060
4061 nfs4_free_createdata(data);
4062out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 return status;
4064}
4065
Chuck Lever4f390c12006-08-22 20:06:22 -04004066static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004067 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
4069 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004070 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004072
4073 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004076 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4077 trace_nfs4_symlink(dir, &dentry->d_name, err);
4078 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 &exception);
4080 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004081
4082 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 return err;
4084}
4085
4086static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004087 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004089 struct nfs4_createdata *data;
4090 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004092 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4093 if (data == NULL)
4094 goto out;
4095
David Quigley1775fd32013-05-22 12:50:42 -04004096 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004097 status = nfs4_do_create(dir, dentry, data);
4098
4099 nfs4_free_createdata(data);
4100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return status;
4102}
4103
4104static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4105 struct iattr *sattr)
4106{
4107 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004108 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004110
David Quigleyaa9c2662013-05-22 12:50:44 -04004111 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4112
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004113 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004115 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4116 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4117 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 &exception);
4119 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004120 nfs4_label_release_security(label);
4121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return err;
4123}
4124
4125static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004126 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
David Howells2b0143b2015-03-17 22:25:59 +00004128 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 struct nfs4_readdir_arg args = {
4130 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004131 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 .pgbase = 0,
4133 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004134 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004135 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 };
4137 struct nfs4_readdir_res res;
4138 struct rpc_message msg = {
4139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4140 .rpc_argp = &args,
4141 .rpc_resp = &res,
4142 .rpc_cred = cred,
4143 };
4144 int status;
4145
Al Viro6de14722013-09-16 10:53:17 -04004146 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4147 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004148 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004149 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004151 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 -05004152 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004153 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004154 status += args.pgbase;
4155 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004156
4157 nfs_invalidate_atime(dir);
4158
Harvey Harrison3110ff82008-05-02 13:42:44 -07004159 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return status;
4161}
4162
4163static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004164 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
4166 struct nfs4_exception exception = { };
4167 int err;
4168 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004169 err = _nfs4_proc_readdir(dentry, cred, cookie,
4170 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004171 trace_nfs4_readdir(d_inode(dentry), err);
4172 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 &exception);
4174 } while (exception.retry);
4175 return err;
4176}
4177
4178static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004179 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004181 struct nfs4_createdata *data;
4182 int mode = sattr->ia_mode;
4183 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4186 if (data == NULL)
4187 goto out;
4188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004190 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192 data->arg.ftype = NF4BLK;
4193 data->arg.u.device.specdata1 = MAJOR(rdev);
4194 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
4196 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004197 data->arg.ftype = NF4CHR;
4198 data->arg.u.device.specdata1 = MAJOR(rdev);
4199 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004200 } else if (!S_ISSOCK(mode)) {
4201 status = -EINVAL;
4202 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
David Quigley1775fd32013-05-22 12:50:42 -04004204
David Quigleyaa9c2662013-05-22 12:50:44 -04004205 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004206 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004207out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 nfs4_free_createdata(data);
4209out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 return status;
4211}
4212
4213static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4214 struct iattr *sattr, dev_t rdev)
4215{
4216 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004217 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004219
David Quigleyaa9c2662013-05-22 12:50:44 -04004220 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4221
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004222 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004224 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4225 trace_nfs4_mknod(dir, &dentry->d_name, err);
4226 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 &exception);
4228 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004229
4230 nfs4_label_release_security(label);
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 return err;
4233}
4234
4235static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4236 struct nfs_fsstat *fsstat)
4237{
4238 struct nfs4_statfs_arg args = {
4239 .fh = fhandle,
4240 .bitmask = server->attr_bitmask,
4241 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004242 struct nfs4_statfs_res res = {
4243 .fsstat = fsstat,
4244 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 struct rpc_message msg = {
4246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4247 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004248 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 };
4250
Trond Myklebust0e574af2005-10-27 22:12:38 -04004251 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004252 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253}
4254
4255static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4256{
4257 struct nfs4_exception exception = { };
4258 int err;
4259 do {
4260 err = nfs4_handle_exception(server,
4261 _nfs4_proc_statfs(server, fhandle, fsstat),
4262 &exception);
4263 } while (exception.retry);
4264 return err;
4265}
4266
4267static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4268 struct nfs_fsinfo *fsinfo)
4269{
4270 struct nfs4_fsinfo_arg args = {
4271 .fh = fhandle,
4272 .bitmask = server->attr_bitmask,
4273 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004274 struct nfs4_fsinfo_res res = {
4275 .fsinfo = fsinfo,
4276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 struct rpc_message msg = {
4278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4279 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004280 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 };
4282
Bryan Schumaker7c513052011-03-24 17:12:24 +00004283 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
4286static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4287{
4288 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004289 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 int err;
4291
4292 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004293 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004294 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004295 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004296 nfs4_set_lease_period(server->nfs_client,
4297 fsinfo->lease_time * HZ,
4298 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004299 break;
4300 }
4301 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 } while (exception.retry);
4303 return err;
4304}
4305
4306static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4307{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004308 int error;
4309
Trond Myklebust0e574af2005-10-27 22:12:38 -04004310 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004311 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004312 if (error == 0) {
4313 /* block layout checks this! */
4314 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004315 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004316 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004317
4318 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
4320
4321static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4322 struct nfs_pathconf *pathconf)
4323{
4324 struct nfs4_pathconf_arg args = {
4325 .fh = fhandle,
4326 .bitmask = server->attr_bitmask,
4327 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004328 struct nfs4_pathconf_res res = {
4329 .pathconf = pathconf,
4330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct rpc_message msg = {
4332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4333 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004334 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 };
4336
4337 /* None of the pathconf attributes are mandatory to implement */
4338 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4339 memset(pathconf, 0, sizeof(*pathconf));
4340 return 0;
4341 }
4342
Trond Myklebust0e574af2005-10-27 22:12:38 -04004343 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004344 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345}
4346
4347static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4348 struct nfs_pathconf *pathconf)
4349{
4350 struct nfs4_exception exception = { };
4351 int err;
4352
4353 do {
4354 err = nfs4_handle_exception(server,
4355 _nfs4_proc_pathconf(server, fhandle, pathconf),
4356 &exception);
4357 } while (exception.retry);
4358 return err;
4359}
4360
Trond Myklebust5521abf2013-03-16 20:54:34 -04004361int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004362 const struct nfs_open_context *ctx,
4363 const struct nfs_lock_context *l_ctx,
4364 fmode_t fmode)
4365{
4366 const struct nfs_lockowner *lockowner = NULL;
4367
4368 if (l_ctx != NULL)
4369 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004370 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004371}
4372EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4373
Trond Myklebust5521abf2013-03-16 20:54:34 -04004374static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4375 const struct nfs_open_context *ctx,
4376 const struct nfs_lock_context *l_ctx,
4377 fmode_t fmode)
4378{
4379 nfs4_stateid current_stateid;
4380
Trond Myklebuste1253be2014-03-05 08:44:23 -05004381 /* If the current stateid represents a lost lock, then exit */
4382 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4383 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004384 return nfs4_stateid_match(stateid, &current_stateid);
4385}
4386
4387static bool nfs4_error_stateid_expired(int err)
4388{
4389 switch (err) {
4390 case -NFS4ERR_DELEG_REVOKED:
4391 case -NFS4ERR_ADMIN_REVOKED:
4392 case -NFS4ERR_BAD_STATEID:
4393 case -NFS4ERR_STALE_STATEID:
4394 case -NFS4ERR_OLD_STATEID:
4395 case -NFS4ERR_OPENMODE:
4396 case -NFS4ERR_EXPIRED:
4397 return true;
4398 }
4399 return false;
4400}
4401
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004402void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004403{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004404 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004405}
4406
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004407static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004409 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004411 trace_nfs4_read(hdr, task->tk_status);
4412 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004413 hdr->args.context->state,
4414 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004415 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004416 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004418
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004419 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004421 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424
Trond Myklebust5521abf2013-03-16 20:54:34 -04004425static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004426 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004427{
4428
4429 if (!nfs4_error_stateid_expired(task->tk_status) ||
4430 nfs4_stateid_is_current(&args->stateid,
4431 args->context,
4432 args->lock_context,
4433 FMODE_READ))
4434 return false;
4435 rpc_restart_call_prepare(task);
4436 return true;
4437}
4438
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004439static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004440{
4441
4442 dprintk("--> %s\n", __func__);
4443
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004444 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004445 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004446 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004447 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004448 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4449 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004450}
4451
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004452static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4453 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004455 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004456 if (!hdr->pgio_done_cb)
4457 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004458 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004459 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004462static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4463 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004464{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4466 &hdr->args.seq_args,
4467 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004468 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004469 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004470 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4471 hdr->args.lock_context,
4472 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004473 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004474 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004475 return -EIO;
4476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004479static int nfs4_write_done_cb(struct rpc_task *task,
4480 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004482 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004483
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004484 trace_nfs4_write(hdr, task->tk_status);
4485 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004486 hdr->args.context->state,
4487 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004488 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004489 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004491 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004492 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004493 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004494 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
4497
Trond Myklebust5521abf2013-03-16 20:54:34 -04004498static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004499 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004500{
4501
4502 if (!nfs4_error_stateid_expired(task->tk_status) ||
4503 nfs4_stateid_is_current(&args->stateid,
4504 args->context,
4505 args->lock_context,
4506 FMODE_WRITE))
4507 return false;
4508 rpc_restart_call_prepare(task);
4509 return true;
4510}
4511
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004512static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004513{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004514 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004515 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004516 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004517 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004518 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4519 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004520}
4521
Trond Myklebust5a37f852012-04-28 14:55:16 -04004522static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004523bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004524{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004525 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004526 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004527 return false;
4528 /* Otherwise, request attributes if and only if we don't hold
4529 * a delegation
4530 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004531 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004532}
Fred Isamana69aef12011-03-03 15:13:47 +00004533
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004534static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4535 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004537 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004538
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004539 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4540 hdr->args.bitmask = NULL;
4541 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004542 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004543 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004544
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004545 if (!hdr->pgio_done_cb)
4546 hdr->pgio_done_cb = nfs4_write_done_cb;
4547 hdr->res.server = server;
4548 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004550 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004551 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552}
4553
Fred Isaman0b7c0152012-04-20 14:47:39 -04004554static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4555{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004556 nfs4_setup_sequence(NFS_SERVER(data->inode),
4557 &data->args.seq_args,
4558 &data->res.seq_res,
4559 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
Fred Isaman0b7c0152012-04-20 14:47:39 -04004562static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004565
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004566 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004567 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4568 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004569 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004570 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Fred Isaman0b7c0152012-04-20 14:47:39 -04004575static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004576{
4577 if (!nfs4_sequence_done(task, &data->res.seq_res))
4578 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004579 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004580}
4581
Fred Isaman0b7c0152012-04-20 14:47:39 -04004582static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004584 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004585
Fred Isaman0b7c0152012-04-20 14:47:39 -04004586 if (data->commit_done_cb == NULL)
4587 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004588 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004589 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004590 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591}
4592
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004593struct nfs4_renewdata {
4594 struct nfs_client *client;
4595 unsigned long timestamp;
4596};
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598/*
4599 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4600 * standalone procedure for queueing an asynchronous RENEW.
4601 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004602static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004603{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004604 struct nfs4_renewdata *data = calldata;
4605 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004606
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004607 if (atomic_read(&clp->cl_count) > 1)
4608 nfs4_schedule_state_renewal(clp);
4609 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004610 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004611}
4612
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004613static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004615 struct nfs4_renewdata *data = calldata;
4616 struct nfs_client *clp = data->client;
4617 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004619 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004620 switch (task->tk_status) {
4621 case 0:
4622 break;
4623 case -NFS4ERR_LEASE_MOVED:
4624 nfs4_schedule_lease_moved_recovery(clp);
4625 break;
4626 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004627 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004628 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4629 return;
4630 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004631 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004632 return;
4633 }
4634 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004636 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004639static const struct rpc_call_ops nfs4_renew_ops = {
4640 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004641 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004642};
4643
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004644static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
4646 struct rpc_message msg = {
4647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4648 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004649 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004651 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004653 if (renew_flags == 0)
4654 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004655 if (!atomic_inc_not_zero(&clp->cl_count))
4656 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004657 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004658 if (data == NULL)
4659 return -ENOMEM;
4660 data->client = clp;
4661 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004662 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004663 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004666static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
4668 struct rpc_message msg = {
4669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4670 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004671 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 };
4673 unsigned long now = jiffies;
4674 int status;
4675
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004676 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 if (status < 0)
4678 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004679 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return 0;
4681}
4682
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004683static inline int nfs4_server_supports_acls(struct nfs_server *server)
4684{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004685 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004686}
4687
Trond Myklebust21f498c2012-08-24 10:59:25 -04004688/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4689 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004690 * the stack.
4691 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004692#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004693
Neil Hormane9e3d722011-03-04 19:26:03 -05004694static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004695 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004696{
4697 struct page *newpage, **spages;
4698 int rc = 0;
4699 size_t len;
4700 spages = pages;
4701
4702 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004703 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004704 newpage = alloc_page(GFP_KERNEL);
4705
4706 if (newpage == NULL)
4707 goto unwind;
4708 memcpy(page_address(newpage), buf, len);
4709 buf += len;
4710 buflen -= len;
4711 *pages++ = newpage;
4712 rc++;
4713 } while (buflen != 0);
4714
4715 return rc;
4716
4717unwind:
4718 for(; rc > 0; rc--)
4719 __free_page(spages[rc-1]);
4720 return -ENOMEM;
4721}
4722
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004723struct nfs4_cached_acl {
4724 int cached;
4725 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004726 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004727};
4728
4729static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004730{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004731 struct nfs_inode *nfsi = NFS_I(inode);
4732
4733 spin_lock(&inode->i_lock);
4734 kfree(nfsi->nfs4_acl);
4735 nfsi->nfs4_acl = acl;
4736 spin_unlock(&inode->i_lock);
4737}
4738
4739static void nfs4_zap_acl_attr(struct inode *inode)
4740{
4741 nfs4_set_cached_acl(inode, NULL);
4742}
4743
4744static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4745{
4746 struct nfs_inode *nfsi = NFS_I(inode);
4747 struct nfs4_cached_acl *acl;
4748 int ret = -ENOENT;
4749
4750 spin_lock(&inode->i_lock);
4751 acl = nfsi->nfs4_acl;
4752 if (acl == NULL)
4753 goto out;
4754 if (buf == NULL) /* user is just asking for length */
4755 goto out_len;
4756 if (acl->cached == 0)
4757 goto out;
4758 ret = -ERANGE; /* see getxattr(2) man page */
4759 if (acl->len > buflen)
4760 goto out;
4761 memcpy(buf, acl->data, acl->len);
4762out_len:
4763 ret = acl->len;
4764out:
4765 spin_unlock(&inode->i_lock);
4766 return ret;
4767}
4768
Sachin Prabhu5794d212012-04-17 14:36:40 +01004769static 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 +00004770{
4771 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004772 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004773
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004774 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004775 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004776 if (acl == NULL)
4777 goto out;
4778 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004779 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004780 } else {
4781 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4782 if (acl == NULL)
4783 goto out;
4784 acl->cached = 0;
4785 }
4786 acl->len = acl_len;
4787out:
4788 nfs4_set_cached_acl(inode, acl);
4789}
4790
Andy Adamsonbf118a32011-12-07 11:55:27 -05004791/*
4792 * The getxattr API returns the required buffer length when called with a
4793 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4794 * the required buf. On a NULL buf, we send a page of data to the server
4795 * guessing that the ACL request can be serviced by a page. If so, we cache
4796 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4797 * the cache. If not so, we throw away the page, and cache the required
4798 * length. The next getxattr call will then produce another round trip to
4799 * the server, this time with the input buf of the required size.
4800 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004801static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004802{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004803 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004804 struct nfs_getaclargs args = {
4805 .fh = NFS_FH(inode),
4806 .acl_pages = pages,
4807 .acl_len = buflen,
4808 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004809 struct nfs_getaclres res = {
4810 .acl_len = buflen,
4811 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004812 struct rpc_message msg = {
4813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4814 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004815 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004816 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004817 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4818 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004819
Andy Adamsonbf118a32011-12-07 11:55:27 -05004820 /* As long as we're doing a round trip to the server anyway,
4821 * let's be prepared for a page of acl data. */
4822 if (npages == 0)
4823 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004824 if (npages > ARRAY_SIZE(pages))
4825 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004826
Andy Adamsonbf118a32011-12-07 11:55:27 -05004827 for (i = 0; i < npages; i++) {
4828 pages[i] = alloc_page(GFP_KERNEL);
4829 if (!pages[i])
4830 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004831 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004832
4833 /* for decoding across pages */
4834 res.acl_scratch = alloc_page(GFP_KERNEL);
4835 if (!res.acl_scratch)
4836 goto out_free;
4837
Andy Adamsonbf118a32011-12-07 11:55:27 -05004838 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004839
Peng Taode040be2012-01-10 22:42:47 +08004840 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004841 __func__, buf, buflen, npages, args.acl_len);
4842 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4843 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004844 if (ret)
4845 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004846
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004847 /* Handle the case where the passed-in buffer is too short */
4848 if (res.acl_flags & NFS4_ACL_TRUNC) {
4849 /* Did the user only issue a request for the acl length? */
4850 if (buf == NULL)
4851 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004852 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004853 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004854 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004855 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004856 if (buf) {
4857 if (res.acl_len > buflen) {
4858 ret = -ERANGE;
4859 goto out_free;
4860 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004861 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004862 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004863out_ok:
4864 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004865out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004866 for (i = 0; i < npages; i++)
4867 if (pages[i])
4868 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004869 if (res.acl_scratch)
4870 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004871 return ret;
4872}
4873
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004874static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4875{
4876 struct nfs4_exception exception = { };
4877 ssize_t ret;
4878 do {
4879 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004880 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004881 if (ret >= 0)
4882 break;
4883 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4884 } while (exception.retry);
4885 return ret;
4886}
4887
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004888static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4889{
4890 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004891 int ret;
4892
4893 if (!nfs4_server_supports_acls(server))
4894 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004895 ret = nfs_revalidate_inode(server, inode);
4896 if (ret < 0)
4897 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004898 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4899 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004900 ret = nfs4_read_cached_acl(inode, buf, buflen);
4901 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004902 /* -ENOENT is returned if there is no ACL or if there is an ACL
4903 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004904 return ret;
4905 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004906}
4907
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004908static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004909{
4910 struct nfs_server *server = NFS_SERVER(inode);
4911 struct page *pages[NFS4ACL_MAXPAGES];
4912 struct nfs_setaclargs arg = {
4913 .fh = NFS_FH(inode),
4914 .acl_pages = pages,
4915 .acl_len = buflen,
4916 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004917 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004918 struct rpc_message msg = {
4919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4920 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004921 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004922 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004923 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004924 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004925
4926 if (!nfs4_server_supports_acls(server))
4927 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004928 if (npages > ARRAY_SIZE(pages))
4929 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004930 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004931 if (i < 0)
4932 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004933 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004934 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004935
4936 /*
4937 * Free each page after tx, so the only ref left is
4938 * held by the network stack
4939 */
4940 for (; i > 0; i--)
4941 put_page(pages[i-1]);
4942
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004943 /*
4944 * Acl update can result in inode attribute update.
4945 * so mark the attribute cache invalid.
4946 */
4947 spin_lock(&inode->i_lock);
4948 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4949 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004950 nfs_access_zap_cache(inode);
4951 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004952 return ret;
4953}
4954
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004955static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4956{
4957 struct nfs4_exception exception = { };
4958 int err;
4959 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004960 err = __nfs4_proc_set_acl(inode, buf, buflen);
4961 trace_nfs4_set_acl(inode, err);
4962 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004963 &exception);
4964 } while (exception.retry);
4965 return err;
4966}
4967
David Quigleyaa9c2662013-05-22 12:50:44 -04004968#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4969static int _nfs4_get_security_label(struct inode *inode, void *buf,
4970 size_t buflen)
4971{
4972 struct nfs_server *server = NFS_SERVER(inode);
4973 struct nfs_fattr fattr;
4974 struct nfs4_label label = {0, 0, buflen, buf};
4975
4976 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004977 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004978 .fh = NFS_FH(inode),
4979 .bitmask = bitmask,
4980 };
4981 struct nfs4_getattr_res res = {
4982 .fattr = &fattr,
4983 .label = &label,
4984 .server = server,
4985 };
4986 struct rpc_message msg = {
4987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004988 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004989 .rpc_resp = &res,
4990 };
4991 int ret;
4992
4993 nfs_fattr_init(&fattr);
4994
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004995 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004996 if (ret)
4997 return ret;
4998 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4999 return -ENOENT;
5000 if (buflen < label.len)
5001 return -ERANGE;
5002 return 0;
5003}
5004
5005static int nfs4_get_security_label(struct inode *inode, void *buf,
5006 size_t buflen)
5007{
5008 struct nfs4_exception exception = { };
5009 int err;
5010
5011 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5012 return -EOPNOTSUPP;
5013
5014 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005015 err = _nfs4_get_security_label(inode, buf, buflen);
5016 trace_nfs4_get_security_label(inode, err);
5017 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005018 &exception);
5019 } while (exception.retry);
5020 return err;
5021}
5022
5023static int _nfs4_do_set_security_label(struct inode *inode,
5024 struct nfs4_label *ilabel,
5025 struct nfs_fattr *fattr,
5026 struct nfs4_label *olabel)
5027{
5028
5029 struct iattr sattr = {0};
5030 struct nfs_server *server = NFS_SERVER(inode);
5031 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005032 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005033 .fh = NFS_FH(inode),
5034 .iap = &sattr,
5035 .server = server,
5036 .bitmask = bitmask,
5037 .label = ilabel,
5038 };
5039 struct nfs_setattrres res = {
5040 .fattr = fattr,
5041 .label = olabel,
5042 .server = server,
5043 };
5044 struct rpc_message msg = {
5045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005046 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005047 .rpc_resp = &res,
5048 };
5049 int status;
5050
Jeff Layton12207f62013-11-01 10:49:32 -04005051 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005052
Jeff Layton12207f62013-11-01 10:49:32 -04005053 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005054 if (status)
5055 dprintk("%s failed: %d\n", __func__, status);
5056
5057 return status;
5058}
5059
5060static int nfs4_do_set_security_label(struct inode *inode,
5061 struct nfs4_label *ilabel,
5062 struct nfs_fattr *fattr,
5063 struct nfs4_label *olabel)
5064{
5065 struct nfs4_exception exception = { };
5066 int err;
5067
5068 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005069 err = _nfs4_do_set_security_label(inode, ilabel,
5070 fattr, olabel);
5071 trace_nfs4_set_security_label(inode, err);
5072 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005073 &exception);
5074 } while (exception.retry);
5075 return err;
5076}
5077
5078static int
Al Viro59301222016-05-27 10:19:30 -04005079nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005080{
5081 struct nfs4_label ilabel, *olabel = NULL;
5082 struct nfs_fattr fattr;
5083 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005084 int status;
5085
5086 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5087 return -EOPNOTSUPP;
5088
5089 nfs_fattr_init(&fattr);
5090
5091 ilabel.pi = 0;
5092 ilabel.lfs = 0;
5093 ilabel.label = (char *)buf;
5094 ilabel.len = buflen;
5095
5096 cred = rpc_lookup_cred();
5097 if (IS_ERR(cred))
5098 return PTR_ERR(cred);
5099
5100 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5101 if (IS_ERR(olabel)) {
5102 status = -PTR_ERR(olabel);
5103 goto out;
5104 }
5105
5106 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5107 if (status == 0)
5108 nfs_setsecurity(inode, &fattr, olabel);
5109
5110 nfs4_label_free(olabel);
5111out:
5112 put_rpccred(cred);
5113 return status;
5114}
5115#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5116
5117
Chuck Leverf0920752012-05-21 22:45:41 -04005118static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5119 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005120{
5121 __be32 verf[2];
5122
Chuck Lever2c820d92012-05-21 22:45:33 -04005123 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5124 /* An impossible timestamp guarantees this value
5125 * will never match a generated boot time. */
5126 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005127 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005128 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005129 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005130 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5131 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005132 }
Chuck Levercd937102012-03-02 17:14:31 -05005133 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5134}
5135
Jeff Laytona3192682015-06-09 19:43:59 -04005136static int
5137nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005138{
Jeff Laytona3192682015-06-09 19:43:59 -04005139 size_t len;
5140 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005141
Trond Myklebustceb3a162015-01-03 15:16:04 -05005142 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005143 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005144
Jeff Laytona3192682015-06-09 19:43:59 -04005145 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005146 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005147 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5148 1 +
5149 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5150 1;
5151 rcu_read_unlock();
5152
5153 if (len > NFS4_OPAQUE_LIMIT + 1)
5154 return -EINVAL;
5155
5156 /*
5157 * Since this string is allocated at mount time, and held until the
5158 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5159 * about a memory-reclaim deadlock.
5160 */
5161 str = kmalloc(len, GFP_KERNEL);
5162 if (!str)
5163 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005164
Chuck Levere984a552012-09-14 17:24:21 -04005165 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005166 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005167 clp->cl_ipaddr,
5168 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5169 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005170 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005171
Jeff Laytona3192682015-06-09 19:43:59 -04005172 clp->cl_owner_id = str;
5173 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005174}
5175
Jeff Layton873e3852015-06-09 19:44:00 -04005176static int
5177nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005178{
Jeff Layton873e3852015-06-09 19:44:00 -04005179 size_t len;
5180 char *str;
5181
5182 len = 10 + 10 + 1 + 10 + 1 +
5183 strlen(nfs4_client_id_uniquifier) + 1 +
5184 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5185
5186 if (len > NFS4_OPAQUE_LIMIT + 1)
5187 return -EINVAL;
5188
5189 /*
5190 * Since this string is allocated at mount time, and held until the
5191 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5192 * about a memory-reclaim deadlock.
5193 */
5194 str = kmalloc(len, GFP_KERNEL);
5195 if (!str)
5196 return -ENOMEM;
5197
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005198 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005199 clp->rpc_ops->version, clp->cl_minorversion,
5200 nfs4_client_id_uniquifier,
5201 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005202 clp->cl_owner_id = str;
5203 return 0;
5204}
5205
5206static int
5207nfs4_init_uniform_client_string(struct nfs_client *clp)
5208{
Jeff Layton873e3852015-06-09 19:44:00 -04005209 size_t len;
5210 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005211
5212 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005213 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005214
5215 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005216 return nfs4_init_uniquifier_client_string(clp);
5217
5218 len = 10 + 10 + 1 + 10 + 1 +
5219 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5220
5221 if (len > NFS4_OPAQUE_LIMIT + 1)
5222 return -EINVAL;
5223
5224 /*
5225 * Since this string is allocated at mount time, and held until the
5226 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5227 * about a memory-reclaim deadlock.
5228 */
5229 str = kmalloc(len, GFP_KERNEL);
5230 if (!str)
5231 return -ENOMEM;
5232
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005233 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005234 clp->rpc_ops->version, clp->cl_minorversion,
5235 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005236 clp->cl_owner_id = str;
5237 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005238}
5239
Chuck Lever706cb8d2014-03-12 12:51:47 -04005240/*
5241 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5242 * services. Advertise one based on the address family of the
5243 * clientaddr.
5244 */
5245static unsigned int
5246nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5247{
5248 if (strchr(clp->cl_ipaddr, ':') != NULL)
5249 return scnprintf(buf, len, "tcp6");
5250 else
5251 return scnprintf(buf, len, "tcp");
5252}
5253
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005254static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5255{
5256 struct nfs4_setclientid *sc = calldata;
5257
5258 if (task->tk_status == 0)
5259 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5260}
5261
5262static const struct rpc_call_ops nfs4_setclientid_ops = {
5263 .rpc_call_done = nfs4_setclientid_done,
5264};
5265
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005266/**
5267 * nfs4_proc_setclientid - Negotiate client ID
5268 * @clp: state data structure
5269 * @program: RPC program for NFSv4 callback service
5270 * @port: IP port number for NFS4 callback service
5271 * @cred: RPC credential to use for this call
5272 * @res: where to place the result
5273 *
5274 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5275 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005276int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5277 unsigned short port, struct rpc_cred *cred,
5278 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
5280 nfs4_verifier sc_verifier;
5281 struct nfs4_setclientid setclientid = {
5282 .sc_verifier = &sc_verifier,
5283 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005284 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 };
5286 struct rpc_message msg = {
5287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5288 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005289 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005290 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005292 struct rpc_task *task;
5293 struct rpc_task_setup task_setup_data = {
5294 .rpc_client = clp->cl_rpcclient,
5295 .rpc_message = &msg,
5296 .callback_ops = &nfs4_setclientid_ops,
5297 .callback_data = &setclientid,
5298 .flags = RPC_TASK_TIMEOUT,
5299 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005300 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Chuck Leverde734832012-07-11 16:30:50 -04005302 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005303 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005304
5305 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5306 status = nfs4_init_uniform_client_string(clp);
5307 else
Jeff Laytona3192682015-06-09 19:43:59 -04005308 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005309
5310 if (status)
5311 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005312
Chuck Leverde734832012-07-11 16:30:50 -04005313 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005314 setclientid.sc_netid_len =
5315 nfs4_init_callback_netid(clp,
5316 setclientid.sc_netid,
5317 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005318 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005319 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 clp->cl_ipaddr, port >> 8, port & 255);
5321
Jeff Layton3a6bb732015-06-09 19:43:57 -04005322 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005323 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005324 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005325 task = rpc_run_task(&task_setup_data);
5326 if (IS_ERR(task)) {
5327 status = PTR_ERR(task);
5328 goto out;
5329 }
5330 status = task->tk_status;
5331 if (setclientid.sc_cred) {
5332 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5333 put_rpccred(setclientid.sc_cred);
5334 }
5335 rpc_put_task(task);
5336out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005337 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005338 dprintk("NFS reply setclientid: %d\n", status);
5339 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340}
5341
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005342/**
5343 * nfs4_proc_setclientid_confirm - Confirm client ID
5344 * @clp: state data structure
5345 * @res: result of a previous SETCLIENTID
5346 * @cred: RPC credential to use for this call
5347 *
5348 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5349 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005350int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005351 struct nfs4_setclientid_res *arg,
5352 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 struct rpc_message msg = {
5355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005356 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005357 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 int status;
5360
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005361 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5362 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5363 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005364 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005365 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005366 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 return status;
5368}
5369
Trond Myklebustfe650402006-01-03 09:55:18 +01005370struct nfs4_delegreturndata {
5371 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005372 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005373 struct nfs_fh fh;
5374 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005375 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005376 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005377 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005378 struct inode *inode;
5379 bool roc;
5380 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005381};
5382
Trond Myklebustfe650402006-01-03 09:55:18 +01005383static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5384{
5385 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005386
Trond Myklebust14516c32010-07-31 14:29:06 -04005387 if (!nfs4_sequence_done(task, &data->res.seq_res))
5388 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005389
Trond Myklebustca8acf82013-08-13 10:36:56 -04005390 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005391 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005392 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005393 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005394 case -NFS4ERR_ADMIN_REVOKED:
5395 case -NFS4ERR_DELEG_REVOKED:
5396 case -NFS4ERR_BAD_STATEID:
5397 case -NFS4ERR_OLD_STATEID:
5398 case -NFS4ERR_STALE_STATEID:
5399 case -NFS4ERR_EXPIRED:
5400 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005401 if (data->roc)
5402 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005403 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005404 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005405 if (nfs4_async_handle_error(task, data->res.server,
5406 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005407 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005408 return;
5409 }
5410 }
5411 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005412}
5413
5414static void nfs4_delegreturn_release(void *calldata)
5415{
Peng Tao039b7562014-07-03 13:05:02 +08005416 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005417 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005418
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005419 if (inode) {
5420 if (data->roc)
5421 pnfs_roc_release(inode);
5422 nfs_iput_and_deactive(inode);
5423 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005424 kfree(calldata);
5425}
5426
Andy Adamson938e1012009-04-01 09:22:28 -04005427static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5428{
5429 struct nfs4_delegreturndata *d_data;
5430
5431 d_data = (struct nfs4_delegreturndata *)data;
5432
Peng Tao500d7012015-09-22 11:35:22 +08005433 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5434 return;
5435
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005436 if (d_data->roc)
5437 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005438
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005439 nfs4_setup_sequence(d_data->res.server,
5440 &d_data->args.seq_args,
5441 &d_data->res.seq_res,
5442 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005443}
Andy Adamson938e1012009-04-01 09:22:28 -04005444
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005445static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005446 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005447 .rpc_call_done = nfs4_delegreturn_done,
5448 .rpc_release = nfs4_delegreturn_release,
5449};
5450
Trond Myklebuste6f81072008-01-24 18:14:34 -05005451static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005452{
5453 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005454 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005455 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005456 struct rpc_message msg = {
5457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5458 .rpc_cred = cred,
5459 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005460 struct rpc_task_setup task_setup_data = {
5461 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005462 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005463 .callback_ops = &nfs4_delegreturn_ops,
5464 .flags = RPC_TASK_ASYNC,
5465 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005466 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005467
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005468 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005469 if (data == NULL)
5470 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005471 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005472
5473 nfs4_state_protect(server->nfs_client,
5474 NFS_SP4_MACH_CRED_CLEANUP,
5475 &task_setup_data.rpc_client, &msg);
5476
Trond Myklebustfe650402006-01-03 09:55:18 +01005477 data->args.fhandle = &data->fh;
5478 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005479 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005480 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005481 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005482 data->res.fattr = &data->fattr;
5483 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005484 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005485 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005486 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005487 data->inode = nfs_igrab_and_active(inode);
5488 if (data->inode)
5489 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005490
Trond Myklebustc970aa82007-07-14 15:39:59 -04005491 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005492 msg.rpc_argp = &data->args;
5493 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005494 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005495 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005496 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005497 if (!issync)
5498 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005499 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005500 if (status != 0)
5501 goto out;
5502 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005503 if (status == 0)
5504 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5505 else
5506 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005507out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005508 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005509 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
Trond Myklebuste6f81072008-01-24 18:14:34 -05005512int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513{
5514 struct nfs_server *server = NFS_SERVER(inode);
5515 struct nfs4_exception exception = { };
5516 int err;
5517 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005518 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005519 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 switch (err) {
5521 case -NFS4ERR_STALE_STATEID:
5522 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 case 0:
5524 return 0;
5525 }
5526 err = nfs4_handle_exception(server, err, &exception);
5527 } while (exception.retry);
5528 return err;
5529}
5530
5531#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5532#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5533
5534/*
5535 * sleep, with exponential backoff, and retry the LOCK operation.
5536 */
5537static unsigned long
5538nfs4_set_lock_task_retry(unsigned long timeout)
5539{
Colin Cross416ad3c2013-05-06 23:50:06 +00005540 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 timeout <<= 1;
5542 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5543 return NFS4_LOCK_MAXTIMEOUT;
5544 return timeout;
5545}
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5548{
5549 struct inode *inode = state->inode;
5550 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005551 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005552 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005554 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005556 struct nfs_lockt_res res = {
5557 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 };
5559 struct rpc_message msg = {
5560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5561 .rpc_argp = &arg,
5562 .rpc_resp = &res,
5563 .rpc_cred = state->owner->so_cred,
5564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 struct nfs4_lock_state *lsp;
5566 int status;
5567
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005568 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005569 status = nfs4_set_lock_state(state, request);
5570 if (status != 0)
5571 goto out;
5572 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005573 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005574 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005575 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005576 switch (status) {
5577 case 0:
5578 request->fl_type = F_UNLCK;
5579 break;
5580 case -NFS4ERR_DENIED:
5581 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005583 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005584 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005585out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return status;
5587}
5588
5589static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5590{
5591 struct nfs4_exception exception = { };
5592 int err;
5593
5594 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005595 err = _nfs4_proc_getlk(state, cmd, request);
5596 trace_nfs4_get_lock(request, state, cmd, err);
5597 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 &exception);
5599 } while (exception.retry);
5600 return err;
5601}
5602
Jeff Layton83bfff22015-07-11 06:43:03 -04005603static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604{
Benjamin Coddington4f656362015-10-22 13:38:14 -04005605 return locks_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606}
5607
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005608struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005609 struct nfs_locku_args arg;
5610 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005611 struct nfs4_lock_state *lsp;
5612 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005613 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005614 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005615 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005616};
5617
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005618static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5619 struct nfs_open_context *ctx,
5620 struct nfs4_lock_state *lsp,
5621 struct nfs_seqid *seqid)
5622{
5623 struct nfs4_unlockdata *p;
5624 struct inode *inode = lsp->ls_state->inode;
5625
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005626 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005627 if (p == NULL)
5628 return NULL;
5629 p->arg.fh = NFS_FH(inode);
5630 p->arg.fl = &p->fl;
5631 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005632 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005633 p->lsp = lsp;
5634 atomic_inc(&lsp->ls_count);
5635 /* Ensure we don't close file until we're done freeing locks! */
5636 p->ctx = get_nfs_open_context(ctx);
5637 memcpy(&p->fl, fl, sizeof(p->fl));
5638 p->server = NFS_SERVER(inode);
5639 return p;
5640}
5641
Trond Myklebust06f814a2006-01-03 09:55:07 +01005642static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005643{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005644 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005645 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005646 nfs4_put_lock_state(calldata->lsp);
5647 put_nfs_open_context(calldata->ctx);
5648 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005649}
5650
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005651static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005652{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005653 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005654
Trond Myklebust14516c32010-07-31 14:29:06 -04005655 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5656 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005657 switch (task->tk_status) {
5658 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005659 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005660 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005661 if (nfs4_update_lock_stateid(calldata->lsp,
5662 &calldata->res.stateid))
5663 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005664 case -NFS4ERR_BAD_STATEID:
5665 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005666 case -NFS4ERR_STALE_STATEID:
5667 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005668 if (!nfs4_stateid_match(&calldata->arg.stateid,
5669 &calldata->lsp->ls_stateid))
5670 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005671 break;
5672 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005673 if (nfs4_async_handle_error(task, calldata->server,
5674 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005675 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005676 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005677 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005678}
5679
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005680static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005681{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005682 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005684 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005685 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005686 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005687 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005688 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005689 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005690 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005691 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005692 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005693 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005694 &calldata->res.seq_res,
5695 task) != 0)
5696 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005697 return;
5698out_no_action:
5699 task->tk_action = NULL;
5700out_wait:
5701 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702}
5703
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005704static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005705 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005706 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005707 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005708};
5709
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005710static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5711 struct nfs_open_context *ctx,
5712 struct nfs4_lock_state *lsp,
5713 struct nfs_seqid *seqid)
5714{
5715 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005716 struct rpc_message msg = {
5717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5718 .rpc_cred = ctx->cred,
5719 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005720 struct rpc_task_setup task_setup_data = {
5721 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005722 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005723 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005724 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005725 .flags = RPC_TASK_ASYNC,
5726 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005727
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005728 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5729 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5730
Frank Filz137d6ac2007-07-09 15:32:29 -07005731 /* Ensure this is an unlock - when canceling a lock, the
5732 * canceled lock is passed in, and it won't be an unlock.
5733 */
5734 fl->fl_type = F_UNLCK;
5735
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005736 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5737 if (data == NULL) {
5738 nfs_free_seqid(seqid);
5739 return ERR_PTR(-ENOMEM);
5740 }
5741
Chuck Levera9c92d62013-08-09 12:48:18 -04005742 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005743 msg.rpc_argp = &data->arg;
5744 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005745 task_setup_data.callback_data = data;
5746 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005747}
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5750{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005751 struct inode *inode = state->inode;
5752 struct nfs4_state_owner *sp = state->owner;
5753 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005754 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005755 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005756 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005757 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005758 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005759 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
Trond Myklebust9b073572006-06-29 16:38:34 -04005761 status = nfs4_set_lock_state(state, request);
5762 /* Unlock _before_ we do the RPC call */
5763 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005764 /* Exclude nfs_delegation_claim_locks() */
5765 mutex_lock(&sp->so_delegreturn_mutex);
5766 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005767 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005768 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005769 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005770 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005771 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005772 }
5773 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005774 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005775 if (status != 0)
5776 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005777 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005778 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005779 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5780 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005781 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5782 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005783 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005784 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005785 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005786 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005787 status = PTR_ERR(task);
5788 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005789 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005790 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005791 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005792out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005793 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005794 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796}
5797
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005798struct nfs4_lockdata {
5799 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005800 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005801 struct nfs4_lock_state *lsp;
5802 struct nfs_open_context *ctx;
5803 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005804 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005805 int rpc_status;
5806 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005807 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005808};
5809
5810static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005811 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5812 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005813{
5814 struct nfs4_lockdata *p;
5815 struct inode *inode = lsp->ls_state->inode;
5816 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005817 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005818
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005819 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005820 if (p == NULL)
5821 return NULL;
5822
5823 p->arg.fh = NFS_FH(inode);
5824 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005825 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005826 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005827 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005828 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5829 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005830 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005831 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005832 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005833 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005834 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005835 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005836 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005837 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005838 atomic_inc(&lsp->ls_count);
5839 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005840 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005841 memcpy(&p->fl, fl, sizeof(p->fl));
5842 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005843out_free_seqid:
5844 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005845out_free:
5846 kfree(p);
5847 return NULL;
5848}
5849
5850static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5851{
5852 struct nfs4_lockdata *data = calldata;
5853 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Harvey Harrison3110ff82008-05-02 13:42:44 -07005855 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005856 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005857 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005858 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005859 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005860 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005861 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005862 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005863 nfs4_stateid_copy(&data->arg.open_stateid,
5864 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005865 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005866 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005867 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005868 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005869 nfs4_stateid_copy(&data->arg.lock_stateid,
5870 &data->lsp->ls_stateid);
5871 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005872 if (!nfs4_valid_open_stateid(state)) {
5873 data->rpc_status = -EBADF;
5874 task->tk_action = NULL;
5875 goto out_release_open_seqid;
5876 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005877 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005878 if (nfs4_setup_sequence(data->server,
5879 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005880 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005881 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005882 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005883out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005884 nfs_release_seqid(data->arg.open_seqid);
5885out_release_lock_seqid:
5886 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005887out_wait:
5888 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005889 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005890}
5891
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005892static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5893{
5894 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005895 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005896
Harvey Harrison3110ff82008-05-02 13:42:44 -07005897 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005898
Trond Myklebust14516c32010-07-31 14:29:06 -04005899 if (!nfs4_sequence_done(task, &data->res.seq_res))
5900 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005901
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005902 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005903 switch (task->tk_status) {
5904 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005905 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005906 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005907 if (data->arg.new_lock) {
5908 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005909 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005910 rpc_restart_call_prepare(task);
5911 break;
5912 }
5913 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005914 if (data->arg.new_lock_owner != 0) {
5915 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5916 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5917 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5918 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5919 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005920 break;
5921 case -NFS4ERR_BAD_STATEID:
5922 case -NFS4ERR_OLD_STATEID:
5923 case -NFS4ERR_STALE_STATEID:
5924 case -NFS4ERR_EXPIRED:
5925 if (data->arg.new_lock_owner != 0) {
5926 if (!nfs4_stateid_match(&data->arg.open_stateid,
5927 &lsp->ls_state->open_stateid))
5928 rpc_restart_call_prepare(task);
5929 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5930 &lsp->ls_stateid))
5931 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005932 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005933 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005934}
5935
5936static void nfs4_lock_release(void *calldata)
5937{
5938 struct nfs4_lockdata *data = calldata;
5939
Harvey Harrison3110ff82008-05-02 13:42:44 -07005940 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005941 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005942 if (data->cancelled != 0) {
5943 struct rpc_task *task;
5944 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5945 data->arg.lock_seqid);
5946 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005947 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005948 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949 } else
5950 nfs_free_seqid(data->arg.lock_seqid);
5951 nfs4_put_lock_state(data->lsp);
5952 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005953 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005954 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005955 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005956}
5957
5958static const struct rpc_call_ops nfs4_lock_ops = {
5959 .rpc_call_prepare = nfs4_lock_prepare,
5960 .rpc_call_done = nfs4_lock_done,
5961 .rpc_release = nfs4_lock_release,
5962};
5963
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005964static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5965{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005966 switch (error) {
5967 case -NFS4ERR_ADMIN_REVOKED:
5968 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005969 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005970 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005971 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005972 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005973 break;
5974 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005975 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005976 case -NFS4ERR_EXPIRED:
5977 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005978 };
5979}
5980
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005981static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005982{
5983 struct nfs4_lockdata *data;
5984 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5987 .rpc_cred = state->owner->so_cred,
5988 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005989 struct rpc_task_setup task_setup_data = {
5990 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005991 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005992 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005993 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005994 .flags = RPC_TASK_ASYNC,
5995 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996 int ret;
5997
Harvey Harrison3110ff82008-05-02 13:42:44 -07005998 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005999 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006000 fl->fl_u.nfs4_fl.owner,
6001 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006002 if (data == NULL)
6003 return -ENOMEM;
6004 if (IS_SETLKW(cmd))
6005 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006006 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006007 msg.rpc_argp = &data->arg;
6008 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006009 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006010 if (recovery_type > NFS_LOCK_NEW) {
6011 if (recovery_type == NFS_LOCK_RECLAIM)
6012 data->arg.reclaim = NFS_LOCK_RECLAIM;
6013 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006014 } else
6015 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006016 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006017 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006018 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 ret = nfs4_wait_for_completion_rpc_task(task);
6020 if (ret == 0) {
6021 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006022 if (ret)
6023 nfs4_handle_setlk_error(data->server, data->lsp,
6024 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 } else
6026 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006027 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006028 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006029 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031}
6032
6033static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6034{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006035 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006036 struct nfs4_exception exception = {
6037 .inode = state->inode,
6038 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006039 int err;
6040
6041 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006042 /* Cache the lock if possible... */
6043 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6044 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006045 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006046 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006047 break;
6048 nfs4_handle_exception(server, err, &exception);
6049 } while (exception.retry);
6050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051}
6052
6053static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6054{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006055 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006056 struct nfs4_exception exception = {
6057 .inode = state->inode,
6058 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006059 int err;
6060
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006061 err = nfs4_set_lock_state(state, request);
6062 if (err != 0)
6063 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006064 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006065 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6066 return 0;
6067 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006068 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006069 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6070 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006071 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006072 switch (err) {
6073 default:
6074 goto out;
6075 case -NFS4ERR_GRACE:
6076 case -NFS4ERR_DELAY:
6077 nfs4_handle_exception(server, err, &exception);
6078 err = 0;
6079 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006080 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006081out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006082 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083}
6084
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006085#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006086/**
6087 * nfs41_check_expired_locks - possibly free a lock stateid
6088 *
6089 * @state: NFSv4 state for an inode
6090 *
6091 * Returns NFS_OK if recovery for this stateid is now finished.
6092 * Otherwise a negative NFS4ERR value is returned.
6093 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006094static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006095{
Chuck Levereb64cf92012-07-11 16:30:05 -04006096 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006097 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006098 struct nfs_server *server = NFS_SERVER(state->inode);
6099
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006100 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006101 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006102 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6103
6104 status = nfs41_test_stateid(server,
6105 &lsp->ls_stateid,
6106 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006107 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006108 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006109 /* Free the stateid unless the server
6110 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006111 if (status != -NFS4ERR_BAD_STATEID)
6112 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006113 &lsp->ls_stateid,
6114 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006115 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006116 ret = status;
6117 }
6118 }
6119 };
6120
6121 return ret;
6122}
6123
6124static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6125{
6126 int status = NFS_OK;
6127
6128 if (test_bit(LK_STATE_IN_USE, &state->flags))
6129 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006130 if (status != NFS_OK)
6131 status = nfs4_lock_expired(state, request);
6132 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006133}
6134#endif
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6137{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006138 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006139 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006140 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006141 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006143 if ((fl_flags & FL_POSIX) &&
6144 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6145 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006146 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006147 status = nfs4_set_lock_state(state, request);
6148 if (status != 0)
6149 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006150 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006151 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006152 if (status < 0)
6153 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006154 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006155 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006156 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006157 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006158 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006159 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006160 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006161 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006162 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006163 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006164 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006165 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006166 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006167 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006168out:
6169 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 return status;
6171}
6172
6173static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6174{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006175 struct nfs4_exception exception = {
6176 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006177 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 int err;
6180
6181 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006182 err = _nfs4_proc_setlk(state, cmd, request);
6183 if (err == -NFS4ERR_DENIED)
6184 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006186 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 } while (exception.retry);
6188 return err;
6189}
6190
6191static int
6192nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6193{
6194 struct nfs_open_context *ctx;
6195 struct nfs4_state *state;
6196 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6197 int status;
6198
6199 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006200 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 state = ctx->state;
6202
6203 if (request->fl_start < 0 || request->fl_end < 0)
6204 return -EINVAL;
6205
Trond Myklebustd9531262009-07-21 19:22:38 -04006206 if (IS_GETLK(cmd)) {
6207 if (state != NULL)
6208 return nfs4_proc_getlk(state, F_GETLK, request);
6209 return 0;
6210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6213 return -EINVAL;
6214
Trond Myklebustd9531262009-07-21 19:22:38 -04006215 if (request->fl_type == F_UNLCK) {
6216 if (state != NULL)
6217 return nfs4_proc_unlck(state, cmd, request);
6218 return 0;
6219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Trond Myklebustd9531262009-07-21 19:22:38 -04006221 if (state == NULL)
6222 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006223 /*
6224 * Don't rely on the VFS having checked the file open mode,
6225 * since it won't do this for flock() locks.
6226 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006227 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006228 case F_RDLCK:
6229 if (!(filp->f_mode & FMODE_READ))
6230 return -EBADF;
6231 break;
6232 case F_WRLCK:
6233 if (!(filp->f_mode & FMODE_WRITE))
6234 return -EBADF;
6235 }
6236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 do {
6238 status = nfs4_proc_setlk(state, cmd, request);
6239 if ((status != -EAGAIN) || IS_SETLK(cmd))
6240 break;
6241 timeout = nfs4_set_lock_task_retry(timeout);
6242 status = -ERESTARTSYS;
6243 if (signalled())
6244 break;
6245 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return status;
6247}
6248
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006249int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006250{
6251 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006252 int err;
6253
6254 err = nfs4_set_lock_state(state, fl);
6255 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006256 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006257 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006258 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006259}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006260
Trond Myklebustcf470c32012-03-07 13:49:12 -05006261struct nfs_release_lockowner_data {
6262 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006263 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006264 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006265 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006266 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006267};
6268
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006269static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6270{
6271 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006272 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006273 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6274 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006275 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006276 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006277}
6278
6279static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6280{
6281 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006282 struct nfs_server *server = data->server;
6283
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006284 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006285
6286 switch (task->tk_status) {
6287 case 0:
6288 renew_lease(server, data->timestamp);
6289 break;
6290 case -NFS4ERR_STALE_CLIENTID:
6291 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006292 nfs4_schedule_lease_recovery(server->nfs_client);
6293 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006294 case -NFS4ERR_LEASE_MOVED:
6295 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006296 if (nfs4_async_handle_error(task, server,
6297 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006298 rpc_restart_call_prepare(task);
6299 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006300}
6301
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006302static void nfs4_release_lockowner_release(void *calldata)
6303{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006304 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006305 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006306 kfree(calldata);
6307}
6308
Trond Myklebust17280172012-03-11 13:11:00 -04006309static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006310 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6311 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006312 .rpc_release = nfs4_release_lockowner_release,
6313};
6314
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006315static void
6316nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006317{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006318 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006319 struct rpc_message msg = {
6320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6321 };
6322
6323 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006324 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006325
Trond Myklebustcf470c32012-03-07 13:49:12 -05006326 data = kmalloc(sizeof(*data), GFP_NOFS);
6327 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006328 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006329 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006330 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006331 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6332 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6333 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006334
Trond Myklebustcf470c32012-03-07 13:49:12 -05006335 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006336 msg.rpc_resp = &data->res;
6337 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006338 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006339}
6340
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006341#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6342
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006343static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006344 struct dentry *unused, struct inode *inode,
6345 const char *key, const void *buf,
6346 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006347{
Al Viro59301222016-05-27 10:19:30 -04006348 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006349}
6350
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006351static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006352 struct dentry *unused, struct inode *inode,
6353 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006354{
Al Virob2968212016-04-10 20:48:24 -04006355 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006356}
6357
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006358static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006359{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006360 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006361}
6362
David Quigleyc9bccef2013-05-22 12:50:45 -04006363#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006364
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006365static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006366 struct dentry *unused, struct inode *inode,
6367 const char *key, const void *buf,
6368 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006369{
6370 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006371 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006372
6373 return -EOPNOTSUPP;
6374}
6375
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006376static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006377 struct dentry *unused, struct inode *inode,
6378 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006379{
6380 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006381 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006382 return -EOPNOTSUPP;
6383}
6384
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006385static ssize_t
6386nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006387{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006388 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006389
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006390 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6391 len = security_inode_listsecurity(inode, list, list_len);
6392 if (list_len && len > list_len)
6393 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006394 }
6395 return len;
6396}
6397
6398static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6399 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006400 .get = nfs4_xattr_get_nfs4_label,
6401 .set = nfs4_xattr_set_nfs4_label,
6402};
David Quigleyc9bccef2013-05-22 12:50:45 -04006403
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006404#else
6405
6406static ssize_t
6407nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6408{
6409 return 0;
6410}
6411
6412#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006413
Andy Adamson533eb462011-06-13 18:25:56 -04006414/*
6415 * nfs_fhget will use either the mounted_on_fileid or the fileid
6416 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006417static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6418{
Andy Adamson533eb462011-06-13 18:25:56 -04006419 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6420 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6421 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006422 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006423 return;
6424
6425 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006426 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006427 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6428 fattr->nlink = 2;
6429}
6430
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006431static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6432 const struct qstr *name,
6433 struct nfs4_fs_locations *fs_locations,
6434 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006435{
6436 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006437 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006438 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006439 };
6440 struct nfs4_fs_locations_arg args = {
6441 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006442 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006443 .page = page,
6444 .bitmask = bitmask,
6445 };
Benny Halevy22958462009-04-01 09:22:02 -04006446 struct nfs4_fs_locations_res res = {
6447 .fs_locations = fs_locations,
6448 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006449 struct rpc_message msg = {
6450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6451 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006452 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006453 };
6454 int status;
6455
Harvey Harrison3110ff82008-05-02 13:42:44 -07006456 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006457
6458 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6459 * is not supported */
6460 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6461 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6462 else
6463 bitmask[0] |= FATTR4_WORD0_FILEID;
6464
Trond Myklebustc228fd32007-01-13 02:28:11 -05006465 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006466 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006467 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006468 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006469 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006470 return status;
6471}
6472
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006473int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6474 const struct qstr *name,
6475 struct nfs4_fs_locations *fs_locations,
6476 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006477{
6478 struct nfs4_exception exception = { };
6479 int err;
6480 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006481 err = _nfs4_proc_fs_locations(client, dir, name,
6482 fs_locations, page);
6483 trace_nfs4_get_fs_locations(dir, name, err);
6484 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006485 &exception);
6486 } while (exception.retry);
6487 return err;
6488}
6489
Chuck Leverb03d7352013-10-17 14:12:50 -04006490/*
6491 * This operation also signals the server that this client is
6492 * performing migration recovery. The server can stop returning
6493 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6494 * appended to this compound to identify the client ID which is
6495 * performing recovery.
6496 */
6497static int _nfs40_proc_get_locations(struct inode *inode,
6498 struct nfs4_fs_locations *locations,
6499 struct page *page, struct rpc_cred *cred)
6500{
6501 struct nfs_server *server = NFS_SERVER(inode);
6502 struct rpc_clnt *clnt = server->client;
6503 u32 bitmask[2] = {
6504 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6505 };
6506 struct nfs4_fs_locations_arg args = {
6507 .clientid = server->nfs_client->cl_clientid,
6508 .fh = NFS_FH(inode),
6509 .page = page,
6510 .bitmask = bitmask,
6511 .migration = 1, /* skip LOOKUP */
6512 .renew = 1, /* append RENEW */
6513 };
6514 struct nfs4_fs_locations_res res = {
6515 .fs_locations = locations,
6516 .migration = 1,
6517 .renew = 1,
6518 };
6519 struct rpc_message msg = {
6520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6521 .rpc_argp = &args,
6522 .rpc_resp = &res,
6523 .rpc_cred = cred,
6524 };
6525 unsigned long now = jiffies;
6526 int status;
6527
6528 nfs_fattr_init(&locations->fattr);
6529 locations->server = server;
6530 locations->nlocations = 0;
6531
6532 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6533 nfs4_set_sequence_privileged(&args.seq_args);
6534 status = nfs4_call_sync_sequence(clnt, server, &msg,
6535 &args.seq_args, &res.seq_res);
6536 if (status)
6537 return status;
6538
6539 renew_lease(server, now);
6540 return 0;
6541}
6542
6543#ifdef CONFIG_NFS_V4_1
6544
6545/*
6546 * This operation also signals the server that this client is
6547 * performing migration recovery. The server can stop asserting
6548 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6549 * performing this operation is identified in the SEQUENCE
6550 * operation in this compound.
6551 *
6552 * When the client supports GETATTR(fs_locations_info), it can
6553 * be plumbed in here.
6554 */
6555static int _nfs41_proc_get_locations(struct inode *inode,
6556 struct nfs4_fs_locations *locations,
6557 struct page *page, struct rpc_cred *cred)
6558{
6559 struct nfs_server *server = NFS_SERVER(inode);
6560 struct rpc_clnt *clnt = server->client;
6561 u32 bitmask[2] = {
6562 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6563 };
6564 struct nfs4_fs_locations_arg args = {
6565 .fh = NFS_FH(inode),
6566 .page = page,
6567 .bitmask = bitmask,
6568 .migration = 1, /* skip LOOKUP */
6569 };
6570 struct nfs4_fs_locations_res res = {
6571 .fs_locations = locations,
6572 .migration = 1,
6573 };
6574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6576 .rpc_argp = &args,
6577 .rpc_resp = &res,
6578 .rpc_cred = cred,
6579 };
6580 int status;
6581
6582 nfs_fattr_init(&locations->fattr);
6583 locations->server = server;
6584 locations->nlocations = 0;
6585
6586 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6587 nfs4_set_sequence_privileged(&args.seq_args);
6588 status = nfs4_call_sync_sequence(clnt, server, &msg,
6589 &args.seq_args, &res.seq_res);
6590 if (status == NFS4_OK &&
6591 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6592 status = -NFS4ERR_LEASE_MOVED;
6593 return status;
6594}
6595
6596#endif /* CONFIG_NFS_V4_1 */
6597
6598/**
6599 * nfs4_proc_get_locations - discover locations for a migrated FSID
6600 * @inode: inode on FSID that is migrating
6601 * @locations: result of query
6602 * @page: buffer
6603 * @cred: credential to use for this operation
6604 *
6605 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6606 * operation failed, or a negative errno if a local error occurred.
6607 *
6608 * On success, "locations" is filled in, but if the server has
6609 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6610 * asserted.
6611 *
6612 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6613 * from this client that require migration recovery.
6614 */
6615int nfs4_proc_get_locations(struct inode *inode,
6616 struct nfs4_fs_locations *locations,
6617 struct page *page, struct rpc_cred *cred)
6618{
6619 struct nfs_server *server = NFS_SERVER(inode);
6620 struct nfs_client *clp = server->nfs_client;
6621 const struct nfs4_mig_recovery_ops *ops =
6622 clp->cl_mvops->mig_recovery_ops;
6623 struct nfs4_exception exception = { };
6624 int status;
6625
6626 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6627 (unsigned long long)server->fsid.major,
6628 (unsigned long long)server->fsid.minor,
6629 clp->cl_hostname);
6630 nfs_display_fhandle(NFS_FH(inode), __func__);
6631
6632 do {
6633 status = ops->get_locations(inode, locations, page, cred);
6634 if (status != -NFS4ERR_DELAY)
6635 break;
6636 nfs4_handle_exception(server, status, &exception);
6637 } while (exception.retry);
6638 return status;
6639}
6640
Chuck Lever44c99932013-10-17 14:13:30 -04006641/*
6642 * This operation also signals the server that this client is
6643 * performing "lease moved" recovery. The server can stop
6644 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6645 * is appended to this compound to identify the client ID which is
6646 * performing recovery.
6647 */
6648static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6649{
6650 struct nfs_server *server = NFS_SERVER(inode);
6651 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6652 struct rpc_clnt *clnt = server->client;
6653 struct nfs4_fsid_present_arg args = {
6654 .fh = NFS_FH(inode),
6655 .clientid = clp->cl_clientid,
6656 .renew = 1, /* append RENEW */
6657 };
6658 struct nfs4_fsid_present_res res = {
6659 .renew = 1,
6660 };
6661 struct rpc_message msg = {
6662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6663 .rpc_argp = &args,
6664 .rpc_resp = &res,
6665 .rpc_cred = cred,
6666 };
6667 unsigned long now = jiffies;
6668 int status;
6669
6670 res.fh = nfs_alloc_fhandle();
6671 if (res.fh == NULL)
6672 return -ENOMEM;
6673
6674 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6675 nfs4_set_sequence_privileged(&args.seq_args);
6676 status = nfs4_call_sync_sequence(clnt, server, &msg,
6677 &args.seq_args, &res.seq_res);
6678 nfs_free_fhandle(res.fh);
6679 if (status)
6680 return status;
6681
6682 do_renew_lease(clp, now);
6683 return 0;
6684}
6685
6686#ifdef CONFIG_NFS_V4_1
6687
6688/*
6689 * This operation also signals the server that this client is
6690 * performing "lease moved" recovery. The server can stop asserting
6691 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6692 * this operation is identified in the SEQUENCE operation in this
6693 * compound.
6694 */
6695static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6696{
6697 struct nfs_server *server = NFS_SERVER(inode);
6698 struct rpc_clnt *clnt = server->client;
6699 struct nfs4_fsid_present_arg args = {
6700 .fh = NFS_FH(inode),
6701 };
6702 struct nfs4_fsid_present_res res = {
6703 };
6704 struct rpc_message msg = {
6705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6706 .rpc_argp = &args,
6707 .rpc_resp = &res,
6708 .rpc_cred = cred,
6709 };
6710 int status;
6711
6712 res.fh = nfs_alloc_fhandle();
6713 if (res.fh == NULL)
6714 return -ENOMEM;
6715
6716 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6717 nfs4_set_sequence_privileged(&args.seq_args);
6718 status = nfs4_call_sync_sequence(clnt, server, &msg,
6719 &args.seq_args, &res.seq_res);
6720 nfs_free_fhandle(res.fh);
6721 if (status == NFS4_OK &&
6722 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6723 status = -NFS4ERR_LEASE_MOVED;
6724 return status;
6725}
6726
6727#endif /* CONFIG_NFS_V4_1 */
6728
6729/**
6730 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6731 * @inode: inode on FSID to check
6732 * @cred: credential to use for this operation
6733 *
6734 * Server indicates whether the FSID is present, moved, or not
6735 * recognized. This operation is necessary to clear a LEASE_MOVED
6736 * condition for this client ID.
6737 *
6738 * Returns NFS4_OK if the FSID is present on this server,
6739 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6740 * NFS4ERR code if some error occurred on the server, or a
6741 * negative errno if a local failure occurred.
6742 */
6743int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6744{
6745 struct nfs_server *server = NFS_SERVER(inode);
6746 struct nfs_client *clp = server->nfs_client;
6747 const struct nfs4_mig_recovery_ops *ops =
6748 clp->cl_mvops->mig_recovery_ops;
6749 struct nfs4_exception exception = { };
6750 int status;
6751
6752 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6753 (unsigned long long)server->fsid.major,
6754 (unsigned long long)server->fsid.minor,
6755 clp->cl_hostname);
6756 nfs_display_fhandle(NFS_FH(inode), __func__);
6757
6758 do {
6759 status = ops->fsid_present(inode, cred);
6760 if (status != -NFS4ERR_DELAY)
6761 break;
6762 nfs4_handle_exception(server, status, &exception);
6763 } while (exception.retry);
6764 return status;
6765}
6766
Andy Adamson5ec16a82013-08-08 10:57:55 -04006767/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006768 * If 'use_integrity' is true and the state managment nfs_client
6769 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6770 * and the machine credential as per RFC3530bis and RFC5661 Security
6771 * Considerations sections. Otherwise, just use the user cred with the
6772 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006773 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006774static 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 +00006775{
6776 int status;
6777 struct nfs4_secinfo_arg args = {
6778 .dir_fh = NFS_FH(dir),
6779 .name = name,
6780 };
6781 struct nfs4_secinfo_res res = {
6782 .flavors = flavors,
6783 };
6784 struct rpc_message msg = {
6785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6786 .rpc_argp = &args,
6787 .rpc_resp = &res,
6788 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006789 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006790 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006791
6792 if (use_integrity) {
6793 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006794 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6795 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006796 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006797
6798 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006799
6800 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6801 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6802
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006803 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6804 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006805 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006806
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006807 if (cred)
6808 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006809
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006810 return status;
6811}
6812
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006813int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6814 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006815{
6816 struct nfs4_exception exception = { };
6817 int err;
6818 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006819 err = -NFS4ERR_WRONGSEC;
6820
6821 /* try to use integrity protection with machine cred */
6822 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6823 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6824
6825 /*
6826 * if unable to use integrity protection, or SECINFO with
6827 * integrity protection returns NFS4ERR_WRONGSEC (which is
6828 * disallowed by spec, but exists in deployed servers) use
6829 * the current filesystem's rpc_client and the user cred.
6830 */
6831 if (err == -NFS4ERR_WRONGSEC)
6832 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6833
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006834 trace_nfs4_secinfo(dir, name, err);
6835 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006836 &exception);
6837 } while (exception.retry);
6838 return err;
6839}
6840
Andy Adamson557134a2009-04-01 09:21:53 -04006841#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006842/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006843 * Check the exchange flags returned by the server for invalid flags, having
6844 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6845 * DS flags set.
6846 */
6847static int nfs4_check_cl_exchange_flags(u32 flags)
6848{
6849 if (flags & ~EXCHGID4_FLAG_MASK_R)
6850 goto out_inval;
6851 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6852 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6853 goto out_inval;
6854 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6855 goto out_inval;
6856 return NFS_OK;
6857out_inval:
6858 return -NFS4ERR_INVAL;
6859}
6860
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006861static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006862nfs41_same_server_scope(struct nfs41_server_scope *a,
6863 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006864{
6865 if (a->server_scope_sz == b->server_scope_sz &&
6866 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6867 return true;
6868
6869 return false;
6870}
6871
Andy Adamson02a95de2016-02-05 16:08:37 -05006872static void
6873nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6874{
6875}
6876
6877static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6878 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6879};
6880
Andy Adamson357f54d2010-12-14 10:11:57 -05006881/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006882 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006883 *
6884 * The 4.1 client currently uses the same TCP connection for the
6885 * fore and backchannel.
6886 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006887static
6888int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6889 struct rpc_xprt *xprt,
6890 struct nfs_client *clp,
6891 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006892{
6893 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006894 struct nfs41_bind_conn_to_session_args args = {
6895 .client = clp,
6896 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6897 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006898 struct nfs41_bind_conn_to_session_res res;
6899 struct rpc_message msg = {
6900 .rpc_proc =
6901 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006902 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006903 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006904 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006905 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006906 struct rpc_task_setup task_setup_data = {
6907 .rpc_client = clnt,
6908 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006909 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006910 .rpc_message = &msg,
6911 .flags = RPC_TASK_TIMEOUT,
6912 };
6913 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006914
6915 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006916
Trond Myklebust71a097c2015-02-18 09:27:18 -08006917 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6918 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6919 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006920
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006921 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6922 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6923 args.dir = NFS4_CDFC4_FORE;
6924
6925 task = rpc_run_task(&task_setup_data);
6926 if (!IS_ERR(task)) {
6927 status = task->tk_status;
6928 rpc_put_task(task);
6929 } else
6930 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006931 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006932 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006933 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006934 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6935 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6936 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006937 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006938 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006939 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006940 dprintk("NFS: %s: Unexpected direction from server\n",
6941 __func__);
6942 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006943 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006944 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006945 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006946 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6947 __func__);
6948 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006949 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006950 }
6951 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006952out:
6953 dprintk("<-- %s status= %d\n", __func__, status);
6954 return status;
6955}
6956
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006957struct rpc_bind_conn_calldata {
6958 struct nfs_client *clp;
6959 struct rpc_cred *cred;
6960};
6961
6962static int
6963nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6964 struct rpc_xprt *xprt,
6965 void *calldata)
6966{
6967 struct rpc_bind_conn_calldata *p = calldata;
6968
6969 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6970}
6971
6972int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6973{
6974 struct rpc_bind_conn_calldata data = {
6975 .clp = clp,
6976 .cred = cred,
6977 };
6978 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6979 nfs4_proc_bind_conn_to_session_callback, &data);
6980}
6981
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006982/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006983 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6984 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006985 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006986static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6987 .how = SP4_MACH_CRED,
6988 .enforce.u.words = {
6989 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6990 1 << (OP_EXCHANGE_ID - 32) |
6991 1 << (OP_CREATE_SESSION - 32) |
6992 1 << (OP_DESTROY_SESSION - 32) |
6993 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006994 },
6995 .allow.u.words = {
6996 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006997 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006998 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006999 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007000 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007001 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007002 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007003 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007004 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007005 1 << (OP_FREE_STATEID - 32) |
7006 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007007 }
7008};
7009
7010/*
7011 * Select the state protection mode for client `clp' given the server results
7012 * from exchange_id in `sp'.
7013 *
7014 * Returns 0 on success, negative errno otherwise.
7015 */
7016static int nfs4_sp4_select_mode(struct nfs_client *clp,
7017 struct nfs41_state_protection *sp)
7018{
7019 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7020 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7021 1 << (OP_EXCHANGE_ID - 32) |
7022 1 << (OP_CREATE_SESSION - 32) |
7023 1 << (OP_DESTROY_SESSION - 32) |
7024 1 << (OP_DESTROY_CLIENTID - 32)
7025 };
7026 unsigned int i;
7027
7028 if (sp->how == SP4_MACH_CRED) {
7029 /* Print state protect result */
7030 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7031 for (i = 0; i <= LAST_NFS4_OP; i++) {
7032 if (test_bit(i, sp->enforce.u.longs))
7033 dfprintk(MOUNT, " enforce op %d\n", i);
7034 if (test_bit(i, sp->allow.u.longs))
7035 dfprintk(MOUNT, " allow op %d\n", i);
7036 }
7037
7038 /* make sure nothing is on enforce list that isn't supported */
7039 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7040 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7041 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7042 return -EINVAL;
7043 }
7044 }
7045
7046 /*
7047 * Minimal mode - state operations are allowed to use machine
7048 * credential. Note this already happens by default, so the
7049 * client doesn't have to do anything more than the negotiation.
7050 *
7051 * NOTE: we don't care if EXCHANGE_ID is in the list -
7052 * we're already using the machine cred for exchange_id
7053 * and will never use a different cred.
7054 */
7055 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7056 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7057 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7058 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7059 dfprintk(MOUNT, "sp4_mach_cred:\n");
7060 dfprintk(MOUNT, " minimal mode enabled\n");
7061 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7062 } else {
7063 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7064 return -EINVAL;
7065 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007066
7067 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007068 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7069 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007070 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7071 dfprintk(MOUNT, " cleanup mode enabled\n");
7072 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7073 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007074
Andrew Elble99ade3c2015-12-02 09:39:51 -05007075 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7076 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7077 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7078 &clp->cl_sp4_flags);
7079 }
7080
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007081 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7082 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7083 dfprintk(MOUNT, " secinfo mode enabled\n");
7084 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7085 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007086
7087 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7088 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7089 dfprintk(MOUNT, " stateid mode enabled\n");
7090 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7091 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007092
7093 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7094 dfprintk(MOUNT, " write mode enabled\n");
7095 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7096 }
7097
7098 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7099 dfprintk(MOUNT, " commit mode enabled\n");
7100 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7101 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007102 }
7103
7104 return 0;
7105}
7106
7107/*
7108 * _nfs4_proc_exchange_id()
7109 *
7110 * Wrapper for EXCHANGE_ID operation.
7111 */
7112static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7113 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007114{
7115 nfs4_verifier verifier;
7116 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007117 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007118 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007119#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007120 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007121 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7122 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7123#else
7124 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7125 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7126#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007127 };
7128 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007129 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007130 };
7131 int status;
7132 struct rpc_message msg = {
7133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7134 .rpc_argp = &args,
7135 .rpc_resp = &res,
7136 .rpc_cred = cred,
7137 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007138
Chuck Leverf0920752012-05-21 22:45:41 -04007139 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007140
7141 status = nfs4_init_uniform_client_string(clp);
7142 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007143 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007144
7145 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007146 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007147 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007148
Chuck Leveracdeb692012-05-21 22:46:16 -04007149 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007150 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007151 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007152 status = -ENOMEM;
7153 goto out;
7154 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007155
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007156 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007157 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007158 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007159 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007160 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007161 }
7162
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007163 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007164 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007165 status = -ENOMEM;
7166 goto out_server_scope;
7167 }
7168
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007169 switch (sp4_how) {
7170 case SP4_NONE:
7171 args.state_protect.how = SP4_NONE;
7172 break;
7173
7174 case SP4_MACH_CRED:
7175 args.state_protect = nfs4_sp4_mach_cred_request;
7176 break;
7177
7178 default:
7179 /* unsupported! */
7180 WARN_ON_ONCE(1);
7181 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007182 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007183 }
7184
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007185 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007186 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007187 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007188 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007189
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007190 if (status == 0)
7191 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7192
Chuck Lever177313f2012-05-21 22:44:58 -04007193 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007194 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007195 clp->cl_exchange_flags = res.flags;
7196 /* Client ID is not confirmed */
7197 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7198 clear_bit(NFS4_SESSION_ESTABLISHED,
7199 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007200 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007201 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007202
Chuck Leveracdeb692012-05-21 22:46:16 -04007203 kfree(clp->cl_serverowner);
7204 clp->cl_serverowner = res.server_owner;
7205 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007206
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007207 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007208 kfree(clp->cl_implid);
7209 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007210 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007211
Chuck Lever177313f2012-05-21 22:44:58 -04007212 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007213 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007214 res.server_scope)) {
7215 dprintk("%s: server_scope mismatch detected\n",
7216 __func__);
7217 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007218 kfree(clp->cl_serverscope);
7219 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007220 }
7221
Chuck Lever177313f2012-05-21 22:44:58 -04007222 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007223 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007224 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007225 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007226 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007227
Kinglong Mee6b559702015-07-01 11:54:53 +08007228out_impl_id:
7229 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007230out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007231 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007232out_server_owner:
7233 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007234out:
Chuck Lever177313f2012-05-21 22:44:58 -04007235 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007236 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007237 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007238 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007239 clp->cl_implid->date.seconds,
7240 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007241 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007242 return status;
7243}
7244
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007245/*
7246 * nfs4_proc_exchange_id()
7247 *
7248 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7249 *
7250 * Since the clientid has expired, all compounds using sessions
7251 * associated with the stale clientid will be returning
7252 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7253 * be in some phase of session reset.
7254 *
7255 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7256 */
7257int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7258{
7259 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7260 int status;
7261
7262 /* try SP4_MACH_CRED if krb5i/p */
7263 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7264 authflavor == RPC_AUTH_GSS_KRB5P) {
7265 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7266 if (!status)
7267 return 0;
7268 }
7269
7270 /* try SP4_NONE */
7271 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7272}
7273
Trond Myklebust66245532012-05-25 17:18:09 -04007274static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7275 struct rpc_cred *cred)
7276{
7277 struct rpc_message msg = {
7278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7279 .rpc_argp = clp,
7280 .rpc_cred = cred,
7281 };
7282 int status;
7283
7284 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007285 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007286 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007287 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007288 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7289 return status;
7290}
7291
7292static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7293 struct rpc_cred *cred)
7294{
7295 unsigned int loop;
7296 int ret;
7297
7298 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7299 ret = _nfs4_proc_destroy_clientid(clp, cred);
7300 switch (ret) {
7301 case -NFS4ERR_DELAY:
7302 case -NFS4ERR_CLIENTID_BUSY:
7303 ssleep(1);
7304 break;
7305 default:
7306 return ret;
7307 }
7308 }
7309 return 0;
7310}
7311
7312int nfs4_destroy_clientid(struct nfs_client *clp)
7313{
7314 struct rpc_cred *cred;
7315 int ret = 0;
7316
7317 if (clp->cl_mvops->minor_version < 1)
7318 goto out;
7319 if (clp->cl_exchange_flags == 0)
7320 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007321 if (clp->cl_preserve_clid)
7322 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007323 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007324 ret = nfs4_proc_destroy_clientid(clp, cred);
7325 if (cred)
7326 put_rpccred(cred);
7327 switch (ret) {
7328 case 0:
7329 case -NFS4ERR_STALE_CLIENTID:
7330 clp->cl_exchange_flags = 0;
7331 }
7332out:
7333 return ret;
7334}
7335
Andy Adamson2050f0c2009-04-01 09:22:30 -04007336struct nfs4_get_lease_time_data {
7337 struct nfs4_get_lease_time_args *args;
7338 struct nfs4_get_lease_time_res *res;
7339 struct nfs_client *clp;
7340};
7341
7342static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7343 void *calldata)
7344{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007345 struct nfs4_get_lease_time_data *data =
7346 (struct nfs4_get_lease_time_data *)calldata;
7347
7348 dprintk("--> %s\n", __func__);
7349 /* just setup sequence, do not trigger session recovery
7350 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007351 nfs41_setup_sequence(data->clp->cl_session,
7352 &data->args->la_seq_args,
7353 &data->res->lr_seq_res,
7354 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007355 dprintk("<-- %s\n", __func__);
7356}
7357
7358/*
7359 * Called from nfs4_state_manager thread for session setup, so don't recover
7360 * from sequence operation or clientid errors.
7361 */
7362static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7363{
7364 struct nfs4_get_lease_time_data *data =
7365 (struct nfs4_get_lease_time_data *)calldata;
7366
7367 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007368 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7369 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007370 switch (task->tk_status) {
7371 case -NFS4ERR_DELAY:
7372 case -NFS4ERR_GRACE:
7373 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7374 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7375 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007376 /* fall through */
7377 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007378 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007379 return;
7380 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007381 dprintk("<-- %s\n", __func__);
7382}
7383
Trond Myklebust17280172012-03-11 13:11:00 -04007384static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007385 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7386 .rpc_call_done = nfs4_get_lease_time_done,
7387};
7388
7389int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7390{
7391 struct rpc_task *task;
7392 struct nfs4_get_lease_time_args args;
7393 struct nfs4_get_lease_time_res res = {
7394 .lr_fsinfo = fsinfo,
7395 };
7396 struct nfs4_get_lease_time_data data = {
7397 .args = &args,
7398 .res = &res,
7399 .clp = clp,
7400 };
7401 struct rpc_message msg = {
7402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7403 .rpc_argp = &args,
7404 .rpc_resp = &res,
7405 };
7406 struct rpc_task_setup task_setup = {
7407 .rpc_client = clp->cl_rpcclient,
7408 .rpc_message = &msg,
7409 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007410 .callback_data = &data,
7411 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007412 };
7413 int status;
7414
Chuck Levera9c92d62013-08-09 12:48:18 -04007415 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007416 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007417 dprintk("--> %s\n", __func__);
7418 task = rpc_run_task(&task_setup);
7419
7420 if (IS_ERR(task))
7421 status = PTR_ERR(task);
7422 else {
7423 status = task->tk_status;
7424 rpc_put_task(task);
7425 }
7426 dprintk("<-- %s return %d\n", __func__, status);
7427
7428 return status;
7429}
7430
Andy Adamsonfc931582009-04-01 09:22:31 -04007431/*
7432 * Initialize the values to be used by the client in CREATE_SESSION
7433 * If nfs4_init_session set the fore channel request and response sizes,
7434 * use them.
7435 *
7436 * Set the back channel max_resp_sz_cached to zero to force the client to
7437 * always set csa_cachethis to FALSE because the current implementation
7438 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7439 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007440static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7441 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007442{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007443 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007444 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007445
Andy Adamson18aad3d2013-06-26 12:21:49 -04007446 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7447 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7448
Andy Adamsonfc931582009-04-01 09:22:31 -04007449 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007450 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7451 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007452 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007453 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007454
7455 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007456 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007457 __func__,
7458 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007459 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007460
7461 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007462 args->bc_attrs.max_rqst_sz = max_bc_payload;
7463 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007464 args->bc_attrs.max_resp_sz_cached = 0;
7465 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007466 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007467
7468 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7469 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7470 __func__,
7471 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7472 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7473 args->bc_attrs.max_reqs);
7474}
7475
Trond Myklebust79969dd2015-02-18 11:30:18 -08007476static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7477 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007478{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007479 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007480 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007481
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007482 if (rcvd->max_resp_sz > sent->max_resp_sz)
7483 return -EINVAL;
7484 /*
7485 * Our requested max_ops is the minimum we need; we're not
7486 * prepared to break up compounds into smaller pieces than that.
7487 * So, no point even trying to continue if the server won't
7488 * cooperate:
7489 */
7490 if (rcvd->max_ops < sent->max_ops)
7491 return -EINVAL;
7492 if (rcvd->max_reqs == 0)
7493 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007494 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7495 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007496 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007497}
7498
Trond Myklebust79969dd2015-02-18 11:30:18 -08007499static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7500 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007501{
7502 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007503 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007504
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007505 if (!(res->flags & SESSION4_BACK_CHAN))
7506 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007507 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7508 return -EINVAL;
7509 if (rcvd->max_resp_sz < sent->max_resp_sz)
7510 return -EINVAL;
7511 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7512 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007513 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007514 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007515 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007516 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007517out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007518 return 0;
7519}
Andy Adamson8d353012009-04-01 09:22:32 -04007520
Andy Adamson8d353012009-04-01 09:22:32 -04007521static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007522 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007523{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007524 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007525
Trond Myklebust79969dd2015-02-18 11:30:18 -08007526 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007527 if (ret)
7528 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007529 return nfs4_verify_back_channel_attrs(args, res);
7530}
7531
7532static void nfs4_update_session(struct nfs4_session *session,
7533 struct nfs41_create_session_res *res)
7534{
7535 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007536 /* Mark client id and session as being confirmed */
7537 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7538 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007539 session->flags = res->flags;
7540 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007541 if (res->flags & SESSION4_BACK_CHAN)
7542 memcpy(&session->bc_attrs, &res->bc_attrs,
7543 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007544}
7545
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007546static int _nfs4_proc_create_session(struct nfs_client *clp,
7547 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007548{
7549 struct nfs4_session *session = clp->cl_session;
7550 struct nfs41_create_session_args args = {
7551 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007552 .clientid = clp->cl_clientid,
7553 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007554 .cb_program = NFS4_CALLBACK,
7555 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007556 struct nfs41_create_session_res res;
7557
Andy Adamsonfc931582009-04-01 09:22:31 -04007558 struct rpc_message msg = {
7559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7560 .rpc_argp = &args,
7561 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007562 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007563 };
7564 int status;
7565
Chuck Lever6b26cc82016-05-02 14:40:40 -04007566 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007567 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007568
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007569 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007570 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007571
Trond Myklebustb519d402016-09-11 14:50:01 -04007572 switch (status) {
7573 case -NFS4ERR_STALE_CLIENTID:
7574 case -NFS4ERR_DELAY:
7575 case -ETIMEDOUT:
7576 case -EACCES:
7577 case -EAGAIN:
7578 goto out;
7579 };
7580
7581 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007582 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007583 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007584 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007585 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007586 if (status)
7587 goto out;
7588 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007589 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007590out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007591 return status;
7592}
7593
7594/*
7595 * Issues a CREATE_SESSION operation to the server.
7596 * It is the responsibility of the caller to verify the session is
7597 * expired before calling this routine.
7598 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007599int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007600{
7601 int status;
7602 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007603 struct nfs4_session *session = clp->cl_session;
7604
7605 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7606
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007607 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007608 if (status)
7609 goto out;
7610
Andy Adamsonaacd5532011-11-09 13:58:21 -05007611 /* Init or reset the session slot tables */
7612 status = nfs4_setup_session_slot_tables(session);
7613 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007614 if (status)
7615 goto out;
7616
7617 ptr = (unsigned *)&session->sess_id.data[0];
7618 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7619 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007620out:
7621 dprintk("<-- %s\n", __func__);
7622 return status;
7623}
7624
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007625/*
7626 * Issue the over-the-wire RPC DESTROY_SESSION.
7627 * The caller must serialize access to this routine.
7628 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007629int nfs4_proc_destroy_session(struct nfs4_session *session,
7630 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007631{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007632 struct rpc_message msg = {
7633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7634 .rpc_argp = session,
7635 .rpc_cred = cred,
7636 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007637 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007638
7639 dprintk("--> nfs4_proc_destroy_session\n");
7640
7641 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007642 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7643 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007644
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007645 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007646 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007647
7648 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007649 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007650 "Session has been destroyed regardless...\n", status);
7651
7652 dprintk("<-- nfs4_proc_destroy_session\n");
7653 return status;
7654}
7655
Trond Myklebust7b38c362012-05-23 13:23:31 -04007656/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007657 * Renew the cl_session lease.
7658 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007659struct nfs4_sequence_data {
7660 struct nfs_client *clp;
7661 struct nfs4_sequence_args args;
7662 struct nfs4_sequence_res res;
7663};
7664
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007665static void nfs41_sequence_release(void *data)
7666{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007667 struct nfs4_sequence_data *calldata = data;
7668 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007669
Alexandros Batsakis71358402010-02-05 03:45:05 -08007670 if (atomic_read(&clp->cl_count) > 1)
7671 nfs4_schedule_state_renewal(clp);
7672 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007673 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007674}
7675
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007676static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7677{
7678 switch(task->tk_status) {
7679 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007680 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7681 return -EAGAIN;
7682 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007683 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007684 }
7685 return 0;
7686}
7687
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007688static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007689{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007690 struct nfs4_sequence_data *calldata = data;
7691 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007692
Trond Myklebust14516c32010-07-31 14:29:06 -04007693 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7694 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007695
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007696 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007697 if (task->tk_status < 0) {
7698 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007699 if (atomic_read(&clp->cl_count) == 1)
7700 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007701
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007702 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7703 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007704 return;
7705 }
7706 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007707 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007708out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007709 dprintk("<-- %s\n", __func__);
7710}
7711
7712static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7713{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007714 struct nfs4_sequence_data *calldata = data;
7715 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007716 struct nfs4_sequence_args *args;
7717 struct nfs4_sequence_res *res;
7718
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007719 args = task->tk_msg.rpc_argp;
7720 res = task->tk_msg.rpc_resp;
7721
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007722 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007723}
7724
7725static const struct rpc_call_ops nfs41_sequence_ops = {
7726 .rpc_call_done = nfs41_sequence_call_done,
7727 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007728 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007729};
7730
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007731static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7732 struct rpc_cred *cred,
7733 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007734{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007735 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007736 struct rpc_message msg = {
7737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7738 .rpc_cred = cred,
7739 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007740 struct rpc_task_setup task_setup_data = {
7741 .rpc_client = clp->cl_rpcclient,
7742 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007743 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007744 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007745 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007746
Alexandros Batsakis71358402010-02-05 03:45:05 -08007747 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007748 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007749 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007750 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007751 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007752 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007753 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007754 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007755 if (is_privileged)
7756 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007757 msg.rpc_argp = &calldata->args;
7758 msg.rpc_resp = &calldata->res;
7759 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007760 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007761
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007762 return rpc_run_task(&task_setup_data);
7763}
7764
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007765static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007766{
7767 struct rpc_task *task;
7768 int ret = 0;
7769
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007770 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007771 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007772 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007773 if (IS_ERR(task))
7774 ret = PTR_ERR(task);
7775 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007776 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007777 dprintk("<-- %s status=%d\n", __func__, ret);
7778 return ret;
7779}
7780
7781static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7782{
7783 struct rpc_task *task;
7784 int ret;
7785
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007786 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007787 if (IS_ERR(task)) {
7788 ret = PTR_ERR(task);
7789 goto out;
7790 }
7791 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007792 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007793 ret = task->tk_status;
7794 rpc_put_task(task);
7795out:
7796 dprintk("<-- %s status=%d\n", __func__, ret);
7797 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007798}
7799
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007800struct nfs4_reclaim_complete_data {
7801 struct nfs_client *clp;
7802 struct nfs41_reclaim_complete_args arg;
7803 struct nfs41_reclaim_complete_res res;
7804};
7805
7806static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7807{
7808 struct nfs4_reclaim_complete_data *calldata = data;
7809
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007810 nfs41_setup_sequence(calldata->clp->cl_session,
7811 &calldata->arg.seq_args,
7812 &calldata->res.seq_res,
7813 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007814}
7815
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007816static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7817{
7818 switch(task->tk_status) {
7819 case 0:
7820 case -NFS4ERR_COMPLETE_ALREADY:
7821 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7822 break;
7823 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007824 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007825 /* fall through */
7826 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007827 return -EAGAIN;
7828 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007829 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007830 }
7831 return 0;
7832}
7833
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007834static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7835{
7836 struct nfs4_reclaim_complete_data *calldata = data;
7837 struct nfs_client *clp = calldata->clp;
7838 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7839
7840 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007841 if (!nfs41_sequence_done(task, res))
7842 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007843
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007844 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007845 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7846 rpc_restart_call_prepare(task);
7847 return;
7848 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007849 dprintk("<-- %s\n", __func__);
7850}
7851
7852static void nfs4_free_reclaim_complete_data(void *data)
7853{
7854 struct nfs4_reclaim_complete_data *calldata = data;
7855
7856 kfree(calldata);
7857}
7858
7859static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7860 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7861 .rpc_call_done = nfs4_reclaim_complete_done,
7862 .rpc_release = nfs4_free_reclaim_complete_data,
7863};
7864
7865/*
7866 * Issue a global reclaim complete.
7867 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007868static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7869 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007870{
7871 struct nfs4_reclaim_complete_data *calldata;
7872 struct rpc_task *task;
7873 struct rpc_message msg = {
7874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007875 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007876 };
7877 struct rpc_task_setup task_setup_data = {
7878 .rpc_client = clp->cl_rpcclient,
7879 .rpc_message = &msg,
7880 .callback_ops = &nfs4_reclaim_complete_call_ops,
7881 .flags = RPC_TASK_ASYNC,
7882 };
7883 int status = -ENOMEM;
7884
7885 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007886 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007887 if (calldata == NULL)
7888 goto out;
7889 calldata->clp = clp;
7890 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007891
Chuck Levera9c92d62013-08-09 12:48:18 -04007892 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007893 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007894 msg.rpc_argp = &calldata->arg;
7895 msg.rpc_resp = &calldata->res;
7896 task_setup_data.callback_data = calldata;
7897 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007898 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007899 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007900 goto out;
7901 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007902 status = nfs4_wait_for_completion_rpc_task(task);
7903 if (status == 0)
7904 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007905 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007906 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007907out:
7908 dprintk("<-- %s status=%d\n", __func__, status);
7909 return status;
7910}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007911
7912static void
7913nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7914{
7915 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007916 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007917 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007918
7919 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007920 nfs41_setup_sequence(session, &lgp->args.seq_args,
7921 &lgp->res.seq_res, task);
7922 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007923}
7924
7925static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7926{
7927 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04007928
7929 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04007930 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04007931 dprintk("<-- %s\n", __func__);
7932}
7933
7934static int
7935nfs4_layoutget_handle_exception(struct rpc_task *task,
7936 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
7937{
Trond Myklebustee314c22012-10-01 17:25:48 -07007938 struct inode *inode = lgp->args.inode;
7939 struct nfs_server *server = NFS_SERVER(inode);
7940 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007941 int nfs4err = task->tk_status;
7942 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007943 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007944
Boaz Harroshed7e5422014-01-22 20:34:54 +02007945 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007946
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007947 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007948 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007949 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007950
7951 /*
7952 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7953 * on the file. set tk_status to -ENODATA to tell upper layer to
7954 * retry go inband.
7955 */
7956 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04007957 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007958 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007959 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007960 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7961 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7962 */
7963 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007964 status = -EOVERFLOW;
7965 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007966 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007967 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007968 * (or clients) writing to the same RAID stripe except when
7969 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04007970 *
7971 * Treat it like we would RECALLCONFLICT -- we retry for a little
7972 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02007973 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007974 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04007975 if (lgp->args.minlength == 0) {
7976 status = -EOVERFLOW;
7977 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007978 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007979 status = -EBUSY;
7980 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04007981 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007982 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007983 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07007984 case -NFS4ERR_EXPIRED:
7985 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04007986 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07007987 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007988 lo = NFS_I(inode)->layout;
7989 /* If the open stateid was bad, then recover it. */
7990 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
7991 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04007992 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007993 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04007994 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007995 break;
7996 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007997
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007998 /*
7999 * Mark the bad layout state as invalid, then retry
8000 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008001 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008002 spin_unlock(&inode->i_lock);
8003 pnfs_free_lseg_list(&head);
8004 status = -EAGAIN;
8005 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008006 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008007
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008008 err = nfs4_handle_exception(server, nfs4err, exception);
8009 if (!status) {
8010 if (exception->retry)
8011 status = -EAGAIN;
8012 else
8013 status = err;
8014 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008015out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008016 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008017 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008018}
8019
Idan Kedar85541162012-08-02 11:47:10 +03008020static size_t max_response_pages(struct nfs_server *server)
8021{
8022 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8023 return nfs_page_array_len(0, max_resp_sz);
8024}
8025
8026static void nfs4_free_pages(struct page **pages, size_t size)
8027{
8028 int i;
8029
8030 if (!pages)
8031 return;
8032
8033 for (i = 0; i < size; i++) {
8034 if (!pages[i])
8035 break;
8036 __free_page(pages[i]);
8037 }
8038 kfree(pages);
8039}
8040
8041static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8042{
8043 struct page **pages;
8044 int i;
8045
8046 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8047 if (!pages) {
8048 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8049 return NULL;
8050 }
8051
8052 for (i = 0; i < size; i++) {
8053 pages[i] = alloc_page(gfp_flags);
8054 if (!pages[i]) {
8055 dprintk("%s: failed to allocate page\n", __func__);
8056 nfs4_free_pages(pages, size);
8057 return NULL;
8058 }
8059 }
8060
8061 return pages;
8062}
8063
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008064static void nfs4_layoutget_release(void *calldata)
8065{
8066 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008067 struct inode *inode = lgp->args.inode;
8068 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008069 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008070
8071 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008072 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008073 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008074 put_nfs_open_context(lgp->args.ctx);
8075 kfree(calldata);
8076 dprintk("<-- %s\n", __func__);
8077}
8078
8079static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8080 .rpc_call_prepare = nfs4_layoutget_prepare,
8081 .rpc_call_done = nfs4_layoutget_done,
8082 .rpc_release = nfs4_layoutget_release,
8083};
8084
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008085struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008086nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008087{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008088 struct inode *inode = lgp->args.inode;
8089 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008090 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008091 struct rpc_task *task;
8092 struct rpc_message msg = {
8093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8094 .rpc_argp = &lgp->args,
8095 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008096 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008097 };
8098 struct rpc_task_setup task_setup_data = {
8099 .rpc_client = server->client,
8100 .rpc_message = &msg,
8101 .callback_ops = &nfs4_layoutget_call_ops,
8102 .callback_data = lgp,
8103 .flags = RPC_TASK_ASYNC,
8104 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008105 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008106 struct nfs4_exception exception = {
8107 .inode = inode,
8108 .timeout = *timeout,
8109 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008110 int status = 0;
8111
8112 dprintk("--> %s\n", __func__);
8113
Peng Tao4bd5a982014-11-17 11:05:17 +08008114 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8115 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8116
Idan Kedar85541162012-08-02 11:47:10 +03008117 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8118 if (!lgp->args.layout.pages) {
8119 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008120 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008121 }
8122 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8123
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008124 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008125 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008126 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008127
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008128 task = rpc_run_task(&task_setup_data);
8129 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008130 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008131 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008132 if (status == 0) {
8133 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8134 *timeout = exception.timeout;
8135 }
8136
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008137 trace_nfs4_layoutget(lgp->args.ctx,
8138 &lgp->args.range,
8139 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008140 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008141 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008142
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008143 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8144 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008145 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008146 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008147 rpc_put_task(task);
8148 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008149 if (status)
8150 return ERR_PTR(status);
8151 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008152}
8153
Benny Halevycbe82602011-05-22 19:52:37 +03008154static void
8155nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8156{
8157 struct nfs4_layoutreturn *lrp = calldata;
8158
8159 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008160 nfs41_setup_sequence(lrp->clp->cl_session,
8161 &lrp->args.seq_args,
8162 &lrp->res.seq_res,
8163 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008164}
8165
8166static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8167{
8168 struct nfs4_layoutreturn *lrp = calldata;
8169 struct nfs_server *server;
8170
8171 dprintk("--> %s\n", __func__);
8172
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008173 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008174 return;
8175
8176 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008177 switch (task->tk_status) {
8178 default:
8179 task->tk_status = 0;
8180 case 0:
8181 break;
8182 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008183 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008184 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008185 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008186 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008187 return;
8188 }
Benny Halevycbe82602011-05-22 19:52:37 +03008189 dprintk("<-- %s\n", __func__);
8190}
8191
8192static void nfs4_layoutreturn_release(void *calldata)
8193{
8194 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008195 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008196 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008197
8198 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008199 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008200 if (lrp->res.lrs_present) {
8201 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8202 &lrp->args.range,
8203 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008204 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008205 } else
8206 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008207 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008208 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008209 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008210 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008211 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008212 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008213 kfree(calldata);
8214 dprintk("<-- %s\n", __func__);
8215}
8216
8217static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8218 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8219 .rpc_call_done = nfs4_layoutreturn_done,
8220 .rpc_release = nfs4_layoutreturn_release,
8221};
8222
Peng Tao6c166052014-11-17 09:30:40 +08008223int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008224{
8225 struct rpc_task *task;
8226 struct rpc_message msg = {
8227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8228 .rpc_argp = &lrp->args,
8229 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008230 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008231 };
8232 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008233 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008234 .rpc_message = &msg,
8235 .callback_ops = &nfs4_layoutreturn_call_ops,
8236 .callback_data = lrp,
8237 };
Peng Tao6c166052014-11-17 09:30:40 +08008238 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008239
Andrew Elble99ade3c2015-12-02 09:39:51 -05008240 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8241 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8242 &task_setup_data.rpc_client, &msg);
8243
Benny Halevycbe82602011-05-22 19:52:37 +03008244 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008245 if (!sync) {
8246 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8247 if (!lrp->inode) {
8248 nfs4_layoutreturn_release(lrp);
8249 return -EAGAIN;
8250 }
8251 task_setup_data.flags |= RPC_TASK_ASYNC;
8252 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008253 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008254 task = rpc_run_task(&task_setup_data);
8255 if (IS_ERR(task))
8256 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008257 if (sync)
8258 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008259 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008260 dprintk("<-- %s status=%d\n", __func__, status);
8261 rpc_put_task(task);
8262 return status;
8263}
8264
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008265static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008266_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8267 struct pnfs_device *pdev,
8268 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008269{
8270 struct nfs4_getdeviceinfo_args args = {
8271 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008272 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8273 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008274 };
8275 struct nfs4_getdeviceinfo_res res = {
8276 .pdev = pdev,
8277 };
8278 struct rpc_message msg = {
8279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8280 .rpc_argp = &args,
8281 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008282 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008283 };
8284 int status;
8285
8286 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008287 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008288 if (res.notification & ~args.notify_types)
8289 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008290 if (res.notification != args.notify_types)
8291 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008292
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008293 dprintk("<-- %s status=%d\n", __func__, status);
8294
8295 return status;
8296}
8297
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008298int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8299 struct pnfs_device *pdev,
8300 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008301{
8302 struct nfs4_exception exception = { };
8303 int err;
8304
8305 do {
8306 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008307 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008308 &exception);
8309 } while (exception.retry);
8310 return err;
8311}
8312EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8313
Andy Adamson863a3c62011-03-23 13:27:54 +00008314static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8315{
8316 struct nfs4_layoutcommit_data *data = calldata;
8317 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008318 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008319
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008320 nfs41_setup_sequence(session,
8321 &data->args.seq_args,
8322 &data->res.seq_res,
8323 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008324}
8325
8326static void
8327nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8328{
8329 struct nfs4_layoutcommit_data *data = calldata;
8330 struct nfs_server *server = NFS_SERVER(data->args.inode);
8331
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008332 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008333 return;
8334
8335 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008336 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8337 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8338 case -NFS4ERR_BADLAYOUT: /* no layout */
8339 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008340 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008341 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008342 break;
8343 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008344 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008345 rpc_restart_call_prepare(task);
8346 return;
8347 }
8348 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008349}
8350
8351static void nfs4_layoutcommit_release(void *calldata)
8352{
8353 struct nfs4_layoutcommit_data *data = calldata;
8354
Andy Adamsondb29c082011-07-30 20:52:38 -04008355 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008356 nfs_post_op_update_inode_force_wcc(data->args.inode,
8357 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008358 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008359 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008360 kfree(data);
8361}
8362
8363static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8364 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8365 .rpc_call_done = nfs4_layoutcommit_done,
8366 .rpc_release = nfs4_layoutcommit_release,
8367};
8368
8369int
Andy Adamsonef311532011-03-12 02:58:10 -05008370nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008371{
8372 struct rpc_message msg = {
8373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8374 .rpc_argp = &data->args,
8375 .rpc_resp = &data->res,
8376 .rpc_cred = data->cred,
8377 };
8378 struct rpc_task_setup task_setup_data = {
8379 .task = &data->task,
8380 .rpc_client = NFS_CLIENT(data->args.inode),
8381 .rpc_message = &msg,
8382 .callback_ops = &nfs4_layoutcommit_ops,
8383 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008384 };
8385 struct rpc_task *task;
8386 int status = 0;
8387
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008388 dprintk("NFS: initiating layoutcommit call. sync %d "
8389 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008390 data->args.lastbytewritten,
8391 data->args.inode->i_ino);
8392
Trond Myklebust472e2592015-02-05 16:50:30 -05008393 if (!sync) {
8394 data->inode = nfs_igrab_and_active(data->args.inode);
8395 if (data->inode == NULL) {
8396 nfs4_layoutcommit_release(data);
8397 return -EAGAIN;
8398 }
8399 task_setup_data.flags = RPC_TASK_ASYNC;
8400 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008401 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008402 task = rpc_run_task(&task_setup_data);
8403 if (IS_ERR(task))
8404 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008405 if (sync)
8406 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008407 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008408 dprintk("%s: status %d\n", __func__, status);
8409 rpc_put_task(task);
8410 return status;
8411}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008412
Andy Adamson97431202013-08-08 10:57:56 -04008413/**
8414 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8415 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8416 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008417static int
8418_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008419 struct nfs_fsinfo *info,
8420 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008421{
8422 struct nfs41_secinfo_no_name_args args = {
8423 .style = SECINFO_STYLE_CURRENT_FH,
8424 };
8425 struct nfs4_secinfo_res res = {
8426 .flavors = flavors,
8427 };
8428 struct rpc_message msg = {
8429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8430 .rpc_argp = &args,
8431 .rpc_resp = &res,
8432 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008433 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008434 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008435 int status;
8436
8437 if (use_integrity) {
8438 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008439 cred = nfs4_get_clid_cred(server->nfs_client);
8440 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008441 }
8442
8443 dprintk("--> %s\n", __func__);
8444 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8445 &res.seq_res, 0);
8446 dprintk("<-- %s status=%d\n", __func__, status);
8447
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008448 if (cred)
8449 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008450
8451 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008452}
8453
8454static int
8455nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8456 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8457{
8458 struct nfs4_exception exception = { };
8459 int err;
8460 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008461 /* first try using integrity protection */
8462 err = -NFS4ERR_WRONGSEC;
8463
8464 /* try to use integrity protection with machine cred */
8465 if (_nfs4_is_integrity_protected(server->nfs_client))
8466 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8467 flavors, true);
8468
8469 /*
8470 * if unable to use integrity protection, or SECINFO with
8471 * integrity protection returns NFS4ERR_WRONGSEC (which is
8472 * disallowed by spec, but exists in deployed servers) use
8473 * the current filesystem's rpc_client and the user cred.
8474 */
8475 if (err == -NFS4ERR_WRONGSEC)
8476 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8477 flavors, false);
8478
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008479 switch (err) {
8480 case 0:
8481 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008482 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008483 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008484 default:
8485 err = nfs4_handle_exception(server, err, &exception);
8486 }
8487 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008488out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008489 return err;
8490}
8491
8492static int
8493nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8494 struct nfs_fsinfo *info)
8495{
8496 int err;
8497 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008498 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008499 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008500 struct nfs4_secinfo4 *secinfo;
8501 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008502
8503 page = alloc_page(GFP_KERNEL);
8504 if (!page) {
8505 err = -ENOMEM;
8506 goto out;
8507 }
8508
8509 flavors = page_address(page);
8510 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8511
8512 /*
8513 * Fall back on "guess and check" method if
8514 * the server doesn't support SECINFO_NO_NAME
8515 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008516 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008517 err = nfs4_find_root_sec(server, fhandle, info);
8518 goto out_freepage;
8519 }
8520 if (err)
8521 goto out_freepage;
8522
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008523 for (i = 0; i < flavors->num_flavors; i++) {
8524 secinfo = &flavors->flavors[i];
8525
8526 switch (secinfo->flavor) {
8527 case RPC_AUTH_NULL:
8528 case RPC_AUTH_UNIX:
8529 case RPC_AUTH_GSS:
8530 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8531 &secinfo->flavor_info);
8532 break;
8533 default:
8534 flavor = RPC_AUTH_MAXFLAVOR;
8535 break;
8536 }
8537
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008538 if (!nfs_auth_info_match(&server->auth_info, flavor))
8539 flavor = RPC_AUTH_MAXFLAVOR;
8540
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008541 if (flavor != RPC_AUTH_MAXFLAVOR) {
8542 err = nfs4_lookup_root_sec(server, fhandle,
8543 info, flavor);
8544 if (!err)
8545 break;
8546 }
8547 }
8548
8549 if (flavor == RPC_AUTH_MAXFLAVOR)
8550 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008551
8552out_freepage:
8553 put_page(page);
8554 if (err == -EACCES)
8555 return -EPERM;
8556out:
8557 return err;
8558}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008559
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008560static int _nfs41_test_stateid(struct nfs_server *server,
8561 nfs4_stateid *stateid,
8562 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008563{
8564 int status;
8565 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008566 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008567 };
8568 struct nfs41_test_stateid_res res;
8569 struct rpc_message msg = {
8570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8571 .rpc_argp = &args,
8572 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008573 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008574 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008575 struct rpc_clnt *rpc_client = server->client;
8576
8577 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8578 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008579
Chuck Lever38527b12012-07-11 16:30:23 -04008580 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008581 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008582 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008583 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008584 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008585 if (status != NFS_OK) {
8586 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008587 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008588 }
8589 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008590 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008591}
8592
Chuck Lever38527b12012-07-11 16:30:23 -04008593/**
8594 * nfs41_test_stateid - perform a TEST_STATEID operation
8595 *
8596 * @server: server / transport on which to perform the operation
8597 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008598 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008599 *
8600 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8601 * Otherwise a negative NFS4ERR value is returned if the operation
8602 * failed or the state ID is not currently valid.
8603 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008604static int nfs41_test_stateid(struct nfs_server *server,
8605 nfs4_stateid *stateid,
8606 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008607{
8608 struct nfs4_exception exception = { };
8609 int err;
8610 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008611 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008612 if (err != -NFS4ERR_DELAY)
8613 break;
8614 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008615 } while (exception.retry);
8616 return err;
8617}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008618
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008619struct nfs_free_stateid_data {
8620 struct nfs_server *server;
8621 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008622 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008623};
8624
8625static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8626{
8627 struct nfs_free_stateid_data *data = calldata;
8628 nfs41_setup_sequence(nfs4_get_session(data->server),
8629 &data->args.seq_args,
8630 &data->res.seq_res,
8631 task);
8632}
8633
8634static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8635{
8636 struct nfs_free_stateid_data *data = calldata;
8637
8638 nfs41_sequence_done(task, &data->res.seq_res);
8639
8640 switch (task->tk_status) {
8641 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008642 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008643 rpc_restart_call_prepare(task);
8644 }
8645}
8646
8647static void nfs41_free_stateid_release(void *calldata)
8648{
8649 kfree(calldata);
8650}
8651
Trond Myklebust17f26b12013-08-21 15:48:42 -04008652static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008653 .rpc_call_prepare = nfs41_free_stateid_prepare,
8654 .rpc_call_done = nfs41_free_stateid_done,
8655 .rpc_release = nfs41_free_stateid_release,
8656};
8657
8658static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8659 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008660 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008661 bool privileged)
8662{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008663 struct rpc_message msg = {
8664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008665 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008666 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008667 struct rpc_task_setup task_setup = {
8668 .rpc_client = server->client,
8669 .rpc_message = &msg,
8670 .callback_ops = &nfs41_free_stateid_ops,
8671 .flags = RPC_TASK_ASYNC,
8672 };
8673 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008674
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008675 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8676 &task_setup.rpc_client, &msg);
8677
Chuck Lever38527b12012-07-11 16:30:23 -04008678 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008679 data = kmalloc(sizeof(*data), GFP_NOFS);
8680 if (!data)
8681 return ERR_PTR(-ENOMEM);
8682 data->server = server;
8683 nfs4_stateid_copy(&data->args.stateid, stateid);
8684
8685 task_setup.callback_data = data;
8686
8687 msg.rpc_argp = &data->args;
8688 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008689 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008690 if (privileged)
8691 nfs4_set_sequence_privileged(&data->args.seq_args);
8692
8693 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008694}
8695
Chuck Lever38527b12012-07-11 16:30:23 -04008696/**
8697 * nfs41_free_stateid - perform a FREE_STATEID operation
8698 *
8699 * @server: server / transport on which to perform the operation
8700 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008701 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008702 *
8703 * Returns NFS_OK if the server freed "stateid". Otherwise a
8704 * negative NFS4ERR value is returned.
8705 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008706static int nfs41_free_stateid(struct nfs_server *server,
8707 nfs4_stateid *stateid,
8708 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008709{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008710 struct rpc_task *task;
8711 int ret;
8712
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008713 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008714 if (IS_ERR(task))
8715 return PTR_ERR(task);
8716 ret = rpc_wait_for_completion_task(task);
8717 if (!ret)
8718 ret = task->tk_status;
8719 rpc_put_task(task);
8720 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008721}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008722
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008723static void
8724nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008725{
8726 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008727 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008728
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008729 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008730 nfs4_free_lock_state(server, lsp);
8731 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008732 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008733 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008734}
8735
Trond Myklebust36281ca2012-03-04 18:13:56 -05008736static bool nfs41_match_stateid(const nfs4_stateid *s1,
8737 const nfs4_stateid *s2)
8738{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008739 if (s1->type != s2->type)
8740 return false;
8741
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008742 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008743 return false;
8744
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008745 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008746 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008747 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008748 return true;
8749
8750 return false;
8751}
8752
Andy Adamson557134a2009-04-01 09:21:53 -04008753#endif /* CONFIG_NFS_V4_1 */
8754
Trond Myklebust36281ca2012-03-04 18:13:56 -05008755static bool nfs4_match_stateid(const nfs4_stateid *s1,
8756 const nfs4_stateid *s2)
8757{
Trond Myklebustf597c532012-03-04 18:13:56 -05008758 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008759}
8760
8761
Trond Myklebust17280172012-03-11 13:11:00 -04008762static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008763 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008764 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 .recover_open = nfs4_open_reclaim,
8766 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008767 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008768 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769};
8770
Andy Adamson591d71c2009-04-01 09:22:47 -04008771#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008772static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008773 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8774 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8775 .recover_open = nfs4_open_reclaim,
8776 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008777 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008778 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008779 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008780};
8781#endif /* CONFIG_NFS_V4_1 */
8782
Trond Myklebust17280172012-03-11 13:11:00 -04008783static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008784 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008785 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008786 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008788 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789};
8790
Andy Adamson591d71c2009-04-01 09:22:47 -04008791#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008792static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008793 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8794 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008795 .recover_open = nfs41_open_expired,
8796 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008797 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008798};
8799#endif /* CONFIG_NFS_V4_1 */
8800
Trond Myklebust17280172012-03-11 13:11:00 -04008801static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008802 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008803 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008804 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008805};
8806
8807#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008808static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008809 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008810 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008811 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008812};
8813#endif
8814
Chuck Leverec011fe2013-10-17 14:12:39 -04008815static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008816 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008817 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008818};
8819
8820#if defined(CONFIG_NFS_V4_1)
8821static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008822 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008823 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008824};
8825#endif /* CONFIG_NFS_V4_1 */
8826
Trond Myklebust97dc1352010-06-16 09:52:26 -04008827static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8828 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008829 .init_caps = NFS_CAP_READDIRPLUS
8830 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008831 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008832 .init_client = nfs40_init_client,
8833 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008834 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008835 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008836 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008837 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008838 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008839 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8840 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8841 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008842 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008843};
8844
8845#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008846static struct nfs_seqid *
8847nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8848{
8849 return NULL;
8850}
8851
Trond Myklebust97dc1352010-06-16 09:52:26 -04008852static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8853 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008854 .init_caps = NFS_CAP_READDIRPLUS
8855 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008856 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008857 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008858 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008859 .init_client = nfs41_init_client,
8860 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008861 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008862 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008863 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008864 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008865 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008866 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8867 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8868 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008869 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008870};
8871#endif
8872
Steve Dickson42c2c422013-05-22 12:50:38 -04008873#if defined(CONFIG_NFS_V4_2)
8874static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8875 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008876 .init_caps = NFS_CAP_READDIRPLUS
8877 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008878 | NFS_CAP_POSIX_LOCK
8879 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008880 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008881 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008882 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008883 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008884 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008885 | NFS_CAP_LAYOUTSTATS
8886 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008887 .init_client = nfs41_init_client,
8888 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008889 .match_stateid = nfs41_match_stateid,
8890 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008891 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008892 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008893 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008894 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8895 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8896 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008897 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008898};
8899#endif
8900
Trond Myklebust97dc1352010-06-16 09:52:26 -04008901const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8902 [0] = &nfs_v4_0_minor_ops,
8903#if defined(CONFIG_NFS_V4_1)
8904 [1] = &nfs_v4_1_minor_ops,
8905#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008906#if defined(CONFIG_NFS_V4_2)
8907 [2] = &nfs_v4_2_minor_ops,
8908#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008909};
8910
Trond Myklebust13997822016-07-24 17:10:52 -04008911static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008912{
8913 ssize_t error, error2;
8914
8915 error = generic_listxattr(dentry, list, size);
8916 if (error < 0)
8917 return error;
8918 if (list) {
8919 list += error;
8920 size -= error;
8921 }
8922
8923 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8924 if (error2 < 0)
8925 return error2;
8926 return error + error2;
8927}
8928
Trond Myklebust17f26b12013-08-21 15:48:42 -04008929static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008930 .create = nfs_create,
8931 .lookup = nfs_lookup,
8932 .atomic_open = nfs_atomic_open,
8933 .link = nfs_link,
8934 .unlink = nfs_unlink,
8935 .symlink = nfs_symlink,
8936 .mkdir = nfs_mkdir,
8937 .rmdir = nfs_rmdir,
8938 .mknod = nfs_mknod,
8939 .rename = nfs_rename,
8940 .permission = nfs_permission,
8941 .getattr = nfs_getattr,
8942 .setattr = nfs_setattr,
8943 .getxattr = generic_getxattr,
8944 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008945 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04008946 .removexattr = generic_removexattr,
8947};
8948
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008949static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008950 .permission = nfs_permission,
8951 .getattr = nfs_getattr,
8952 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008953 .getxattr = generic_getxattr,
8954 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008955 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008956 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008957};
8958
David Howells509de812006-08-22 20:06:11 -04008959const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960 .version = 4, /* protocol version */
8961 .dentry_ops = &nfs4_dentry_operations,
8962 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008963 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008964 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008966 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008967 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 .getattr = nfs4_proc_getattr,
8969 .setattr = nfs4_proc_setattr,
8970 .lookup = nfs4_proc_lookup,
8971 .access = nfs4_proc_access,
8972 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973 .create = nfs4_proc_create,
8974 .remove = nfs4_proc_remove,
8975 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008976 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008978 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008979 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008980 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 .link = nfs4_proc_link,
8982 .symlink = nfs4_proc_symlink,
8983 .mkdir = nfs4_proc_mkdir,
8984 .rmdir = nfs4_proc_remove,
8985 .readdir = nfs4_proc_readdir,
8986 .mknod = nfs4_proc_mknod,
8987 .statfs = nfs4_proc_statfs,
8988 .fsinfo = nfs4_proc_fsinfo,
8989 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008990 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008991 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008992 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008994 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008996 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008998 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008999 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009001 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009002 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009003 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009004 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009005 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009006 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009007 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009008 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009009 .create_server = nfs4_create_server,
9010 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011};
9012
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009013static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009014 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009015 .list = nfs4_xattr_list_nfs4_acl,
9016 .get = nfs4_xattr_get_nfs4_acl,
9017 .set = nfs4_xattr_set_nfs4_acl,
9018};
9019
9020const struct xattr_handler *nfs4_xattr_handlers[] = {
9021 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009022#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9023 &nfs4_xattr_nfs4_label_handler,
9024#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009025 NULL
9026};
9027
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028/*
9029 * Local variables:
9030 * c-basic-offset: 8
9031 * End:
9032 */