blob: 4b1491da16eb003cb7d21ea5ecf835bec9db9e90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400366 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500368 int ret = errorcode;
369
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400370 exception->delay = 0;
371 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 exception->retry = 0;
373 switch(errorcode) {
374 case 0:
375 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500376 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400380 if (inode) {
381 int err;
382
383 err = nfs_async_inode_return_delegation(inode,
384 stateid);
385 if (err == 0)
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
389 break;
390 }
391 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 if (state == NULL)
393 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400394 ret = nfs4_schedule_stateid_recovery(server, state);
395 if (ret < 0)
396 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400398 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400399 if (state != NULL) {
400 ret = nfs4_schedule_stateid_recovery(server, state);
401 if (ret < 0)
402 break;
403 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500405 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400408 case -NFS4ERR_MOVED:
409 ret = nfs4_schedule_migration_recovery(server);
410 if (ret < 0)
411 break;
412 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500416#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
425 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400427 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500428#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
432 * fail
433 */
434 ret = -EBUSY;
435 break;
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400438 nfs_inc_server_stats(server, NFSIOS_DELAY);
439 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400440 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400441 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400442 exception->delay = 1;
443 return 0;
444
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800448 break;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
457 "uid/gids. "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
460 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 }
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500464wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->recovering = 1;
466 return 0;
467}
468
469/* This is the error handling routine for processes that are allowed
470 * to sleep.
471 */
472int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
473{
474 struct nfs_client *clp = server->nfs_client;
475 int ret;
476
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
480 goto out_retry;
481 }
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
485 return -EIO;
486 goto out_retry;
487 }
488 return ret;
489out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500490 if (ret == 0)
491 exception->retry = 1;
492 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500493}
494
Trond Myklebust037fc982015-09-20 15:51:00 -0400495static int
496nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
498{
499 struct nfs_client *clp = server->nfs_client;
500 int ret;
501
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
505 goto out_retry;
506 }
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
511 goto out_retry;
512 }
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 ret = -EIO;
515 return ret;
516out_retry:
517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
520}
521
522static int
523nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
525{
526 struct nfs4_exception exception = {
527 .state = state,
528 };
529
530 if (task->tk_status >= 0)
531 return 0;
532 if (timeout)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
535 task->tk_status,
536 &exception);
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
539 if (exception.retry)
540 return -EAGAIN;
541 return 0;
542}
543
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400544/*
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
547 */
548static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
549{
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
551
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
554 return true;
555
556 return false;
557}
Trond Myklebust65de8722008-12-23 15:21:44 -0500558
Trond Myklebust452e9352010-07-31 14:29:06 -0400559static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
565}
566
Trond Myklebust452e9352010-07-31 14:29:06 -0400567static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
568{
Trond Myklebustbe824162015-07-05 14:50:46 -0400569 struct nfs_client *clp = server->nfs_client;
570
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400573}
574
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400575struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
579};
580
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800581void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400583{
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
587
588 res->sr_slot = NULL;
589}
590
591static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
592{
593 args->sa_privileged = 1;
594}
595
Peng Taocb04ad22014-06-11 05:24:15 +0800596int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400600{
Chuck Lever3bd23842013-08-09 12:49:19 -0400601 struct nfs4_slot *slot;
602
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
605 goto out_start;
606
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
609 goto out_sleep;
610
611 slot = nfs4_alloc_slot(tbl);
612 if (IS_ERR(slot)) {
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
615 goto out_sleep;
616 }
617 spin_unlock(&tbl->slot_tbl_lock);
618
619 args->sa_slot = slot;
620 res->sr_slot = slot;
621
622out_start:
623 rpc_call_start(task);
624 return 0;
625
626out_sleep:
627 if (args->sa_privileged)
628 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
629 NULL, RPC_PRIORITY_PRIVILEGED);
630 else
631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
633 return -EAGAIN;
634}
Peng Taocb04ad22014-06-11 05:24:15 +0800635EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400636
637static int nfs40_sequence_done(struct rpc_task *task,
638 struct nfs4_sequence_res *res)
639{
640 struct nfs4_slot *slot = res->sr_slot;
641 struct nfs4_slot_table *tbl;
642
Trond Myklebustcab92c12014-01-29 12:12:15 -0500643 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400644 goto out;
645
646 tbl = slot->table;
647 spin_lock(&tbl->slot_tbl_lock);
648 if (!nfs41_wake_and_assign_slot(tbl, slot))
649 nfs4_free_slot(tbl, slot);
650 spin_unlock(&tbl->slot_tbl_lock);
651
652 res->sr_slot = NULL;
653out:
654 return 1;
655}
656
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400657#if defined(CONFIG_NFS_V4_1)
658
Trond Myklebustd185a332010-06-16 09:52:25 -0400659static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400660{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500661 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400662 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500663 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500664 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400665
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500667 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500668
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500669 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500670 /* Be nice to the server: try to ensure that the last transmitted
671 * value for highest_user_slotid <= target_highest_slotid
672 */
673 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
674 send_new_highest_used_slotid = true;
675
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500677 send_new_highest_used_slotid = false;
678 goto out_unlock;
679 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500680 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500681
682 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
683 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500684out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500685 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400686 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500687 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400688 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400689}
690
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500691int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500693 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500694 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400695 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500696 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500697 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400698
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 if (slot == NULL)
700 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400701 /* don't increment the sequence number if the task wasn't sent */
702 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703 goto out;
704
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500705 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500706
Trond Myklebustac20d162012-12-15 15:36:07 -0500707 if (slot->interrupted) {
708 slot->interrupted = 0;
709 interrupted = true;
710 }
711
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400712 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500713 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400714 switch (res->sr_status) {
715 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400716 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500717 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500718 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500719 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500720 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400721 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500722 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500724 case 1:
725 /*
726 * sr_status remains 1 if an RPC level error occurred.
727 * The server may or may not have processed the sequence
728 * operation..
729 * Mark the slot as having hosted an interrupted RPC call.
730 */
731 slot->interrupted = 1;
732 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400733 case -NFS4ERR_DELAY:
734 /* The server detected a resend of the RPC call and
735 * returned NFS4ERR_DELAY as per Section 2.10.6.2
736 * of RFC5661.
737 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500738 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400739 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500740 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500741 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400742 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500743 case -NFS4ERR_BADSLOT:
744 /*
745 * The slot id we used was probably retired. Try again
746 * using a different slot id.
747 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500748 goto retry_nowait;
749 case -NFS4ERR_SEQ_MISORDERED:
750 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500751 * Was the last operation on this sequence interrupted?
752 * If so, retry after bumping the sequence number.
753 */
754 if (interrupted) {
755 ++slot->seq_nr;
756 goto retry_nowait;
757 }
758 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500759 * Could this slot have been previously retired?
760 * If so, then the server may be expecting seq_nr = 1!
761 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500762 if (slot->seq_nr != 1) {
763 slot->seq_nr = 1;
764 goto retry_nowait;
765 }
766 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500767 case -NFS4ERR_SEQ_FALSE_RETRY:
768 ++slot->seq_nr;
769 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400770 default:
771 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500772 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400773 }
774out:
775 /* The session may be reset by one of the error handlers. */
776 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400777 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500778out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500779 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500780retry_nowait:
781 if (rpc_restart_call_prepare(task)) {
782 task->tk_status = 0;
783 ret = 0;
784 }
785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400787 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400788 goto out;
789 rpc_delay(task, NFS4_POLL_RETRY_MAX);
790 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400791}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500792EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400793
Peng Tao2c4b1312014-06-11 05:24:16 +0800794int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400795{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500796 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400797 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400798 if (!res->sr_slot->table->session)
799 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400801}
Peng Tao2c4b1312014-06-11 05:24:16 +0800802EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400803
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000804int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400805 struct nfs4_sequence_args *args,
806 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 struct rpc_task *task)
808{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400809 struct nfs4_slot *slot;
810 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400811
812 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400813 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400814 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400815 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400816
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400817 tbl = &session->fc_slot_table;
818
Trond Myklebust69d206b2012-11-22 13:21:02 -0500819 task->tk_timeout = 0;
820
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400821 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400822 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400823 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500824 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500825 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400826 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400827 }
828
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500829 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500830 if (IS_ERR(slot)) {
831 /* If out of memory, try again in 1/4 second */
832 if (slot == ERR_PTR(-ENOMEM))
833 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400834 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400835 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400836 }
837 spin_unlock(&tbl->slot_tbl_lock);
838
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500839 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400840
Chuck Levere8d92382013-08-09 12:47:51 -0400841 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500842 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400843
Benny Halevydfb4f3092010-09-24 09:17:01 -0400844 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500845 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400846 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400847 /*
848 * sr_status is only set in decode_sequence, and so will remain
849 * set to 1 if an rpc level failure occurs.
850 */
851 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400852 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400853out_success:
854 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400856out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400857 /* Privileged tasks are queued with top priority */
858 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400859 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
860 NULL, RPC_PRIORITY_PRIVILEGED);
861 else
862 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400863 spin_unlock(&tbl->slot_tbl_lock);
864 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000866EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400867
Chuck Lever5a580e02013-08-09 12:48:09 -0400868static int nfs4_setup_sequence(const struct nfs_server *server,
869 struct nfs4_sequence_args *args,
870 struct nfs4_sequence_res *res,
871 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400872{
Trond Myklebust035168a2010-06-16 09:52:26 -0400873 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400874 int ret = 0;
875
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800877 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
878 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400879
Chuck Levere8d92382013-08-09 12:47:51 -0400880 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400881 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400882 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400883
Trond Myklebust9d12b212012-01-17 22:04:25 -0500884 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400885
Andy Adamsonce5039c2009-04-01 09:22:13 -0400886 dprintk("<-- %s status=%d\n", __func__, ret);
887 return ret;
888}
889
Andy Adamsonce5039c2009-04-01 09:22:13 -0400890static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
891{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400892 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400893 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894
Trond Myklebust035168a2010-06-16 09:52:26 -0400895 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
896
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898}
899
Andy Adamson69ab40c2009-04-01 09:22:19 -0400900static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
901{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400902 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400903
Trond Myklebust14516c32010-07-31 14:29:06 -0400904 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400905}
906
Trond Myklebust17280172012-03-11 13:11:00 -0400907static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400908 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400909 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400910};
911
Chuck Lever3bd23842013-08-09 12:49:19 -0400912#else /* !CONFIG_NFS_V4_1 */
913
Chuck Lever5a580e02013-08-09 12:48:09 -0400914static int nfs4_setup_sequence(const struct nfs_server *server,
915 struct nfs4_sequence_args *args,
916 struct nfs4_sequence_res *res,
917 struct rpc_task *task)
918{
Peng Taocb04ad22014-06-11 05:24:15 +0800919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
920 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400921}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400922
Peng Tao2c4b1312014-06-11 05:24:16 +0800923int nfs4_sequence_done(struct rpc_task *task,
924 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400925{
Chuck Lever3bd23842013-08-09 12:49:19 -0400926 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400927}
Peng Tao2c4b1312014-06-11 05:24:16 +0800928EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400929
930#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400931
Chuck Lever9915ea72013-08-09 12:48:27 -0400932static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
933{
934 struct nfs4_call_sync_data *data = calldata;
935 nfs4_setup_sequence(data->seq_server,
936 data->seq_args, data->seq_res, task);
937}
938
939static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
940{
941 struct nfs4_call_sync_data *data = calldata;
942 nfs4_sequence_done(task, data->seq_res);
943}
944
945static const struct rpc_call_ops nfs40_call_sync_ops = {
946 .rpc_call_prepare = nfs40_call_sync_prepare,
947 .rpc_call_done = nfs40_call_sync_done,
948};
949
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400950static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400951 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100953 struct nfs4_sequence_args *args,
954 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400955{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100956 int ret;
957 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400958 struct nfs_client *clp = server->nfs_client;
959 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 .seq_server = server,
961 .seq_args = args,
962 .seq_res = res,
963 };
964 struct rpc_task_setup task_setup = {
965 .rpc_client = clnt,
966 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400967 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 .callback_data = &data
969 };
970
971 task = rpc_run_task(&task_setup);
972 if (IS_ERR(task))
973 ret = PTR_ERR(task);
974 else {
975 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400976 rpc_put_task(task);
977 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978 return ret;
979}
980
Bryan Schumaker7c513052011-03-24 17:12:24 +0000981int nfs4_call_sync(struct rpc_clnt *clnt,
982 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000983 struct rpc_message *msg,
984 struct nfs4_sequence_args *args,
985 struct nfs4_sequence_res *res,
986 int cache_reply)
987{
Chuck Levera9c92d62013-08-09 12:48:18 -0400988 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400989 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
993{
994 struct nfs_inode *nfsi = NFS_I(dir);
995
996 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400997 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700998 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001000 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001001 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001002 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 spin_unlock(&dir->i_lock);
1004}
1005
1006struct nfs4_opendata {
1007 struct kref kref;
1008 struct nfs_openargs o_arg;
1009 struct nfs_openres o_res;
1010 struct nfs_open_confirmargs c_arg;
1011 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001012 struct nfs4_string owner_name;
1013 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001014 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001016 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001018 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct nfs4_state_owner *owner;
1020 struct nfs4_state *state;
1021 struct iattr attrs;
1022 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001023 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001024 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001025 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001026 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int cancelled;
1028};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001029
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001030static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1031 int err, struct nfs4_exception *exception)
1032{
1033 if (err != -EINVAL)
1034 return false;
1035 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1036 return false;
1037 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1038 exception->retry = 1;
1039 return true;
1040}
1041
Trond Myklebust6ae37332015-01-30 14:21:14 -05001042static u32
1043nfs4_map_atomic_open_share(struct nfs_server *server,
1044 fmode_t fmode, int openflags)
1045{
1046 u32 res = 0;
1047
1048 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1049 case FMODE_READ:
1050 res = NFS4_SHARE_ACCESS_READ;
1051 break;
1052 case FMODE_WRITE:
1053 res = NFS4_SHARE_ACCESS_WRITE;
1054 break;
1055 case FMODE_READ|FMODE_WRITE:
1056 res = NFS4_SHARE_ACCESS_BOTH;
1057 }
1058 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1059 goto out;
1060 /* Want no delegation if we're using O_DIRECT */
1061 if (openflags & O_DIRECT)
1062 res |= NFS4_SHARE_WANT_NO_DELEG;
1063out:
1064 return res;
1065}
1066
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001067static enum open_claim_type4
1068nfs4_map_atomic_open_claim(struct nfs_server *server,
1069 enum open_claim_type4 claim)
1070{
1071 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1072 return claim;
1073 switch (claim) {
1074 default:
1075 return claim;
1076 case NFS4_OPEN_CLAIM_FH:
1077 return NFS4_OPEN_CLAIM_NULL;
1078 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1079 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1080 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1081 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1082 }
1083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001086{
1087 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001088 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001089 p->o_res.seqid = p->o_arg.seqid;
1090 p->c_res.seqid = p->c_arg.seqid;
1091 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001092 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001093 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001094 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001095}
1096
Al Viro82a2c1b2011-06-22 18:30:55 -04001097static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001098 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001099 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001100 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001101 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001102 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103{
Al Viro82a2c1b2011-06-22 18:30:55 -04001104 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001105 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001107 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001108 struct nfs4_opendata *p;
1109
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001110 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001111 if (p == NULL)
1112 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001113
1114 p->f_label = nfs4_label_alloc(server, gfp_mask);
1115 if (IS_ERR(p->f_label))
1116 goto err_free_p;
1117
Kinglong Meea49c2692015-07-27 15:31:38 +08001118 p->a_label = nfs4_label_alloc(server, gfp_mask);
1119 if (IS_ERR(p->a_label))
1120 goto err_free_f;
1121
Trond Myklebust63f5f792015-01-23 19:19:25 -05001122 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1123 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001124 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001125 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001126 nfs_sb_active(dentry->d_sb);
1127 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 p->dir = parent;
1129 p->owner = sp;
1130 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001131 p->o_arg.open_flags = flags;
1132 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001133 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1134 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001135 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1136 * will return permission denied for all bits until close */
1137 if (!(flags & O_EXCL)) {
1138 /* ask server to check for all possible rights as results
1139 * are cached */
1140 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1141 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001142 }
David Howells7539bba2006-08-22 20:06:09 -04001143 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001144 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1145 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001146 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001148 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001149 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001150 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1152 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001153 case NFS4_OPEN_CLAIM_NULL:
1154 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1155 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1156 p->o_arg.fh = NFS_FH(dir);
1157 break;
1158 case NFS4_OPEN_CLAIM_PREVIOUS:
1159 case NFS4_OPEN_CLAIM_FH:
1160 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1161 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001162 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001163 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001164 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001165 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001166
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001167 p->o_arg.u.attrs = &p->attrs;
1168 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001169
1170 verf[0] = jiffies;
1171 verf[1] = current->pid;
1172 memcpy(p->o_arg.u.verifier.data, verf,
1173 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001175 p->c_arg.fh = &p->o_res.fh;
1176 p->c_arg.stateid = &p->o_res.stateid;
1177 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001179 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001181
1182err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001183 nfs4_label_free(p->a_label);
1184err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001185 nfs4_label_free(p->f_label);
1186err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 kfree(p);
1188err:
1189 dput(parent);
1190 return NULL;
1191}
1192
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001193static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001195 struct nfs4_opendata *p = container_of(kref,
1196 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001197 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001198
1199 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001200 if (p->state != NULL)
1201 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001202 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001203
Kinglong Meea49c2692015-07-27 15:31:38 +08001204 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001205 nfs4_label_free(p->f_label);
1206
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001207 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001208 dput(p->dentry);
1209 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001210 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001211 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001212 kfree(p);
1213}
1214
1215static void nfs4_opendata_put(struct nfs4_opendata *p)
1216{
1217 if (p != NULL)
1218 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001219}
1220
Trond Myklebust06f814a2006-01-03 09:55:07 +01001221static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1222{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001223 int ret;
1224
Trond Myklebust06f814a2006-01-03 09:55:07 +01001225 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001226 return ret;
1227}
1228
Trond Myklebust24311f82015-09-20 10:50:17 -04001229static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1230 fmode_t fmode)
1231{
1232 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1233 case FMODE_READ|FMODE_WRITE:
1234 return state->n_rdwr != 0;
1235 case FMODE_WRITE:
1236 return state->n_wronly != 0;
1237 case FMODE_READ:
1238 return state->n_rdonly != 0;
1239 }
1240 WARN_ON_ONCE(1);
1241 return false;
1242}
1243
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001244static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001245{
1246 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247
Trond Myklebust536e43d2012-01-17 22:04:26 -05001248 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001249 goto out;
1250 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001251 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001252 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1253 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001254 break;
1255 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001256 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1257 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001258 break;
1259 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001260 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1261 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001262 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264 return ret;
1265}
1266
Trond Myklebust2a606182015-08-19 22:30:00 -05001267static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1268 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001270 if (delegation == NULL)
1271 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001272 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001274 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1275 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001276 switch (claim) {
1277 case NFS4_OPEN_CLAIM_NULL:
1278 case NFS4_OPEN_CLAIM_FH:
1279 break;
1280 case NFS4_OPEN_CLAIM_PREVIOUS:
1281 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1282 break;
1283 default:
1284 return 0;
1285 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001286 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001287 return 1;
1288}
1289
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001291{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001292 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001293 case FMODE_WRITE:
1294 state->n_wronly++;
1295 break;
1296 case FMODE_READ:
1297 state->n_rdonly++;
1298 break;
1299 case FMODE_READ|FMODE_WRITE:
1300 state->n_rdwr++;
1301 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001302 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001303}
1304
Trond Myklebust4f14c192014-02-12 19:15:06 -05001305static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001306{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001307 struct nfs_client *clp = state->owner->so_server->nfs_client;
1308 bool need_recover = false;
1309
1310 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1311 need_recover = true;
1312 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1313 need_recover = true;
1314 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1315 need_recover = true;
1316 if (need_recover)
1317 nfs4_state_mark_reclaim_nograce(clp, state);
1318}
1319
Trond Myklebuste999e802014-02-10 18:20:47 -05001320static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1321 nfs4_stateid *stateid)
1322{
1323 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1324 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001325 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1326 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001327 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001328 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001329 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1330 return true;
1331 return false;
1332}
1333
Trond Myklebustf95549c2015-01-23 18:06:09 -05001334static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1335{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001336 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1337 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001338 if (state->n_wronly)
1339 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1340 if (state->n_rdonly)
1341 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1342 if (state->n_rdwr)
1343 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001344 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001345}
1346
Trond Myklebust226056c2014-02-11 10:41:07 -05001347static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001348 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001349 nfs4_stateid *stateid, fmode_t fmode)
1350{
1351 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1352 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1353 case FMODE_WRITE:
1354 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1355 break;
1356 case FMODE_READ:
1357 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1358 break;
1359 case 0:
1360 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1361 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1362 clear_bit(NFS_OPEN_STATE, &state->flags);
1363 }
1364 if (stateid == NULL)
1365 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001366 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001367 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1368 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1369 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001370 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001371 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001372 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001373 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001374 nfs4_stateid_copy(&state->stateid, stateid);
1375 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001376}
1377
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001378static void nfs_clear_open_stateid(struct nfs4_state *state,
1379 nfs4_stateid *arg_stateid,
1380 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001381{
1382 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001383 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001384 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001385 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1386 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001387}
1388
Trond Myklebust003707c2007-07-05 18:07:55 -04001389static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1390{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001391 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001392 case FMODE_READ:
1393 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1394 break;
1395 case FMODE_WRITE:
1396 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1397 break;
1398 case FMODE_READ|FMODE_WRITE:
1399 set_bit(NFS_O_RDWR_STATE, &state->flags);
1400 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001401 if (!nfs_need_update_open_stateid(state, stateid))
1402 return;
1403 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1404 nfs4_stateid_copy(&state->stateid, stateid);
1405 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001406}
1407
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001408static 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 -07001409{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001410 /*
1411 * Protect the call to nfs4_state_set_mode_locked and
1412 * serialise the stateid update
1413 */
Andrew Elble361cad32015-12-02 09:20:57 -05001414 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001415 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001416 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001417 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001418 set_bit(NFS_DELEGATED_STATE, &state->flags);
1419 }
1420 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001421 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001422 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001424 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427static 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 -05001428{
1429 struct nfs_inode *nfsi = NFS_I(state->inode);
1430 struct nfs_delegation *deleg_cur;
1431 int ret = 0;
1432
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001434
1435 rcu_read_lock();
1436 deleg_cur = rcu_dereference(nfsi->delegation);
1437 if (deleg_cur == NULL)
1438 goto no_delegation;
1439
1440 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001441 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001442 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001443 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001444 goto no_delegation_unlock;
1445
1446 if (delegation == NULL)
1447 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001448 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001449 goto no_delegation_unlock;
1450
Trond Myklebustb7391f42008-12-23 15:21:52 -05001451 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001452 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001453 ret = 1;
1454no_delegation_unlock:
1455 spin_unlock(&deleg_cur->lock);
1456no_delegation:
1457 rcu_read_unlock();
1458
1459 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001460 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001461 ret = 1;
1462 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001463 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1464 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001465
1466 return ret;
1467}
1468
Trond Myklebust39071e62015-01-24 15:07:56 -05001469static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1470 const nfs4_stateid *stateid)
1471{
1472 struct nfs4_state *state = lsp->ls_state;
1473 bool ret = false;
1474
1475 spin_lock(&state->state_lock);
1476 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1477 goto out_noupdate;
1478 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1479 goto out_noupdate;
1480 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1481 ret = true;
1482out_noupdate:
1483 spin_unlock(&state->state_lock);
1484 return ret;
1485}
Trond Myklebust34310432008-12-23 15:21:38 -05001486
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488{
1489 struct nfs_delegation *delegation;
1490
1491 rcu_read_lock();
1492 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001494 rcu_read_unlock();
1495 return;
1496 }
1497 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001498 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001499}
1500
Trond Myklebust6ee41262007-07-08 14:11:36 -04001501static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001502{
1503 struct nfs4_state *state = opendata->state;
1504 struct nfs_inode *nfsi = NFS_I(state->inode);
1505 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001506 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001507 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001508 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001509 nfs4_stateid stateid;
1510 int ret = -EAGAIN;
1511
Trond Myklebustaac00a82007-07-05 19:02:21 -04001512 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001513 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001514 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001515 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001516 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001517 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001518 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001519 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001520 rcu_read_lock();
1521 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001522 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001523 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001524 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001525 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001526 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001527 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001528 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001529 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001530 if (!opendata->is_recover) {
1531 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1532 if (ret != 0)
1533 goto out;
1534 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001535 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001536
1537 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001539 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001541out:
1542 return ERR_PTR(ret);
1543out_return_state:
1544 atomic_inc(&state->count);
1545 return state;
1546}
1547
Andy Adamsone23008e2012-10-02 21:07:32 -04001548static void
1549nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1550{
1551 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1552 struct nfs_delegation *delegation;
1553 int delegation_flags = 0;
1554
1555 rcu_read_lock();
1556 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1557 if (delegation)
1558 delegation_flags = delegation->flags;
1559 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001560 switch (data->o_arg.claim) {
1561 default:
1562 break;
1563 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1564 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001565 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1566 "returning a delegation for "
1567 "OPEN(CLAIM_DELEGATE_CUR)\n",
1568 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001569 return;
1570 }
1571 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001572 nfs_inode_set_delegation(state->inode,
1573 data->owner->so_cred,
1574 &data->o_res);
1575 else
1576 nfs_inode_reclaim_delegation(state->inode,
1577 data->owner->so_cred,
1578 &data->o_res);
1579}
1580
1581/*
1582 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1583 * and update the nfs4_state.
1584 */
1585static struct nfs4_state *
1586_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1587{
1588 struct inode *inode = data->state->inode;
1589 struct nfs4_state *state = data->state;
1590 int ret;
1591
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001592 if (!data->rpc_done) {
1593 if (data->rpc_status) {
1594 ret = data->rpc_status;
1595 goto err;
1596 }
1597 /* cached opens have already been processed */
1598 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001599 }
1600
Andy Adamsone23008e2012-10-02 21:07:32 -04001601 ret = nfs_refresh_inode(inode, &data->f_attr);
1602 if (ret)
1603 goto err;
1604
1605 if (data->o_res.delegation_type != 0)
1606 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001607update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001608 update_open_stateid(state, &data->o_res.stateid, NULL,
1609 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001610 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001611
1612 return state;
1613err:
1614 return ERR_PTR(ret);
1615
1616}
1617
1618static struct nfs4_state *
1619_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620{
1621 struct inode *inode;
1622 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001623 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624
Trond Myklebustaac00a82007-07-05 19:02:21 -04001625 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001626 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001627 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628 goto out;
1629 }
1630
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001631 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001633 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001634 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001635 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001636 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001637 goto err;
1638 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 state = nfs4_get_open_state(inode, data->owner);
1640 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001641 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001642 if (data->o_res.delegation_type != 0)
1643 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001644 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001645 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001647out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001648 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001650err_put_inode:
1651 iput(inode);
1652err:
1653 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001654}
1655
Andy Adamsone23008e2012-10-02 21:07:32 -04001656static struct nfs4_state *
1657nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1658{
1659 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1660 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1661 return _nfs4_opendata_to_nfs4_state(data);
1662}
1663
Trond Myklebust864472e2006-01-03 09:55:15 +01001664static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1665{
1666 struct nfs_inode *nfsi = NFS_I(state->inode);
1667 struct nfs_open_context *ctx;
1668
1669 spin_lock(&state->inode->i_lock);
1670 list_for_each_entry(ctx, &nfsi->open_files, list) {
1671 if (ctx->state != state)
1672 continue;
1673 get_nfs_open_context(ctx);
1674 spin_unlock(&state->inode->i_lock);
1675 return ctx;
1676 }
1677 spin_unlock(&state->inode->i_lock);
1678 return ERR_PTR(-ENOENT);
1679}
1680
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001681static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1682 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001683{
1684 struct nfs4_opendata *opendata;
1685
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001686 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001687 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001688 if (opendata == NULL)
1689 return ERR_PTR(-ENOMEM);
1690 opendata->state = state;
1691 atomic_inc(&state->count);
1692 return opendata;
1693}
1694
Trond Myklebust24311f82015-09-20 10:50:17 -04001695static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1696 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001697{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001698 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001699 int ret;
1700
Trond Myklebust24311f82015-09-20 10:50:17 -04001701 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001702 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001703 opendata->o_arg.open_flags = 0;
1704 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001705 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1706 NFS_SB(opendata->dentry->d_sb),
1707 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001708 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1709 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1710 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001711 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 if (ret != 0)
1713 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001714 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001715 if (IS_ERR(newstate))
1716 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001717 if (newstate != opendata->state)
1718 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001719 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001720 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001721}
1722
1723static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1724{
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 int ret;
1726
Trond Myklebust4f14c192014-02-12 19:15:06 -05001727 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1728 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1729 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1730 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001731 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001732 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001733 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001734 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001735 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1736 if (ret != 0)
1737 return ret;
1738 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1739 if (ret != 0)
1740 return ret;
1741 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1742 if (ret != 0)
1743 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001744 /*
1745 * We may have performed cached opens for all three recoveries.
1746 * Check if we need to update the current stateid.
1747 */
1748 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001749 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001750 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001751 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001752 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001753 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001754 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001755 return 0;
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/*
1759 * OPEN_RECLAIM:
1760 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001762static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001764 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001765 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001766 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int status;
1768
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001769 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1770 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001771 if (IS_ERR(opendata))
1772 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001773 rcu_read_lock();
1774 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001775 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001776 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001777 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001778 opendata->o_arg.u.delegation_type = delegation_type;
1779 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001780 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return status;
1782}
1783
Trond Myklebust539cd032007-06-05 11:46:42 -04001784static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct nfs_server *server = NFS_SERVER(state->inode);
1787 struct nfs4_exception exception = { };
1788 int err;
1789 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001790 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001791 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001792 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1793 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001794 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001795 break;
1796 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 } while (exception.retry);
1798 return err;
1799}
1800
Trond Myklebust864472e2006-01-03 09:55:15 +01001801static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1802{
1803 struct nfs_open_context *ctx;
1804 int ret;
1805
1806 ctx = nfs4_state_find_open_context(state);
1807 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001808 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001809 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001810 put_nfs_open_context(ctx);
1811 return ret;
1812}
1813
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001814static 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 -07001815{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001816 switch (err) {
1817 default:
1818 printk(KERN_ERR "NFS: %s: unhandled error "
1819 "%d.\n", __func__, err);
1820 case 0:
1821 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001822 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001823 case -ESTALE:
1824 break;
1825 case -NFS4ERR_BADSESSION:
1826 case -NFS4ERR_BADSLOT:
1827 case -NFS4ERR_BAD_HIGH_SLOT:
1828 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1829 case -NFS4ERR_DEADSESSION:
1830 set_bit(NFS_DELEGATED_STATE, &state->flags);
1831 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1832 return -EAGAIN;
1833 case -NFS4ERR_STALE_CLIENTID:
1834 case -NFS4ERR_STALE_STATEID:
1835 set_bit(NFS_DELEGATED_STATE, &state->flags);
1836 case -NFS4ERR_EXPIRED:
1837 /* Don't recall a delegation if it was lost */
1838 nfs4_schedule_lease_recovery(server->nfs_client);
1839 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001840 case -NFS4ERR_MOVED:
1841 nfs4_schedule_migration_recovery(server);
1842 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001843 case -NFS4ERR_LEASE_MOVED:
1844 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1845 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001846 case -NFS4ERR_DELEG_REVOKED:
1847 case -NFS4ERR_ADMIN_REVOKED:
1848 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001849 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001850 nfs_inode_find_state_and_recover(state->inode,
1851 stateid);
1852 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001853 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001854 case -NFS4ERR_DELAY:
1855 case -NFS4ERR_GRACE:
1856 set_bit(NFS_DELEGATED_STATE, &state->flags);
1857 ssleep(1);
1858 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001859 case -ENOMEM:
1860 case -NFS4ERR_DENIED:
1861 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1862 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return err;
1865}
1866
Trond Myklebust24311f82015-09-20 10:50:17 -04001867int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1868 struct nfs4_state *state, const nfs4_stateid *stateid,
1869 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001870{
1871 struct nfs_server *server = NFS_SERVER(state->inode);
1872 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001873 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001874
1875 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1876 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1877 if (IS_ERR(opendata))
1878 return PTR_ERR(opendata);
1879 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001880 write_seqlock(&state->seqlock);
1881 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1882 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001883 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1884 switch (type & (FMODE_READ|FMODE_WRITE)) {
1885 case FMODE_READ|FMODE_WRITE:
1886 case FMODE_WRITE:
1887 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1888 if (err)
1889 break;
1890 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1891 if (err)
1892 break;
1893 case FMODE_READ:
1894 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1895 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001896 nfs4_opendata_put(opendata);
1897 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1898}
1899
Chuck Leverbe05c862013-08-09 12:49:47 -04001900static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1901{
1902 struct nfs4_opendata *data = calldata;
1903
Peng Taocb04ad22014-06-11 05:24:15 +08001904 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1905 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001906}
1907
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001908static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1909{
1910 struct nfs4_opendata *data = calldata;
1911
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001912 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001913
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001914 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001915 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001916 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001917 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001918 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001919 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001920 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001921}
1922
1923static void nfs4_open_confirm_release(void *calldata)
1924{
1925 struct nfs4_opendata *data = calldata;
1926 struct nfs4_state *state = NULL;
1927
1928 /* If this request hasn't been cancelled, do nothing */
1929 if (data->cancelled == 0)
1930 goto out_free;
1931 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001932 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001933 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001934 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001935 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001936 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001937out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001938 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001939}
1940
1941static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001942 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001943 .rpc_call_done = nfs4_open_confirm_done,
1944 .rpc_release = nfs4_open_confirm_release,
1945};
1946
1947/*
1948 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1949 */
1950static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1951{
David Howells2b0143b2015-03-17 22:25:59 +00001952 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001953 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001954 struct rpc_message msg = {
1955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1956 .rpc_argp = &data->c_arg,
1957 .rpc_resp = &data->c_res,
1958 .rpc_cred = data->owner->so_cred,
1959 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001960 struct rpc_task_setup task_setup_data = {
1961 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001962 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001963 .callback_ops = &nfs4_open_confirm_ops,
1964 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001965 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001966 .flags = RPC_TASK_ASYNC,
1967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int status;
1969
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001970 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001971 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001972 data->rpc_done = 0;
1973 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001974 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001975 if (data->is_recover)
1976 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001977 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001978 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001980 status = nfs4_wait_for_completion_rpc_task(task);
1981 if (status != 0) {
1982 data->cancelled = 1;
1983 smp_wmb();
1984 } else
1985 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001986 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return status;
1988}
1989
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001990static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001992 struct nfs4_opendata *data = calldata;
1993 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001994 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001995 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001996
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001997 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001998 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001999 /*
2000 * Check if we still need to send an OPEN call, or if we can use
2001 * a delegation instead.
2002 */
2003 if (data->state != NULL) {
2004 struct nfs_delegation *delegation;
2005
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002006 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002007 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002008 rcu_read_lock();
2009 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002010 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002011 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002012 rcu_read_unlock();
2013 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002014 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002015 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002016 switch (claim) {
2017 default:
2018 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002019 case NFS4_OPEN_CLAIM_PREVIOUS:
2020 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2021 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002022 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002023 case NFS4_OPEN_CLAIM_FH:
2024 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002025 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2026 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002027 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002028 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002029 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002030 &data->o_res.seq_res,
2031 task) != 0)
2032 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002033
2034 /* Set the create mode (note dependency on the session type) */
2035 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2036 if (data->o_arg.open_flags & O_EXCL) {
2037 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2038 if (nfs4_has_persistent_session(clp))
2039 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2040 else if (clp->cl_mvops->minor_version > 0)
2041 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2042 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002043 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002044unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002045 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002046 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002047out_no_action:
2048 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002049out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002050 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002051}
2052
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002053static void nfs4_open_done(struct rpc_task *task, void *calldata)
2054{
2055 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002057 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002058
Trond Myklebust14516c32010-07-31 14:29:06 -04002059 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2060 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002061
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002062 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002063 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2064 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002065 case S_IFREG:
2066 break;
2067 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002068 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002069 break;
2070 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002071 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002072 break;
2073 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002074 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002075 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002076 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002077 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002078 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2079 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002080 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002081 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002082}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002083
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002084static void nfs4_open_release(void *calldata)
2085{
2086 struct nfs4_opendata *data = calldata;
2087 struct nfs4_state *state = NULL;
2088
2089 /* If this request hasn't been cancelled, do nothing */
2090 if (data->cancelled == 0)
2091 goto out_free;
2092 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002093 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002094 goto out_free;
2095 /* In case we need an open_confirm, no cleanup! */
2096 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2097 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002098 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002099 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002100 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002101out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002102 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002103}
2104
2105static const struct rpc_call_ops nfs4_open_ops = {
2106 .rpc_call_prepare = nfs4_open_prepare,
2107 .rpc_call_done = nfs4_open_done,
2108 .rpc_release = nfs4_open_release,
2109};
2110
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002111static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112{
David Howells2b0143b2015-03-17 22:25:59 +00002113 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002114 struct nfs_server *server = NFS_SERVER(dir);
2115 struct nfs_openargs *o_arg = &data->o_arg;
2116 struct nfs_openres *o_res = &data->o_res;
2117 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002118 struct rpc_message msg = {
2119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2120 .rpc_argp = o_arg,
2121 .rpc_resp = o_res,
2122 .rpc_cred = data->owner->so_cred,
2123 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002124 struct rpc_task_setup task_setup_data = {
2125 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002126 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002127 .callback_ops = &nfs4_open_ops,
2128 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002129 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002130 .flags = RPC_TASK_ASYNC,
2131 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002132 int status;
2133
Chuck Levera9c92d62013-08-09 12:48:18 -04002134 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002135 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002136 data->rpc_done = 0;
2137 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002138 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002139 data->is_recover = 0;
2140 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002141 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002142 data->is_recover = 1;
2143 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002144 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002145 if (IS_ERR(task))
2146 return PTR_ERR(task);
2147 status = nfs4_wait_for_completion_rpc_task(task);
2148 if (status != 0) {
2149 data->cancelled = 1;
2150 smp_wmb();
2151 } else
2152 status = data->rpc_status;
2153 rpc_put_task(task);
2154
2155 return status;
2156}
2157
2158static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2159{
David Howells2b0143b2015-03-17 22:25:59 +00002160 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002161 struct nfs_openres *o_res = &data->o_res;
2162 int status;
2163
2164 status = nfs4_run_open_task(data, 1);
2165 if (status != 0 || !data->rpc_done)
2166 return status;
2167
Trond Myklebust6926afd2012-01-07 13:22:46 -05002168 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2169
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002170 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2171 status = _nfs4_proc_open_confirm(data);
2172 if (status != 0)
2173 return status;
2174 }
2175
2176 return status;
2177}
2178
Trond Myklebustf3792d62014-07-10 08:54:32 -04002179/*
2180 * Additional permission checks in order to distinguish between an
2181 * open for read, and an open for execute. This works around the
2182 * fact that NFSv4 OPEN treats read and execute permissions as being
2183 * the same.
2184 * Note that in the non-execute case, we want to turn off permission
2185 * checking if we just created a new file (POSIX open() semantics).
2186 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002187static int nfs4_opendata_access(struct rpc_cred *cred,
2188 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002189 struct nfs4_state *state, fmode_t fmode,
2190 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002191{
2192 struct nfs_access_entry cache;
2193 u32 mask;
2194
2195 /* access call failed or for some reason the server doesn't
2196 * support any access modes -- defer access call until later */
2197 if (opendata->o_res.access_supported == 0)
2198 return 0;
2199
2200 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002201 /*
2202 * Use openflags to check for exec, because fmode won't
2203 * always have FMODE_EXEC set when file open for exec.
2204 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002205 if (openflags & __FMODE_EXEC) {
2206 /* ONLY check for exec rights */
2207 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002208 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002209 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002210
2211 cache.cred = cred;
2212 cache.jiffies = jiffies;
2213 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2214 nfs_access_add_cache(state->inode, &cache);
2215
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002216 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002217 return 0;
2218
2219 /* even though OPEN succeeded, access is denied. Close the file */
2220 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002221 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002222}
2223
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002224/*
2225 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2226 */
2227static int _nfs4_proc_open(struct nfs4_opendata *data)
2228{
David Howells2b0143b2015-03-17 22:25:59 +00002229 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002230 struct nfs_server *server = NFS_SERVER(dir);
2231 struct nfs_openargs *o_arg = &data->o_arg;
2232 struct nfs_openres *o_res = &data->o_res;
2233 int status;
2234
2235 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002236 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002238 if (status != 0) {
2239 if (status == -NFS4ERR_BADNAME &&
2240 !(o_arg->open_flags & O_CREAT))
2241 return -ENOENT;
2242 return status;
2243 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002244
Trond Myklebust6926afd2012-01-07 13:22:46 -05002245 nfs_fattr_map_and_free_names(server, &data->f_attr);
2246
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002247 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002248 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002249 if (o_arg->open_flags & O_EXCL)
2250 data->file_created = 1;
2251 else if (o_res->cinfo.before != o_res->cinfo.after)
2252 data->file_created = 1;
2253 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002254 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2255 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002257 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002262 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Andy Adamsond83217c2011-03-01 01:34:17 +00002266static int nfs4_recover_expired_lease(struct nfs_server *server)
2267{
2268 return nfs4_client_recover_expired_lease(server->nfs_client);
2269}
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271/*
2272 * OPEN_EXPIRED:
2273 * reclaim state on the server after a network partition.
2274 * Assumes caller holds the appropriate lock
2275 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002276static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002278 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002279 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002281 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002282 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002283 if (IS_ERR(opendata))
2284 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002285 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002286 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002287 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002288 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002292static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002293{
Trond Myklebust539cd032007-06-05 11:46:42 -04002294 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002295 struct nfs4_exception exception = { };
2296 int err;
2297
2298 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002299 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002300 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002301 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2302 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002303 switch (err) {
2304 default:
2305 goto out;
2306 case -NFS4ERR_GRACE:
2307 case -NFS4ERR_DELAY:
2308 nfs4_handle_exception(server, err, &exception);
2309 err = 0;
2310 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002311 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002312out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002313 return err;
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002319 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Trond Myklebust864472e2006-01-03 09:55:15 +01002321 ctx = nfs4_state_find_open_context(state);
2322 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002323 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002324 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002325 put_nfs_open_context(ctx);
2326 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002329static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2330{
2331 nfs_remove_bad_delegation(state->inode);
2332 write_seqlock(&state->seqlock);
2333 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2334 write_sequnlock(&state->seqlock);
2335 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2336}
2337
2338static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2339{
2340 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2341 nfs_finish_clear_delegation_stateid(state);
2342}
2343
2344static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2345{
2346 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2347 nfs40_clear_delegation_stateid(state);
2348 return nfs4_open_expired(sp, state);
2349}
2350
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002351#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002352static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002353{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002354 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002355 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002356 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002357 struct rpc_cred *cred;
2358 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002359
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002360 /* Get the delegation credential for use by test/free_stateid */
2361 rcu_read_lock();
2362 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002363 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002364 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002365 return;
2366 }
2367
2368 nfs4_stateid_copy(&stateid, &delegation->stateid);
2369 cred = get_rpccred(delegation->cred);
2370 rcu_read_unlock();
2371 status = nfs41_test_stateid(server, &stateid, cred);
2372 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002373
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002374 if (status != NFS_OK) {
2375 /* Free the stateid unless the server explicitly
2376 * informs us the stateid is unrecognized. */
2377 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002378 nfs41_free_stateid(server, &stateid, cred);
2379 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002380 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002381
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002382 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002383}
2384
2385/**
2386 * nfs41_check_open_stateid - possibly free an open stateid
2387 *
2388 * @state: NFSv4 state for an inode
2389 *
2390 * Returns NFS_OK if recovery for this stateid is now finished.
2391 * Otherwise a negative NFS4ERR value is returned.
2392 */
2393static int nfs41_check_open_stateid(struct nfs4_state *state)
2394{
2395 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002396 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002397 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002398 int status;
2399
2400 /* If a state reset has been done, test_stateid is unneeded */
2401 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2402 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2403 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2404 return -NFS4ERR_BAD_STATEID;
2405
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002406 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002407 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002408 if (status != NFS_OK) {
2409 /* Free the stateid unless the server explicitly
2410 * informs us the stateid is unrecognized. */
2411 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002412 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002413
2414 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2415 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2416 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002417 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002418 }
2419 return status;
2420}
2421
2422static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2423{
Chuck Levereb64cf92012-07-11 16:30:05 -04002424 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002425
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002426 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002427 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002428 if (status != NFS_OK)
2429 status = nfs4_open_expired(sp, state);
2430 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002431}
2432#endif
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002435 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2436 * fields corresponding to attributes that were used to store the verifier.
2437 * Make sure we clobber those fields in the later setattr call
2438 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002439static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2440 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002441{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002442 const u32 *attrset = opendata->o_res.attrset;
2443
2444 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002445 !(sattr->ia_valid & ATTR_ATIME_SET))
2446 sattr->ia_valid |= ATTR_ATIME;
2447
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002448 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002449 !(sattr->ia_valid & ATTR_MTIME_SET))
2450 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002451
2452 /* Except MODE, it seems harmless of setting twice. */
2453 if ((attrset[1] & FATTR4_WORD1_MODE))
2454 sattr->ia_valid &= ~ATTR_MODE;
2455
2456 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2457 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002458}
2459
Trond Myklebustc21443c2013-02-07 14:26:21 -05002460static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2461 fmode_t fmode,
2462 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002463 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002464{
2465 struct nfs4_state_owner *sp = opendata->owner;
2466 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002467 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002468 struct nfs4_state *state;
2469 unsigned int seq;
2470 int ret;
2471
2472 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2473
2474 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002475 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002476 goto out;
2477
2478 state = nfs4_opendata_to_nfs4_state(opendata);
2479 ret = PTR_ERR(state);
2480 if (IS_ERR(state))
2481 goto out;
2482 if (server->caps & NFS_CAP_POSIX_LOCK)
2483 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2484
Trond Myklebust275bb302013-05-29 13:11:28 -04002485 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002486 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002487 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002488 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002489 alias = d_exact_alias(dentry, state->inode);
2490 if (!alias)
2491 alias = d_splice_alias(igrab(state->inode), dentry);
2492 /* d_splice_alias() can't fail here - it's a non-directory */
2493 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002494 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002495 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002496 }
2497 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002498 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002499 }
2500
Trond Myklebustc21443c2013-02-07 14:26:21 -05002501 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2502 if (ret != 0)
2503 goto out;
2504
Trond Myklebust3efb9722013-05-29 13:17:04 -04002505 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002506 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002507 nfs_inode_attach_open_context(ctx);
2508 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2509 nfs4_schedule_stateid_recovery(server, state);
2510 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002511out:
2512 return ret;
2513}
2514
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002515/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002516 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
Andy Adamson82be4172012-05-23 05:02:35 -04002518static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002519 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002520 int flags,
2521 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002522 struct nfs4_label *label,
2523 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 struct nfs4_state_owner *sp;
2526 struct nfs4_state *state = NULL;
2527 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002528 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002529 struct dentry *dentry = ctx->dentry;
2530 struct rpc_cred *cred = ctx->cred;
2531 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2532 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002533 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002534 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002535 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002539 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2540 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2542 goto out_err;
2543 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002544 status = nfs4_recover_expired_lease(server);
2545 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002546 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002547 if (d_really_is_positive(dentry))
2548 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002549 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002550 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002551 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002552 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002553 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002554 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002555 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
David Quigley14c43f72013-05-22 12:50:43 -04002557 if (label) {
2558 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2559 if (IS_ERR(olabel)) {
2560 status = PTR_ERR(olabel);
2561 goto err_opendata_put;
2562 }
2563 }
2564
Trond Myklebuste911b812014-03-26 13:24:37 -07002565 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2566 if (!opendata->f_attr.mdsthreshold) {
2567 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2568 if (!opendata->f_attr.mdsthreshold)
2569 goto err_free_label;
2570 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002571 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002572 }
David Howells2b0143b2015-03-17 22:25:59 +00002573 if (d_really_is_positive(dentry))
2574 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002575
Trond Myklebust3efb9722013-05-29 13:17:04 -04002576 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002577 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002578 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002579 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002580
NeilBrownefcbc042015-07-30 13:00:56 +10002581 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002582 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002583 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002584 /*
2585 * send create attributes which was not set by open
2586 * with an extra setattr.
2587 */
2588 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2589 nfs_fattr_init(opendata->o_res.f_attr);
2590 status = nfs4_do_setattr(state->inode, cred,
2591 opendata->o_res.f_attr, sattr,
2592 state, label, olabel);
2593 if (status == 0) {
2594 nfs_setattr_update_inode(state->inode, sattr,
2595 opendata->o_res.f_attr);
2596 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2597 }
David Quigley1775fd32013-05-22 12:50:42 -04002598 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002599 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002600 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002601 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002602
Trond Myklebuste911b812014-03-26 13:24:37 -07002603 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002604 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002605 opendata->f_attr.mdsthreshold = NULL;
2606 }
Andy Adamson82be4172012-05-23 05:02:35 -04002607
David Quigley14c43f72013-05-22 12:50:43 -04002608 nfs4_label_free(olabel);
2609
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002610 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002613err_free_label:
2614 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002615err_opendata_put:
2616 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002617err_put_state_owner:
2618 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return status;
2621}
2622
2623
Andy Adamson82be4172012-05-23 05:02:35 -04002624static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002625 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002626 int flags,
2627 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002628 struct nfs4_label *label,
2629 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002631 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 struct nfs4_exception exception = { };
2633 struct nfs4_state *res;
2634 int status;
2635
2636 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002637 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002638 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002639 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (status == 0)
2641 break;
2642 /* NOTE: BAD_SEQID means the server and client disagree about the
2643 * book-keeping w.r.t. state-changing operations
2644 * (OPEN/CLOSE/LOCK/LOCKU...)
2645 * It is actually a sign of a bug on the client or on the server.
2646 *
2647 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002648 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 * have unhashed the old state_owner for us, and that we can
2650 * therefore safely retry using a new one. We should still warn
2651 * the user though...
2652 */
2653 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002654 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002655 " returned a bad sequence-id error!\n",
2656 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 exception.retry = 1;
2658 continue;
2659 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002660 /*
2661 * BAD_STATEID on OPEN means that the server cancelled our
2662 * state before it received the OPEN_CONFIRM.
2663 * Recover by retrying the request as per the discussion
2664 * on Page 181 of RFC3530.
2665 */
2666 if (status == -NFS4ERR_BAD_STATEID) {
2667 exception.retry = 1;
2668 continue;
2669 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002670 if (status == -EAGAIN) {
2671 /* We must have found a delegation */
2672 exception.retry = 1;
2673 continue;
2674 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002675 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2676 continue;
2677 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 status, &exception));
2679 } while (exception.retry);
2680 return res;
2681}
2682
Trond Myklebust8487c472016-06-26 08:44:35 -04002683static int _nfs4_do_setattr(struct inode *inode,
2684 struct nfs_setattrargs *arg,
2685 struct nfs_setattrres *res,
2686 struct rpc_cred *cred,
2687 struct nfs4_state *state)
2688{
2689 struct nfs_server *server = NFS_SERVER(inode);
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2692 .rpc_argp = arg,
2693 .rpc_resp = res,
2694 .rpc_cred = cred,
2695 };
2696 struct rpc_cred *delegation_cred = NULL;
2697 unsigned long timestamp = jiffies;
2698 fmode_t fmode;
2699 bool truncate;
2700 int status;
2701
2702 nfs_fattr_init(res->fattr);
2703
2704 /* Servers should only apply open mode checks for file size changes */
2705 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2706 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2707
2708 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2709 /* Use that stateid */
2710 } else if (truncate && state != NULL) {
2711 struct nfs_lockowner lockowner = {
2712 .l_owner = current->files,
2713 .l_pid = current->tgid,
2714 };
2715 if (!nfs4_valid_open_stateid(state))
2716 return -EBADF;
2717 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2718 &arg->stateid, &delegation_cred) == -EIO)
2719 return -EBADF;
2720 } else
2721 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2722 if (delegation_cred)
2723 msg.rpc_cred = delegation_cred;
2724
2725 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2726
2727 put_rpccred(delegation_cred);
2728 if (status == 0 && state != NULL)
2729 renew_lease(server, timestamp);
2730 trace_nfs4_setattr(inode, &arg->stateid, status);
2731 return status;
2732}
2733
2734static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2735 struct nfs_fattr *fattr, struct iattr *sattr,
2736 struct nfs4_state *state, struct nfs4_label *ilabel,
2737 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002739 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002741 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .iap = sattr,
2743 .server = server,
2744 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002745 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 };
2747 struct nfs_setattrres res = {
2748 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002749 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 .server = server,
2751 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002752 struct nfs4_exception exception = {
2753 .state = state,
2754 .inode = inode,
2755 .stateid = &arg.stateid,
2756 };
2757 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
David Quigleyaa9c2662013-05-22 12:50:44 -04002759 arg.bitmask = nfs4_bitmask(server, ilabel);
2760 if (ilabel)
2761 arg.bitmask = nfs4_bitmask(server, olabel);
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002764 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002765 switch (err) {
2766 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002767 if (!(sattr->ia_valid & ATTR_SIZE)) {
2768 pr_warn_once("NFSv4: server %s is incorrectly "
2769 "applying open mode checks to "
2770 "a SETATTR that is not "
2771 "changing file size.\n",
2772 server->nfs_client->cl_hostname);
2773 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002774 if (state && !(state->state & FMODE_WRITE)) {
2775 err = -EBADF;
2776 if (sattr->ia_valid & ATTR_OPEN)
2777 err = -EACCES;
2778 goto out;
2779 }
2780 }
2781 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002783out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return err;
2785}
2786
Peng Tao500d7012015-09-22 11:35:22 +08002787static bool
2788nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2789{
2790 if (inode == NULL || !nfs_have_layout(inode))
2791 return false;
2792
2793 return pnfs_wait_on_layoutreturn(inode, task);
2794}
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796struct nfs4_closedata {
2797 struct inode *inode;
2798 struct nfs4_state *state;
2799 struct nfs_closeargs arg;
2800 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002801 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002802 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002803 bool roc;
2804 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805};
2806
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002807static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002808{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002809 struct nfs4_closedata *calldata = data;
2810 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002811 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002812
Fred Isamanf7e89172011-01-06 11:36:32 +00002813 if (calldata->roc)
2814 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002815 nfs4_put_open_state(calldata->state);
2816 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002817 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002818 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002819 kfree(calldata);
2820}
2821
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002822static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002824 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002827 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Chuck Levera3ca5652012-03-01 17:00:40 -05002829 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002830 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2831 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002832 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 /* hmm. we are done with the inode, and in the process of freeing
2834 * the state_owner. we keep this around to process errors
2835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 switch (task->tk_status) {
2837 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002838 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002839 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002840 pnfs_roc_set_barrier(state->inode,
2841 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002842 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002843 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002844 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002846 case -NFS4ERR_OLD_STATEID:
2847 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002849 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002850 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002851 rpc_restart_call_prepare(task);
2852 goto out_release;
2853 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002854 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002857 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002858 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002859 goto out_release;
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002862 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2863 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002864out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002865 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002866 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002867 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002870static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002872 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002873 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002874 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002875 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002876 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002877
Chuck Levera3ca5652012-03-01 17:00:40 -05002878 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002879 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002880 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002881
Trond Myklebust88069f72009-12-08 08:33:16 -05002882 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002883 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002884 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2885 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2886 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002887 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002888 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002889 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002890 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002891 if (state->n_rdonly == 0)
2892 call_close |= is_rdonly;
2893 else if (is_rdonly)
2894 calldata->arg.fmode |= FMODE_READ;
2895 if (state->n_wronly == 0)
2896 call_close |= is_wronly;
2897 else if (is_wronly)
2898 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002899 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2900 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002901 } else if (is_rdwr)
2902 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2903
Trond Myklebust5d422302013-03-14 16:57:48 -04002904 if (!nfs4_valid_open_stateid(state))
2905 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002906 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002907
2908 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002909 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002910 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002911 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002912
Peng Tao500d7012015-09-22 11:35:22 +08002913 if (nfs4_wait_on_layoutreturn(inode, task)) {
2914 nfs_release_seqid(calldata->arg.seqid);
2915 goto out_wait;
2916 }
2917
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002918 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002919 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002920 if (calldata->roc)
2921 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2922
Trond Myklebust6ae37332015-01-30 14:21:14 -05002923 calldata->arg.share_access =
2924 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2925 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002926
Trond Myklebust516a6af2005-10-27 22:12:41 -04002927 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002928 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002929 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002930 &calldata->arg.seq_args,
2931 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002932 task) != 0)
2933 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002934 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002935 return;
2936out_no_action:
2937 task->tk_action = NULL;
2938out_wait:
2939 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002942static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002943 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002944 .rpc_call_done = nfs4_close_done,
2945 .rpc_release = nfs4_free_closedata,
2946};
2947
Peng Taofe08c542014-07-03 13:05:01 +08002948static bool nfs4_roc(struct inode *inode)
2949{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002950 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002951 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002952 return pnfs_roc(inode);
2953}
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955/*
2956 * It is possible for data to be read/written from a mem-mapped file
2957 * after the sys_close call (which hits the vfs layer as a flush).
2958 * This means that we can't safely call nfsv4 close on a file until
2959 * the inode is cleared. This in turn means that we are not good
2960 * NFSv4 citizens - we do not indicate to the server to update the file's
2961 * share state even when we are done with one of the three share
2962 * stateid's in the inode.
2963 *
2964 * NOTE: Caller must be holding the sp->so_owner semaphore!
2965 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002966int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002968 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002969 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002971 struct nfs4_state_owner *sp = state->owner;
2972 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002973 struct rpc_message msg = {
2974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2975 .rpc_cred = state->owner->so_cred,
2976 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002977 struct rpc_task_setup task_setup_data = {
2978 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002979 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002980 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002981 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002982 .flags = RPC_TASK_ASYNC,
2983 };
Trond Myklebust95121352005-10-18 14:20:12 -07002984 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002986 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2987 &task_setup_data.rpc_client, &msg);
2988
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002989 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002991 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002992 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002993 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002995 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002997 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2998 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002999 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003000 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003001 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003002 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003003 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003004 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003005 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003006 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003007 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003008
Trond Myklebust1174dd12010-12-21 10:52:24 -05003009 msg.rpc_argp = &calldata->arg;
3010 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003011 task_setup_data.callback_data = calldata;
3012 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003013 if (IS_ERR(task))
3014 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003015 status = 0;
3016 if (wait)
3017 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003018 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003019 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003020out_free_calldata:
3021 kfree(calldata);
3022out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003023 nfs4_put_open_state(state);
3024 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003025 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Trond Myklebust2b484292010-09-17 10:56:51 -04003028static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003029nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3030 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003033 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3034
3035 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Trond Myklebust565277f2007-10-15 18:17:53 -04003037 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003038 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003039
3040 nfs4_label_release_security(label);
3041
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003042 if (IS_ERR(state))
3043 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003044 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Trond Myklebust1185a552009-12-03 15:54:02 -05003047static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003048{
3049 if (ctx->state == NULL)
3050 return;
3051 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003052 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003053 else
Al Viro643168c2011-06-22 18:20:23 -04003054 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Trond Myklebustb944dba2013-11-04 15:20:20 -05003057#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3058#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003059#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3062{
Kinglong Mee8c612822015-08-26 21:12:58 +08003063 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003064 struct nfs4_server_caps_arg args = {
3065 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003066 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 struct nfs4_server_caps_res res = {};
3069 struct rpc_message msg = {
3070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003071 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 .rpc_resp = &res,
3073 };
3074 int status;
3075
Kinglong Mee8c612822015-08-26 21:12:58 +08003076 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3077 FATTR4_WORD0_FH_EXPIRE_TYPE |
3078 FATTR4_WORD0_LINK_SUPPORT |
3079 FATTR4_WORD0_SYMLINK_SUPPORT |
3080 FATTR4_WORD0_ACLSUPPORT;
3081 if (minorversion)
3082 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3083
Bryan Schumaker7c513052011-03-24 17:12:24 +00003084 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003086 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003087 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003088 case 0:
3089 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3090 res.attr_bitmask[2] = 0;
3091 break;
3092 case 1:
3093 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3094 break;
3095 case 2:
3096 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003099 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3100 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3101 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3102 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003103 NFS_CAP_CTIME|NFS_CAP_MTIME|
3104 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003105 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3106 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 server->caps |= NFS_CAP_ACLS;
3108 if (res.has_links != 0)
3109 server->caps |= NFS_CAP_HARDLINKS;
3110 if (res.has_symlinks != 0)
3111 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003112 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3113 server->caps |= NFS_CAP_FILEID;
3114 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3115 server->caps |= NFS_CAP_MODE;
3116 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3117 server->caps |= NFS_CAP_NLINK;
3118 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3119 server->caps |= NFS_CAP_OWNER;
3120 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3121 server->caps |= NFS_CAP_OWNER_GROUP;
3122 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3123 server->caps |= NFS_CAP_ATIME;
3124 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3125 server->caps |= NFS_CAP_CTIME;
3126 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3127 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003128#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3129 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3130 server->caps |= NFS_CAP_SECURITY_LABEL;
3131#endif
3132 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3133 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003134 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003135
Trond Myklebusta65318b2009-03-11 14:10:28 -04003136 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3137 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3138 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003139 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003140 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3141 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003143 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return status;
3147}
3148
Trond Myklebust55a97592006-06-09 09:34:19 -04003149int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
3151 struct nfs4_exception exception = { };
3152 int err;
3153 do {
3154 err = nfs4_handle_exception(server,
3155 _nfs4_server_capabilities(server, fhandle),
3156 &exception);
3157 } while (exception.retry);
3158 return err;
3159}
3160
3161static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3162 struct nfs_fsinfo *info)
3163{
David Quigleyaa9c2662013-05-22 12:50:44 -04003164 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003166 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 };
3168 struct nfs4_lookup_res res = {
3169 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003170 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 .fh = fhandle,
3172 };
3173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3175 .rpc_argp = &args,
3176 .rpc_resp = &res,
3177 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003178
David Quigleyaa9c2662013-05-22 12:50:44 -04003179 bitmask[0] = nfs4_fattr_bitmap[0];
3180 bitmask[1] = nfs4_fattr_bitmap[1];
3181 /*
3182 * Process the label in the upcoming getfattr
3183 */
3184 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3185
Trond Myklebust0e574af2005-10-27 22:12:38 -04003186 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003187 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
3190static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3191 struct nfs_fsinfo *info)
3192{
3193 struct nfs4_exception exception = { };
3194 int err;
3195 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003196 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003197 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003198 switch (err) {
3199 case 0:
3200 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003201 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003202 default:
3203 err = nfs4_handle_exception(server, err, &exception);
3204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003206out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return err;
3208}
3209
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003210static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3211 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3212{
Trond Myklebustc2190662013-08-26 19:23:04 -04003213 struct rpc_auth_create_args auth_args = {
3214 .pseudoflavor = flavor,
3215 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003216 struct rpc_auth *auth;
3217 int ret;
3218
Trond Myklebustc2190662013-08-26 19:23:04 -04003219 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003220 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003221 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003222 goto out;
3223 }
3224 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003225out:
3226 return ret;
3227}
3228
Chuck Lever9a744ba2013-03-16 15:56:02 -04003229/*
3230 * Retry pseudoroot lookup with various security flavors. We do this when:
3231 *
3232 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3233 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3234 *
3235 * Returns zero on success, or a negative NFS4ERR value, or a
3236 * negative errno value.
3237 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003238static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003239 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003241 /* Per 3530bis 15.33.5 */
3242 static const rpc_authflavor_t flav_array[] = {
3243 RPC_AUTH_GSS_KRB5P,
3244 RPC_AUTH_GSS_KRB5I,
3245 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003246 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003247 RPC_AUTH_NULL,
3248 };
3249 int status = -EPERM;
3250 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003252 if (server->auth_info.flavor_len > 0) {
3253 /* try each flavor specified by user */
3254 for (i = 0; i < server->auth_info.flavor_len; i++) {
3255 status = nfs4_lookup_root_sec(server, fhandle, info,
3256 server->auth_info.flavors[i]);
3257 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3258 continue;
3259 break;
3260 }
3261 } else {
3262 /* no flavors specified by user, try default list */
3263 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3264 status = nfs4_lookup_root_sec(server, fhandle, info,
3265 flav_array[i]);
3266 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3267 continue;
3268 break;
3269 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003270 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003271
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003272 /*
3273 * -EACCESS could mean that the user doesn't have correct permissions
3274 * to access the mount. It could also mean that we tried to mount
3275 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3276 * existing mount programs don't handle -EACCES very well so it should
3277 * be mapped to -EPERM instead.
3278 */
3279 if (status == -EACCES)
3280 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003281 return status;
3282}
3283
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003284static int nfs4_do_find_root_sec(struct nfs_server *server,
3285 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3286{
3287 int mv = server->nfs_client->cl_minorversion;
3288 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3289}
3290
3291/**
3292 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3293 * @server: initialized nfs_server handle
3294 * @fhandle: we fill in the pseudo-fs root file handle
3295 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003296 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003297 *
3298 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003299 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003300int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003301 struct nfs_fsinfo *info,
3302 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003303{
Andre Przywarac7757072015-04-23 17:17:40 +01003304 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003305
Andre Przywarac7757072015-04-23 17:17:40 +01003306 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003307 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003308
3309 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003310 status = nfs4_do_find_root_sec(server, fhandle, info);
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 if (status == 0)
3313 status = nfs4_server_capabilities(server, fhandle);
3314 if (status == 0)
3315 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003316
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003317 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
Bryan Schumakerbae36242012-05-10 15:07:31 -04003320static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3321 struct nfs_fsinfo *info)
3322{
3323 int error;
3324 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003325 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003326
3327 error = nfs4_server_capabilities(server, mntfh);
3328 if (error < 0) {
3329 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3330 return error;
3331 }
3332
David Quigley14c43f72013-05-22 12:50:43 -04003333 label = nfs4_label_alloc(server, GFP_KERNEL);
3334 if (IS_ERR(label))
3335 return PTR_ERR(label);
3336
David Quigley1775fd32013-05-22 12:50:42 -04003337 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003338 if (error < 0) {
3339 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003340 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003341 }
3342
3343 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3344 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3345 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3346
David Quigley14c43f72013-05-22 12:50:43 -04003347err_free_label:
3348 nfs4_label_free(label);
3349
Bryan Schumakerbae36242012-05-10 15:07:31 -04003350 return error;
3351}
3352
Manoj Naik6b97fd32006-06-09 09:34:29 -04003353/*
3354 * Get locations and (maybe) other attributes of a referral.
3355 * Note that we'll actually follow the referral later when
3356 * we detect fsid mismatch in inode revalidation
3357 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003358static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3359 const struct qstr *name, struct nfs_fattr *fattr,
3360 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003361{
3362 int status = -ENOMEM;
3363 struct page *page = NULL;
3364 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003365
3366 page = alloc_page(GFP_KERNEL);
3367 if (page == NULL)
3368 goto out;
3369 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3370 if (locations == NULL)
3371 goto out;
3372
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003373 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003374 if (status != 0)
3375 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003376
3377 /*
3378 * If the fsid didn't change, this is a migration event, not a
3379 * referral. Cause us to drop into the exception handler, which
3380 * will kick off migration recovery.
3381 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003382 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003383 dprintk("%s: server did not return a different fsid for"
3384 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003385 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003386 goto out;
3387 }
Andy Adamson533eb462011-06-13 18:25:56 -04003388 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3389 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003390
Andy Adamson533eb462011-06-13 18:25:56 -04003391 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003392 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003393 memset(fhandle, 0, sizeof(struct nfs_fh));
3394out:
3395 if (page)
3396 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003397 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003398 return status;
3399}
3400
David Quigley1775fd32013-05-22 12:50:42 -04003401static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3402 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 struct nfs4_getattr_arg args = {
3405 .fh = fhandle,
3406 .bitmask = server->attr_bitmask,
3407 };
3408 struct nfs4_getattr_res res = {
3409 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003410 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 .server = server,
3412 };
3413 struct rpc_message msg = {
3414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3415 .rpc_argp = &args,
3416 .rpc_resp = &res,
3417 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003418
3419 args.bitmask = nfs4_bitmask(server, label);
3420
Trond Myklebust0e574af2005-10-27 22:12:38 -04003421 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003422 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
David Quigley1775fd32013-05-22 12:50:42 -04003425static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 struct nfs4_exception exception = { };
3429 int err;
3430 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003431 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3432 trace_nfs4_getattr(server, fhandle, fattr, err);
3433 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 &exception);
3435 } while (exception.retry);
3436 return err;
3437}
3438
3439/*
3440 * The file is not closed if it is opened due to the a request to change
3441 * the size of the file. The open call will not be needed once the
3442 * VFS layer lookup-intents are implemented.
3443 *
3444 * Close is called when the inode is destroyed.
3445 * If we haven't opened the file for O_WRONLY, we
3446 * need to in the size_change case to obtain a stateid.
3447 *
3448 * Got race?
3449 * Because OPEN is always done by name in nfsv4, it is
3450 * possible that we opened a different file by the same
3451 * name. We can recognize this race condition, but we
3452 * can't do anything about it besides returning an error.
3453 *
3454 * This will be fixed with VFS changes (lookup-intent).
3455 */
3456static int
3457nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3458 struct iattr *sattr)
3459{
David Howells2b0143b2015-03-17 22:25:59 +00003460 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003461 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003462 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003463 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 int status;
3465
Peng Tao88ac8152014-09-12 11:04:10 +08003466 if (pnfs_ld_layoutret_on_setattr(inode) &&
3467 sattr->ia_valid & ATTR_SIZE &&
3468 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003469 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003470
Trond Myklebust0e574af2005-10-27 22:12:38 -04003471 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Andy Adamson26699402012-05-30 16:12:24 -04003473 /* Deal with open(O_TRUNC) */
3474 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003475 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003476
3477 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003478 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003479 return 0;
3480
Trond Myklebustd5308382005-11-04 15:33:38 -05003481 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003482 if (sattr->ia_valid & ATTR_FILE) {
3483 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003484
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003485 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003486 if (ctx) {
3487 cred = ctx->cred;
3488 state = ctx->state;
3489 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003490 }
3491
David Quigley14c43f72013-05-22 12:50:43 -04003492 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3493 if (IS_ERR(label))
3494 return PTR_ERR(label);
3495
3496 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003497 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003498 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003499 nfs_setsecurity(inode, fattr, label);
3500 }
David Quigley14c43f72013-05-22 12:50:43 -04003501 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return status;
3503}
3504
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003505static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3506 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003507 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003508{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003509 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003510 int status;
3511 struct nfs4_lookup_arg args = {
3512 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003513 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003514 .name = name,
3515 };
3516 struct nfs4_lookup_res res = {
3517 .server = server,
3518 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003519 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003520 .fh = fhandle,
3521 };
3522 struct rpc_message msg = {
3523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3524 .rpc_argp = &args,
3525 .rpc_resp = &res,
3526 };
3527
David Quigleyaa9c2662013-05-22 12:50:44 -04003528 args.bitmask = nfs4_bitmask(server, label);
3529
David Howells2b3de442006-08-22 20:06:09 -04003530 nfs_fattr_init(fattr);
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003533 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 dprintk("NFS reply lookup: %d\n", status);
3535 return status;
3536}
3537
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003538static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003539{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003540 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003541 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003542 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3543 fattr->nlink = 2;
3544}
3545
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003546static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3547 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003548 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003549{
3550 struct nfs4_exception exception = { };
3551 struct rpc_clnt *client = *clnt;
3552 int err;
3553 do {
David Quigley1775fd32013-05-22 12:50:42 -04003554 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003555 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003556 switch (err) {
3557 case -NFS4ERR_BADNAME:
3558 err = -ENOENT;
3559 goto out;
3560 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003561 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003562 if (err == -NFS4ERR_MOVED)
3563 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003564 goto out;
3565 case -NFS4ERR_WRONGSEC:
3566 err = -EPERM;
3567 if (client != *clnt)
3568 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003569 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003570 if (IS_ERR(client))
3571 return PTR_ERR(client);
3572
3573 exception.retry = 1;
3574 break;
3575 default:
3576 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3577 }
3578 } while (exception.retry);
3579
3580out:
3581 if (err == 0)
3582 *clnt = client;
3583 else if (client != *clnt)
3584 rpc_shutdown_client(client);
3585
3586 return err;
3587}
3588
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003589static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003590 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3591 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003593 int status;
3594 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003595
David Quigley1775fd32013-05-22 12:50:42 -04003596 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003597 if (client != NFS_CLIENT(dir)) {
3598 rpc_shutdown_client(client);
3599 nfs_fixup_secinfo_attributes(fattr);
3600 }
3601 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003604struct rpc_clnt *
3605nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3606 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3607{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003608 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003609 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003610
David Quigley1775fd32013-05-22 12:50:42 -04003611 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003612 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003613 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003614 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003615}
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3618{
Trond Myklebust76b32992007-08-10 17:45:11 -04003619 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 struct nfs4_accessargs args = {
3621 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003622 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003624 struct nfs4_accessres res = {
3625 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003626 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3629 .rpc_argp = &args,
3630 .rpc_resp = &res,
3631 .rpc_cred = entry->cred,
3632 };
3633 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003634 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636 /*
3637 * Determine which access bits we want to ask for...
3638 */
3639 if (mode & MAY_READ)
3640 args.access |= NFS4_ACCESS_READ;
3641 if (S_ISDIR(inode->i_mode)) {
3642 if (mode & MAY_WRITE)
3643 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3644 if (mode & MAY_EXEC)
3645 args.access |= NFS4_ACCESS_LOOKUP;
3646 } else {
3647 if (mode & MAY_WRITE)
3648 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3649 if (mode & MAY_EXEC)
3650 args.access |= NFS4_ACCESS_EXECUTE;
3651 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003652
3653 res.fattr = nfs_alloc_fattr();
3654 if (res.fattr == NULL)
3655 return -ENOMEM;
3656
Bryan Schumaker7c513052011-03-24 17:12:24 +00003657 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003659 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003660 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003662 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return status;
3664}
3665
3666static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3667{
3668 struct nfs4_exception exception = { };
3669 int err;
3670 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003671 err = _nfs4_proc_access(inode, entry);
3672 trace_nfs4_access(inode, err);
3673 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 &exception);
3675 } while (exception.retry);
3676 return err;
3677}
3678
3679/*
3680 * TODO: For the time being, we don't try to get any attributes
3681 * along with any of the zero-copy operations READ, READDIR,
3682 * READLINK, WRITE.
3683 *
3684 * In the case of the first three, we want to put the GETATTR
3685 * after the read-type operation -- this is because it is hard
3686 * to predict the length of a GETATTR response in v4, and thus
3687 * align the READ data correctly. This means that the GETATTR
3688 * may end up partially falling into the page cache, and we should
3689 * shift it into the 'tail' of the xdr_buf before processing.
3690 * To do this efficiently, we need to know the total length
3691 * of data received, which doesn't seem to be available outside
3692 * of the RPC layer.
3693 *
3694 * In the case of WRITE, we also want to put the GETATTR after
3695 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003696 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 *
3698 * Both of these changes to the XDR layer would in fact be quite
3699 * minor, but I decided to leave them for a subsequent patch.
3700 */
3701static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3702 unsigned int pgbase, unsigned int pglen)
3703{
3704 struct nfs4_readlink args = {
3705 .fh = NFS_FH(inode),
3706 .pgbase = pgbase,
3707 .pglen = pglen,
3708 .pages = &page,
3709 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003710 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 struct rpc_message msg = {
3712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3713 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003714 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 };
3716
Bryan Schumaker7c513052011-03-24 17:12:24 +00003717 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 -07003718}
3719
3720static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3721 unsigned int pgbase, unsigned int pglen)
3722{
3723 struct nfs4_exception exception = { };
3724 int err;
3725 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003726 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3727 trace_nfs4_readlink(inode, err);
3728 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 &exception);
3730 } while (exception.retry);
3731 return err;
3732}
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003735 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737static int
3738nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003739 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
David Quigleyaa9c2662013-05-22 12:50:44 -04003741 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003742 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 int status = 0;
3745
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003746 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3747 if (IS_ERR(ctx))
3748 return PTR_ERR(ctx);
3749
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3751
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003752 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003753 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 if (IS_ERR(state)) {
3755 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003756 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003759 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003760 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return status;
3762}
3763
3764static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3765{
Trond Myklebust16e42952005-10-27 22:12:44 -04003766 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003767 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003769 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003771 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003772 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003773 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3776 .rpc_argp = &args,
3777 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003779 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003780
Bryan Schumaker7c513052011-03-24 17:12:24 +00003781 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003782 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003783 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return status;
3785}
3786
3787static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3788{
3789 struct nfs4_exception exception = { };
3790 int err;
3791 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003792 err = _nfs4_proc_remove(dir, name);
3793 trace_nfs4_remove(dir, name, err);
3794 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 &exception);
3796 } while (exception.retry);
3797 return err;
3798}
3799
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003800static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003802 struct nfs_server *server = NFS_SERVER(dir);
3803 struct nfs_removeargs *args = msg->rpc_argp;
3804 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003806 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003808 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003809
3810 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003813static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3814{
Al Viro884be172016-04-28 23:56:31 -04003815 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003816 &data->args.seq_args,
3817 &data->res.seq_res,
3818 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003821static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003823 struct nfs_unlinkdata *data = task->tk_calldata;
3824 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003825
Trond Myklebust14516c32010-07-31 14:29:06 -04003826 if (!nfs4_sequence_done(task, &res->seq_res))
3827 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003828 if (nfs4_async_handle_error(task, res->server, NULL,
3829 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003830 return 0;
3831 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003832 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833}
3834
Jeff Laytond3d41522010-09-17 17:31:57 -04003835static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3836{
3837 struct nfs_server *server = NFS_SERVER(dir);
3838 struct nfs_renameargs *arg = msg->rpc_argp;
3839 struct nfs_renameres *res = msg->rpc_resp;
3840
3841 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003842 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003843 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003844}
3845
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003846static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3847{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003848 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3849 &data->args.seq_args,
3850 &data->res.seq_res,
3851 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003852}
3853
3854static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3855 struct inode *new_dir)
3856{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003857 struct nfs_renamedata *data = task->tk_calldata;
3858 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003859
3860 if (!nfs4_sequence_done(task, &res->seq_res))
3861 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003862 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003863 return 0;
3864
3865 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003866 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003867 return 1;
3868}
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3871{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003872 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 struct nfs4_link_arg arg = {
3874 .fh = NFS_FH(inode),
3875 .dir_fh = NFS_FH(dir),
3876 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003877 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003879 struct nfs4_link_res res = {
3880 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003881 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct rpc_message msg = {
3884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3885 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003886 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003888 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Trond Myklebust136f2622010-04-16 16:22:49 -04003890 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003891 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003892 goto out;
3893
David Quigley14c43f72013-05-22 12:50:43 -04003894 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3895 if (IS_ERR(res.label)) {
3896 status = PTR_ERR(res.label);
3897 goto out;
3898 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003899 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003900
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003902 if (!status) {
3903 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003904 status = nfs_post_op_update_inode(inode, res.fattr);
3905 if (!status)
3906 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003907 }
David Quigley14c43f72013-05-22 12:50:43 -04003908
3909
3910 nfs4_label_free(res.label);
3911
Trond Myklebust136f2622010-04-16 16:22:49 -04003912out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003913 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 return status;
3915}
3916
3917static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3918{
3919 struct nfs4_exception exception = { };
3920 int err;
3921 do {
3922 err = nfs4_handle_exception(NFS_SERVER(inode),
3923 _nfs4_proc_link(inode, dir, name),
3924 &exception);
3925 } while (exception.retry);
3926 return err;
3927}
3928
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003929struct nfs4_createdata {
3930 struct rpc_message msg;
3931 struct nfs4_create_arg arg;
3932 struct nfs4_create_res res;
3933 struct nfs_fh fh;
3934 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003935 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003936};
3937
3938static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3939 struct qstr *name, struct iattr *sattr, u32 ftype)
3940{
3941 struct nfs4_createdata *data;
3942
3943 data = kzalloc(sizeof(*data), GFP_KERNEL);
3944 if (data != NULL) {
3945 struct nfs_server *server = NFS_SERVER(dir);
3946
David Quigley14c43f72013-05-22 12:50:43 -04003947 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3948 if (IS_ERR(data->label))
3949 goto out_free;
3950
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003951 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3952 data->msg.rpc_argp = &data->arg;
3953 data->msg.rpc_resp = &data->res;
3954 data->arg.dir_fh = NFS_FH(dir);
3955 data->arg.server = server;
3956 data->arg.name = name;
3957 data->arg.attrs = sattr;
3958 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003959 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003960 data->res.server = server;
3961 data->res.fh = &data->fh;
3962 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003963 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003964 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003965 }
3966 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003967out_free:
3968 kfree(data);
3969 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003970}
3971
3972static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3973{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003974 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003975 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003976 if (status == 0) {
3977 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003978 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003979 }
3980 return status;
3981}
3982
3983static void nfs4_free_createdata(struct nfs4_createdata *data)
3984{
David Quigley14c43f72013-05-22 12:50:43 -04003985 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003986 kfree(data);
3987}
3988
Chuck Lever4f390c12006-08-22 20:06:22 -04003989static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003990 struct page *page, unsigned int len, struct iattr *sattr,
3991 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003993 struct nfs4_createdata *data;
3994 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Chuck Lever94a6d752006-08-22 20:06:23 -04003996 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003997 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003998
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003999 status = -ENOMEM;
4000 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4001 if (data == NULL)
4002 goto out;
4003
4004 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4005 data->arg.u.symlink.pages = &page;
4006 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004007 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004009 status = nfs4_do_create(dir, dentry, data);
4010
4011 nfs4_free_createdata(data);
4012out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 return status;
4014}
4015
Chuck Lever4f390c12006-08-22 20:06:22 -04004016static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004017 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
4019 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004020 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004022
4023 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004026 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4027 trace_nfs4_symlink(dir, &dentry->d_name, err);
4028 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 &exception);
4030 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004031
4032 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return err;
4034}
4035
4036static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004037 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004039 struct nfs4_createdata *data;
4040 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004042 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4043 if (data == NULL)
4044 goto out;
4045
David Quigley1775fd32013-05-22 12:50:42 -04004046 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004047 status = nfs4_do_create(dir, dentry, data);
4048
4049 nfs4_free_createdata(data);
4050out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return status;
4052}
4053
4054static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4055 struct iattr *sattr)
4056{
4057 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004058 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004060
David Quigleyaa9c2662013-05-22 12:50:44 -04004061 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4062
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004063 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004065 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4066 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 &exception);
4069 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004070 nfs4_label_release_security(label);
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return err;
4073}
4074
4075static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004076 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
David Howells2b0143b2015-03-17 22:25:59 +00004078 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 struct nfs4_readdir_arg args = {
4080 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004081 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 .pgbase = 0,
4083 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004084 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004085 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 };
4087 struct nfs4_readdir_res res;
4088 struct rpc_message msg = {
4089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4090 .rpc_argp = &args,
4091 .rpc_resp = &res,
4092 .rpc_cred = cred,
4093 };
4094 int status;
4095
Al Viro6de14722013-09-16 10:53:17 -04004096 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4097 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004098 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004099 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004101 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 -05004102 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004103 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004104 status += args.pgbase;
4105 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004106
4107 nfs_invalidate_atime(dir);
4108
Harvey Harrison3110ff82008-05-02 13:42:44 -07004109 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 return status;
4111}
4112
4113static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004114 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
4116 struct nfs4_exception exception = { };
4117 int err;
4118 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004119 err = _nfs4_proc_readdir(dentry, cred, cookie,
4120 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004121 trace_nfs4_readdir(d_inode(dentry), err);
4122 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 &exception);
4124 } while (exception.retry);
4125 return err;
4126}
4127
4128static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004129 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004131 struct nfs4_createdata *data;
4132 int mode = sattr->ia_mode;
4133 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004135 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4136 if (data == NULL)
4137 goto out;
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004140 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004142 data->arg.ftype = NF4BLK;
4143 data->arg.u.device.specdata1 = MAJOR(rdev);
4144 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
4146 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004147 data->arg.ftype = NF4CHR;
4148 data->arg.u.device.specdata1 = MAJOR(rdev);
4149 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004150 } else if (!S_ISSOCK(mode)) {
4151 status = -EINVAL;
4152 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
David Quigley1775fd32013-05-22 12:50:42 -04004154
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004156 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004157out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004158 nfs4_free_createdata(data);
4159out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return status;
4161}
4162
4163static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4164 struct iattr *sattr, dev_t rdev)
4165{
4166 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004167 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004169
David Quigleyaa9c2662013-05-22 12:50:44 -04004170 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4171
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004172 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004174 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4175 trace_nfs4_mknod(dir, &dentry->d_name, err);
4176 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 &exception);
4178 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004179
4180 nfs4_label_release_security(label);
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return err;
4183}
4184
4185static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4186 struct nfs_fsstat *fsstat)
4187{
4188 struct nfs4_statfs_arg args = {
4189 .fh = fhandle,
4190 .bitmask = server->attr_bitmask,
4191 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004192 struct nfs4_statfs_res res = {
4193 .fsstat = fsstat,
4194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 struct rpc_message msg = {
4196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4197 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004198 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 };
4200
Trond Myklebust0e574af2005-10-27 22:12:38 -04004201 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004202 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
4205static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4206{
4207 struct nfs4_exception exception = { };
4208 int err;
4209 do {
4210 err = nfs4_handle_exception(server,
4211 _nfs4_proc_statfs(server, fhandle, fsstat),
4212 &exception);
4213 } while (exception.retry);
4214 return err;
4215}
4216
4217static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4218 struct nfs_fsinfo *fsinfo)
4219{
4220 struct nfs4_fsinfo_arg args = {
4221 .fh = fhandle,
4222 .bitmask = server->attr_bitmask,
4223 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004224 struct nfs4_fsinfo_res res = {
4225 .fsinfo = fsinfo,
4226 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 struct rpc_message msg = {
4228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4229 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004230 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 };
4232
Bryan Schumaker7c513052011-03-24 17:12:24 +00004233 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234}
4235
4236static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4237{
4238 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004239 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 int err;
4241
4242 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004243 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004244 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004245 if (err == 0) {
4246 struct nfs_client *clp = server->nfs_client;
4247
4248 spin_lock(&clp->cl_lock);
4249 clp->cl_lease_time = fsinfo->lease_time * HZ;
4250 clp->cl_last_renewal = now;
4251 spin_unlock(&clp->cl_lock);
4252 break;
4253 }
4254 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 } while (exception.retry);
4256 return err;
4257}
4258
4259static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4260{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004261 int error;
4262
Trond Myklebust0e574af2005-10-27 22:12:38 -04004263 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004264 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004265 if (error == 0) {
4266 /* block layout checks this! */
4267 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004268 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004269 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004270
4271 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
4274static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4275 struct nfs_pathconf *pathconf)
4276{
4277 struct nfs4_pathconf_arg args = {
4278 .fh = fhandle,
4279 .bitmask = server->attr_bitmask,
4280 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004281 struct nfs4_pathconf_res res = {
4282 .pathconf = pathconf,
4283 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 struct rpc_message msg = {
4285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4286 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004287 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 };
4289
4290 /* None of the pathconf attributes are mandatory to implement */
4291 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4292 memset(pathconf, 0, sizeof(*pathconf));
4293 return 0;
4294 }
4295
Trond Myklebust0e574af2005-10-27 22:12:38 -04004296 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004297 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
4300static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4301 struct nfs_pathconf *pathconf)
4302{
4303 struct nfs4_exception exception = { };
4304 int err;
4305
4306 do {
4307 err = nfs4_handle_exception(server,
4308 _nfs4_proc_pathconf(server, fhandle, pathconf),
4309 &exception);
4310 } while (exception.retry);
4311 return err;
4312}
4313
Trond Myklebust5521abf2013-03-16 20:54:34 -04004314int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004315 const struct nfs_open_context *ctx,
4316 const struct nfs_lock_context *l_ctx,
4317 fmode_t fmode)
4318{
4319 const struct nfs_lockowner *lockowner = NULL;
4320
4321 if (l_ctx != NULL)
4322 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004323 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004324}
4325EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4326
Trond Myklebust5521abf2013-03-16 20:54:34 -04004327static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4328 const struct nfs_open_context *ctx,
4329 const struct nfs_lock_context *l_ctx,
4330 fmode_t fmode)
4331{
4332 nfs4_stateid current_stateid;
4333
Trond Myklebuste1253be2014-03-05 08:44:23 -05004334 /* If the current stateid represents a lost lock, then exit */
4335 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4336 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004337 return nfs4_stateid_match(stateid, &current_stateid);
4338}
4339
4340static bool nfs4_error_stateid_expired(int err)
4341{
4342 switch (err) {
4343 case -NFS4ERR_DELEG_REVOKED:
4344 case -NFS4ERR_ADMIN_REVOKED:
4345 case -NFS4ERR_BAD_STATEID:
4346 case -NFS4ERR_STALE_STATEID:
4347 case -NFS4ERR_OLD_STATEID:
4348 case -NFS4ERR_OPENMODE:
4349 case -NFS4ERR_EXPIRED:
4350 return true;
4351 }
4352 return false;
4353}
4354
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004355void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004356{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004357 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004358}
4359
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004360static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004362 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004364 trace_nfs4_read(hdr, task->tk_status);
4365 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004366 hdr->args.context->state,
4367 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004368 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004369 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004371
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004372 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004374 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376}
4377
Trond Myklebust5521abf2013-03-16 20:54:34 -04004378static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004379 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004380{
4381
4382 if (!nfs4_error_stateid_expired(task->tk_status) ||
4383 nfs4_stateid_is_current(&args->stateid,
4384 args->context,
4385 args->lock_context,
4386 FMODE_READ))
4387 return false;
4388 rpc_restart_call_prepare(task);
4389 return true;
4390}
4391
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004392static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004393{
4394
4395 dprintk("--> %s\n", __func__);
4396
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004397 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004398 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004399 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004400 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004401 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4402 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004403}
4404
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004405static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4406 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004408 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004409 if (!hdr->pgio_done_cb)
4410 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004411 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004412 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413}
4414
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004415static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4416 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004417{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004418 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4419 &hdr->args.seq_args,
4420 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004421 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004422 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004423 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4424 hdr->args.lock_context,
4425 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004426 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004427 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004428 return -EIO;
4429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004432static int nfs4_write_done_cb(struct rpc_task *task,
4433 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004435 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004436
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004437 trace_nfs4_write(hdr, task->tk_status);
4438 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004439 hdr->args.context->state,
4440 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004441 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004442 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004444 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004445 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004446 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004447 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
Trond Myklebust5521abf2013-03-16 20:54:34 -04004451static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004452 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004453{
4454
4455 if (!nfs4_error_stateid_expired(task->tk_status) ||
4456 nfs4_stateid_is_current(&args->stateid,
4457 args->context,
4458 args->lock_context,
4459 FMODE_WRITE))
4460 return false;
4461 rpc_restart_call_prepare(task);
4462 return true;
4463}
4464
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004466{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004467 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004468 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004469 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004470 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004471 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4472 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004473}
4474
Trond Myklebust5a37f852012-04-28 14:55:16 -04004475static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004476bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004477{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004478 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004479 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004480 return false;
4481 /* Otherwise, request attributes if and only if we don't hold
4482 * a delegation
4483 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004484 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004485}
Fred Isamana69aef12011-03-03 15:13:47 +00004486
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004487static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4488 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004490 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004491
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004492 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4493 hdr->args.bitmask = NULL;
4494 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004495 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004496 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004497
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004498 if (!hdr->pgio_done_cb)
4499 hdr->pgio_done_cb = nfs4_write_done_cb;
4500 hdr->res.server = server;
4501 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004503 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004504 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505}
4506
Fred Isaman0b7c0152012-04-20 14:47:39 -04004507static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4508{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004509 nfs4_setup_sequence(NFS_SERVER(data->inode),
4510 &data->args.seq_args,
4511 &data->res.seq_res,
4512 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
4514
Fred Isaman0b7c0152012-04-20 14:47:39 -04004515static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004518
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004519 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004520 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4521 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004522 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004523 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
Fred Isaman0b7c0152012-04-20 14:47:39 -04004528static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004529{
4530 if (!nfs4_sequence_done(task, &data->res.seq_res))
4531 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004532 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004533}
4534
Fred Isaman0b7c0152012-04-20 14:47:39 -04004535static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004537 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004538
Fred Isaman0b7c0152012-04-20 14:47:39 -04004539 if (data->commit_done_cb == NULL)
4540 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004541 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004542 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004543 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004546struct nfs4_renewdata {
4547 struct nfs_client *client;
4548 unsigned long timestamp;
4549};
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551/*
4552 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4553 * standalone procedure for queueing an asynchronous RENEW.
4554 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004555static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004556{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004557 struct nfs4_renewdata *data = calldata;
4558 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004559
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004560 if (atomic_read(&clp->cl_count) > 1)
4561 nfs4_schedule_state_renewal(clp);
4562 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004563 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004564}
4565
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004566static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004568 struct nfs4_renewdata *data = calldata;
4569 struct nfs_client *clp = data->client;
4570 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004572 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004573 switch (task->tk_status) {
4574 case 0:
4575 break;
4576 case -NFS4ERR_LEASE_MOVED:
4577 nfs4_schedule_lease_moved_recovery(clp);
4578 break;
4579 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004580 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004581 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4582 return;
4583 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004584 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004585 return;
4586 }
4587 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004589 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590}
4591
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004592static const struct rpc_call_ops nfs4_renew_ops = {
4593 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004594 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004595};
4596
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004597static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
4599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4601 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004602 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004604 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004606 if (renew_flags == 0)
4607 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004608 if (!atomic_inc_not_zero(&clp->cl_count))
4609 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004610 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004611 if (data == NULL)
4612 return -ENOMEM;
4613 data->client = clp;
4614 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004615 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004616 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004619static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
4621 struct rpc_message msg = {
4622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4623 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004624 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 };
4626 unsigned long now = jiffies;
4627 int status;
4628
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004629 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (status < 0)
4631 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004632 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 return 0;
4634}
4635
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004636static inline int nfs4_server_supports_acls(struct nfs_server *server)
4637{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004638 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004639}
4640
Trond Myklebust21f498c2012-08-24 10:59:25 -04004641/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4642 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004643 * the stack.
4644 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004645#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004646
Neil Hormane9e3d722011-03-04 19:26:03 -05004647static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004648 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004649{
4650 struct page *newpage, **spages;
4651 int rc = 0;
4652 size_t len;
4653 spages = pages;
4654
4655 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004656 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004657 newpage = alloc_page(GFP_KERNEL);
4658
4659 if (newpage == NULL)
4660 goto unwind;
4661 memcpy(page_address(newpage), buf, len);
4662 buf += len;
4663 buflen -= len;
4664 *pages++ = newpage;
4665 rc++;
4666 } while (buflen != 0);
4667
4668 return rc;
4669
4670unwind:
4671 for(; rc > 0; rc--)
4672 __free_page(spages[rc-1]);
4673 return -ENOMEM;
4674}
4675
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004676struct nfs4_cached_acl {
4677 int cached;
4678 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004679 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004680};
4681
4682static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004683{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004684 struct nfs_inode *nfsi = NFS_I(inode);
4685
4686 spin_lock(&inode->i_lock);
4687 kfree(nfsi->nfs4_acl);
4688 nfsi->nfs4_acl = acl;
4689 spin_unlock(&inode->i_lock);
4690}
4691
4692static void nfs4_zap_acl_attr(struct inode *inode)
4693{
4694 nfs4_set_cached_acl(inode, NULL);
4695}
4696
4697static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4698{
4699 struct nfs_inode *nfsi = NFS_I(inode);
4700 struct nfs4_cached_acl *acl;
4701 int ret = -ENOENT;
4702
4703 spin_lock(&inode->i_lock);
4704 acl = nfsi->nfs4_acl;
4705 if (acl == NULL)
4706 goto out;
4707 if (buf == NULL) /* user is just asking for length */
4708 goto out_len;
4709 if (acl->cached == 0)
4710 goto out;
4711 ret = -ERANGE; /* see getxattr(2) man page */
4712 if (acl->len > buflen)
4713 goto out;
4714 memcpy(buf, acl->data, acl->len);
4715out_len:
4716 ret = acl->len;
4717out:
4718 spin_unlock(&inode->i_lock);
4719 return ret;
4720}
4721
Sachin Prabhu5794d212012-04-17 14:36:40 +01004722static 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 +00004723{
4724 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004725 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004726
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004727 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004728 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004729 if (acl == NULL)
4730 goto out;
4731 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004732 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004733 } else {
4734 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4735 if (acl == NULL)
4736 goto out;
4737 acl->cached = 0;
4738 }
4739 acl->len = acl_len;
4740out:
4741 nfs4_set_cached_acl(inode, acl);
4742}
4743
Andy Adamsonbf118a32011-12-07 11:55:27 -05004744/*
4745 * The getxattr API returns the required buffer length when called with a
4746 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4747 * the required buf. On a NULL buf, we send a page of data to the server
4748 * guessing that the ACL request can be serviced by a page. If so, we cache
4749 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4750 * the cache. If not so, we throw away the page, and cache the required
4751 * length. The next getxattr call will then produce another round trip to
4752 * the server, this time with the input buf of the required size.
4753 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004754static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004755{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004756 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004757 struct nfs_getaclargs args = {
4758 .fh = NFS_FH(inode),
4759 .acl_pages = pages,
4760 .acl_len = buflen,
4761 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004762 struct nfs_getaclres res = {
4763 .acl_len = buflen,
4764 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004765 struct rpc_message msg = {
4766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4767 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004768 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004769 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004770 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4771 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004772
Andy Adamsonbf118a32011-12-07 11:55:27 -05004773 /* As long as we're doing a round trip to the server anyway,
4774 * let's be prepared for a page of acl data. */
4775 if (npages == 0)
4776 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004777 if (npages > ARRAY_SIZE(pages))
4778 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004779
Andy Adamsonbf118a32011-12-07 11:55:27 -05004780 for (i = 0; i < npages; i++) {
4781 pages[i] = alloc_page(GFP_KERNEL);
4782 if (!pages[i])
4783 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004784 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004785
4786 /* for decoding across pages */
4787 res.acl_scratch = alloc_page(GFP_KERNEL);
4788 if (!res.acl_scratch)
4789 goto out_free;
4790
Andy Adamsonbf118a32011-12-07 11:55:27 -05004791 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004792
Peng Taode040be2012-01-10 22:42:47 +08004793 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004794 __func__, buf, buflen, npages, args.acl_len);
4795 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4796 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004797 if (ret)
4798 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004799
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004800 /* Handle the case where the passed-in buffer is too short */
4801 if (res.acl_flags & NFS4_ACL_TRUNC) {
4802 /* Did the user only issue a request for the acl length? */
4803 if (buf == NULL)
4804 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004805 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004806 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004807 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004808 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004809 if (buf) {
4810 if (res.acl_len > buflen) {
4811 ret = -ERANGE;
4812 goto out_free;
4813 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004814 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004815 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004816out_ok:
4817 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004818out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004819 for (i = 0; i < npages; i++)
4820 if (pages[i])
4821 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004822 if (res.acl_scratch)
4823 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004824 return ret;
4825}
4826
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004827static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4828{
4829 struct nfs4_exception exception = { };
4830 ssize_t ret;
4831 do {
4832 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004833 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004834 if (ret >= 0)
4835 break;
4836 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4837 } while (exception.retry);
4838 return ret;
4839}
4840
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004841static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4842{
4843 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004844 int ret;
4845
4846 if (!nfs4_server_supports_acls(server))
4847 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004848 ret = nfs_revalidate_inode(server, inode);
4849 if (ret < 0)
4850 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004851 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4852 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004853 ret = nfs4_read_cached_acl(inode, buf, buflen);
4854 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004855 /* -ENOENT is returned if there is no ACL or if there is an ACL
4856 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004857 return ret;
4858 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004859}
4860
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004861static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004862{
4863 struct nfs_server *server = NFS_SERVER(inode);
4864 struct page *pages[NFS4ACL_MAXPAGES];
4865 struct nfs_setaclargs arg = {
4866 .fh = NFS_FH(inode),
4867 .acl_pages = pages,
4868 .acl_len = buflen,
4869 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004870 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004871 struct rpc_message msg = {
4872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4873 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004874 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004875 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004876 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004877 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004878
4879 if (!nfs4_server_supports_acls(server))
4880 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004881 if (npages > ARRAY_SIZE(pages))
4882 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004883 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004884 if (i < 0)
4885 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004886 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004887 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004888
4889 /*
4890 * Free each page after tx, so the only ref left is
4891 * held by the network stack
4892 */
4893 for (; i > 0; i--)
4894 put_page(pages[i-1]);
4895
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004896 /*
4897 * Acl update can result in inode attribute update.
4898 * so mark the attribute cache invalid.
4899 */
4900 spin_lock(&inode->i_lock);
4901 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4902 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004903 nfs_access_zap_cache(inode);
4904 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004905 return ret;
4906}
4907
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004908static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4909{
4910 struct nfs4_exception exception = { };
4911 int err;
4912 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004913 err = __nfs4_proc_set_acl(inode, buf, buflen);
4914 trace_nfs4_set_acl(inode, err);
4915 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004916 &exception);
4917 } while (exception.retry);
4918 return err;
4919}
4920
David Quigleyaa9c2662013-05-22 12:50:44 -04004921#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4922static int _nfs4_get_security_label(struct inode *inode, void *buf,
4923 size_t buflen)
4924{
4925 struct nfs_server *server = NFS_SERVER(inode);
4926 struct nfs_fattr fattr;
4927 struct nfs4_label label = {0, 0, buflen, buf};
4928
4929 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004930 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004931 .fh = NFS_FH(inode),
4932 .bitmask = bitmask,
4933 };
4934 struct nfs4_getattr_res res = {
4935 .fattr = &fattr,
4936 .label = &label,
4937 .server = server,
4938 };
4939 struct rpc_message msg = {
4940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004941 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004942 .rpc_resp = &res,
4943 };
4944 int ret;
4945
4946 nfs_fattr_init(&fattr);
4947
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004948 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004949 if (ret)
4950 return ret;
4951 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4952 return -ENOENT;
4953 if (buflen < label.len)
4954 return -ERANGE;
4955 return 0;
4956}
4957
4958static int nfs4_get_security_label(struct inode *inode, void *buf,
4959 size_t buflen)
4960{
4961 struct nfs4_exception exception = { };
4962 int err;
4963
4964 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4965 return -EOPNOTSUPP;
4966
4967 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004968 err = _nfs4_get_security_label(inode, buf, buflen);
4969 trace_nfs4_get_security_label(inode, err);
4970 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004971 &exception);
4972 } while (exception.retry);
4973 return err;
4974}
4975
4976static int _nfs4_do_set_security_label(struct inode *inode,
4977 struct nfs4_label *ilabel,
4978 struct nfs_fattr *fattr,
4979 struct nfs4_label *olabel)
4980{
4981
4982 struct iattr sattr = {0};
4983 struct nfs_server *server = NFS_SERVER(inode);
4984 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004985 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004986 .fh = NFS_FH(inode),
4987 .iap = &sattr,
4988 .server = server,
4989 .bitmask = bitmask,
4990 .label = ilabel,
4991 };
4992 struct nfs_setattrres res = {
4993 .fattr = fattr,
4994 .label = olabel,
4995 .server = server,
4996 };
4997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004999 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005000 .rpc_resp = &res,
5001 };
5002 int status;
5003
Jeff Layton12207f62013-11-01 10:49:32 -04005004 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005005
Jeff Layton12207f62013-11-01 10:49:32 -04005006 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005007 if (status)
5008 dprintk("%s failed: %d\n", __func__, status);
5009
5010 return status;
5011}
5012
5013static int nfs4_do_set_security_label(struct inode *inode,
5014 struct nfs4_label *ilabel,
5015 struct nfs_fattr *fattr,
5016 struct nfs4_label *olabel)
5017{
5018 struct nfs4_exception exception = { };
5019 int err;
5020
5021 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005022 err = _nfs4_do_set_security_label(inode, ilabel,
5023 fattr, olabel);
5024 trace_nfs4_set_security_label(inode, err);
5025 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005026 &exception);
5027 } while (exception.retry);
5028 return err;
5029}
5030
5031static int
Al Viro59301222016-05-27 10:19:30 -04005032nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005033{
5034 struct nfs4_label ilabel, *olabel = NULL;
5035 struct nfs_fattr fattr;
5036 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005037 int status;
5038
5039 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5040 return -EOPNOTSUPP;
5041
5042 nfs_fattr_init(&fattr);
5043
5044 ilabel.pi = 0;
5045 ilabel.lfs = 0;
5046 ilabel.label = (char *)buf;
5047 ilabel.len = buflen;
5048
5049 cred = rpc_lookup_cred();
5050 if (IS_ERR(cred))
5051 return PTR_ERR(cred);
5052
5053 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5054 if (IS_ERR(olabel)) {
5055 status = -PTR_ERR(olabel);
5056 goto out;
5057 }
5058
5059 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5060 if (status == 0)
5061 nfs_setsecurity(inode, &fattr, olabel);
5062
5063 nfs4_label_free(olabel);
5064out:
5065 put_rpccred(cred);
5066 return status;
5067}
5068#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5069
5070
Chuck Leverf0920752012-05-21 22:45:41 -04005071static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5072 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005073{
5074 __be32 verf[2];
5075
Chuck Lever2c820d92012-05-21 22:45:33 -04005076 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5077 /* An impossible timestamp guarantees this value
5078 * will never match a generated boot time. */
5079 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005080 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005081 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005082 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005083 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5084 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005085 }
Chuck Levercd937102012-03-02 17:14:31 -05005086 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5087}
5088
Jeff Laytona3192682015-06-09 19:43:59 -04005089static int
5090nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005091{
Jeff Laytona3192682015-06-09 19:43:59 -04005092 size_t len;
5093 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005094
Trond Myklebustceb3a162015-01-03 15:16:04 -05005095 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005096 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005097
Jeff Laytona3192682015-06-09 19:43:59 -04005098 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005099 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005100 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5101 1 +
5102 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5103 1;
5104 rcu_read_unlock();
5105
5106 if (len > NFS4_OPAQUE_LIMIT + 1)
5107 return -EINVAL;
5108
5109 /*
5110 * Since this string is allocated at mount time, and held until the
5111 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5112 * about a memory-reclaim deadlock.
5113 */
5114 str = kmalloc(len, GFP_KERNEL);
5115 if (!str)
5116 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005117
Chuck Levere984a552012-09-14 17:24:21 -04005118 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005119 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005120 clp->cl_ipaddr,
5121 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5122 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005123 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005124
Jeff Laytona3192682015-06-09 19:43:59 -04005125 clp->cl_owner_id = str;
5126 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005127}
5128
Jeff Layton873e3852015-06-09 19:44:00 -04005129static int
5130nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005131{
Jeff Layton873e3852015-06-09 19:44:00 -04005132 size_t len;
5133 char *str;
5134
5135 len = 10 + 10 + 1 + 10 + 1 +
5136 strlen(nfs4_client_id_uniquifier) + 1 +
5137 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5138
5139 if (len > NFS4_OPAQUE_LIMIT + 1)
5140 return -EINVAL;
5141
5142 /*
5143 * Since this string is allocated at mount time, and held until the
5144 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5145 * about a memory-reclaim deadlock.
5146 */
5147 str = kmalloc(len, GFP_KERNEL);
5148 if (!str)
5149 return -ENOMEM;
5150
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005151 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005152 clp->rpc_ops->version, clp->cl_minorversion,
5153 nfs4_client_id_uniquifier,
5154 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005155 clp->cl_owner_id = str;
5156 return 0;
5157}
5158
5159static int
5160nfs4_init_uniform_client_string(struct nfs_client *clp)
5161{
Jeff Layton873e3852015-06-09 19:44:00 -04005162 size_t len;
5163 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005164
5165 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005166 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005167
5168 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005169 return nfs4_init_uniquifier_client_string(clp);
5170
5171 len = 10 + 10 + 1 + 10 + 1 +
5172 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5173
5174 if (len > NFS4_OPAQUE_LIMIT + 1)
5175 return -EINVAL;
5176
5177 /*
5178 * Since this string is allocated at mount time, and held until the
5179 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5180 * about a memory-reclaim deadlock.
5181 */
5182 str = kmalloc(len, GFP_KERNEL);
5183 if (!str)
5184 return -ENOMEM;
5185
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005186 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005187 clp->rpc_ops->version, clp->cl_minorversion,
5188 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005189 clp->cl_owner_id = str;
5190 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005191}
5192
Chuck Lever706cb8d2014-03-12 12:51:47 -04005193/*
5194 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5195 * services. Advertise one based on the address family of the
5196 * clientaddr.
5197 */
5198static unsigned int
5199nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5200{
5201 if (strchr(clp->cl_ipaddr, ':') != NULL)
5202 return scnprintf(buf, len, "tcp6");
5203 else
5204 return scnprintf(buf, len, "tcp");
5205}
5206
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005207static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5208{
5209 struct nfs4_setclientid *sc = calldata;
5210
5211 if (task->tk_status == 0)
5212 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5213}
5214
5215static const struct rpc_call_ops nfs4_setclientid_ops = {
5216 .rpc_call_done = nfs4_setclientid_done,
5217};
5218
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005219/**
5220 * nfs4_proc_setclientid - Negotiate client ID
5221 * @clp: state data structure
5222 * @program: RPC program for NFSv4 callback service
5223 * @port: IP port number for NFS4 callback service
5224 * @cred: RPC credential to use for this call
5225 * @res: where to place the result
5226 *
5227 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5228 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005229int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5230 unsigned short port, struct rpc_cred *cred,
5231 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
5233 nfs4_verifier sc_verifier;
5234 struct nfs4_setclientid setclientid = {
5235 .sc_verifier = &sc_verifier,
5236 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005237 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 };
5239 struct rpc_message msg = {
5240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5241 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005242 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005243 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005245 struct rpc_task *task;
5246 struct rpc_task_setup task_setup_data = {
5247 .rpc_client = clp->cl_rpcclient,
5248 .rpc_message = &msg,
5249 .callback_ops = &nfs4_setclientid_ops,
5250 .callback_data = &setclientid,
5251 .flags = RPC_TASK_TIMEOUT,
5252 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005253 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Chuck Leverde734832012-07-11 16:30:50 -04005255 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005256 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005257
5258 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5259 status = nfs4_init_uniform_client_string(clp);
5260 else
Jeff Laytona3192682015-06-09 19:43:59 -04005261 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005262
5263 if (status)
5264 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005265
Chuck Leverde734832012-07-11 16:30:50 -04005266 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005267 setclientid.sc_netid_len =
5268 nfs4_init_callback_netid(clp,
5269 setclientid.sc_netid,
5270 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005271 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005272 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 clp->cl_ipaddr, port >> 8, port & 255);
5274
Jeff Layton3a6bb732015-06-09 19:43:57 -04005275 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005276 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005277 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005278 task = rpc_run_task(&task_setup_data);
5279 if (IS_ERR(task)) {
5280 status = PTR_ERR(task);
5281 goto out;
5282 }
5283 status = task->tk_status;
5284 if (setclientid.sc_cred) {
5285 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5286 put_rpccred(setclientid.sc_cred);
5287 }
5288 rpc_put_task(task);
5289out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005290 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005291 dprintk("NFS reply setclientid: %d\n", status);
5292 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293}
5294
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005295/**
5296 * nfs4_proc_setclientid_confirm - Confirm client ID
5297 * @clp: state data structure
5298 * @res: result of a previous SETCLIENTID
5299 * @cred: RPC credential to use for this call
5300 *
5301 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5302 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005303int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005304 struct nfs4_setclientid_res *arg,
5305 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 struct rpc_message msg = {
5308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005309 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005310 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 int status;
5313
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005314 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5315 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5316 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005317 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005318 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005319 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 return status;
5321}
5322
Trond Myklebustfe650402006-01-03 09:55:18 +01005323struct nfs4_delegreturndata {
5324 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005325 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005326 struct nfs_fh fh;
5327 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005328 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005329 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005330 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005331 struct inode *inode;
5332 bool roc;
5333 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005334};
5335
Trond Myklebustfe650402006-01-03 09:55:18 +01005336static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5337{
5338 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005339
Trond Myklebust14516c32010-07-31 14:29:06 -04005340 if (!nfs4_sequence_done(task, &data->res.seq_res))
5341 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005342
Trond Myklebustca8acf82013-08-13 10:36:56 -04005343 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005344 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005345 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005346 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005347 case -NFS4ERR_ADMIN_REVOKED:
5348 case -NFS4ERR_DELEG_REVOKED:
5349 case -NFS4ERR_BAD_STATEID:
5350 case -NFS4ERR_OLD_STATEID:
5351 case -NFS4ERR_STALE_STATEID:
5352 case -NFS4ERR_EXPIRED:
5353 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005354 if (data->roc)
5355 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005356 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005357 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005358 if (nfs4_async_handle_error(task, data->res.server,
5359 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005360 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005361 return;
5362 }
5363 }
5364 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005365}
5366
5367static void nfs4_delegreturn_release(void *calldata)
5368{
Peng Tao039b7562014-07-03 13:05:02 +08005369 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005370 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005371
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005372 if (inode) {
5373 if (data->roc)
5374 pnfs_roc_release(inode);
5375 nfs_iput_and_deactive(inode);
5376 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005377 kfree(calldata);
5378}
5379
Andy Adamson938e1012009-04-01 09:22:28 -04005380static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5381{
5382 struct nfs4_delegreturndata *d_data;
5383
5384 d_data = (struct nfs4_delegreturndata *)data;
5385
Peng Tao500d7012015-09-22 11:35:22 +08005386 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5387 return;
5388
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005389 if (d_data->roc)
5390 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005391
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005392 nfs4_setup_sequence(d_data->res.server,
5393 &d_data->args.seq_args,
5394 &d_data->res.seq_res,
5395 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005396}
Andy Adamson938e1012009-04-01 09:22:28 -04005397
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005398static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005399 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005400 .rpc_call_done = nfs4_delegreturn_done,
5401 .rpc_release = nfs4_delegreturn_release,
5402};
5403
Trond Myklebuste6f81072008-01-24 18:14:34 -05005404static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005405{
5406 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005407 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005408 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005409 struct rpc_message msg = {
5410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5411 .rpc_cred = cred,
5412 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005413 struct rpc_task_setup task_setup_data = {
5414 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005415 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005416 .callback_ops = &nfs4_delegreturn_ops,
5417 .flags = RPC_TASK_ASYNC,
5418 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005419 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005420
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005421 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005422 if (data == NULL)
5423 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005424 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005425
5426 nfs4_state_protect(server->nfs_client,
5427 NFS_SP4_MACH_CRED_CLEANUP,
5428 &task_setup_data.rpc_client, &msg);
5429
Trond Myklebustfe650402006-01-03 09:55:18 +01005430 data->args.fhandle = &data->fh;
5431 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005432 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005433 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005434 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005435 data->res.fattr = &data->fattr;
5436 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005437 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005438 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005439 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005440 data->inode = nfs_igrab_and_active(inode);
5441 if (data->inode)
5442 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005443
Trond Myklebustc970aa82007-07-14 15:39:59 -04005444 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005445 msg.rpc_argp = &data->args;
5446 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005447 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005448 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005449 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005450 if (!issync)
5451 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005452 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005453 if (status != 0)
5454 goto out;
5455 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005456 if (status == 0)
5457 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5458 else
5459 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005460out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005461 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005462 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463}
5464
Trond Myklebuste6f81072008-01-24 18:14:34 -05005465int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466{
5467 struct nfs_server *server = NFS_SERVER(inode);
5468 struct nfs4_exception exception = { };
5469 int err;
5470 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005471 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005472 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 switch (err) {
5474 case -NFS4ERR_STALE_STATEID:
5475 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 case 0:
5477 return 0;
5478 }
5479 err = nfs4_handle_exception(server, err, &exception);
5480 } while (exception.retry);
5481 return err;
5482}
5483
5484#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5485#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5486
5487/*
5488 * sleep, with exponential backoff, and retry the LOCK operation.
5489 */
5490static unsigned long
5491nfs4_set_lock_task_retry(unsigned long timeout)
5492{
Colin Cross416ad3c2013-05-06 23:50:06 +00005493 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 timeout <<= 1;
5495 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5496 return NFS4_LOCK_MAXTIMEOUT;
5497 return timeout;
5498}
5499
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5501{
5502 struct inode *inode = state->inode;
5503 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005504 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005505 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005507 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005509 struct nfs_lockt_res res = {
5510 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 };
5512 struct rpc_message msg = {
5513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5514 .rpc_argp = &arg,
5515 .rpc_resp = &res,
5516 .rpc_cred = state->owner->so_cred,
5517 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 struct nfs4_lock_state *lsp;
5519 int status;
5520
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005521 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005522 status = nfs4_set_lock_state(state, request);
5523 if (status != 0)
5524 goto out;
5525 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005526 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005527 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005528 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005529 switch (status) {
5530 case 0:
5531 request->fl_type = F_UNLCK;
5532 break;
5533 case -NFS4ERR_DENIED:
5534 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005536 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005537 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005538out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 return status;
5540}
5541
5542static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5543{
5544 struct nfs4_exception exception = { };
5545 int err;
5546
5547 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005548 err = _nfs4_proc_getlk(state, cmd, request);
5549 trace_nfs4_get_lock(request, state, cmd, err);
5550 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 &exception);
5552 } while (exception.retry);
5553 return err;
5554}
5555
Jeff Layton83bfff22015-07-11 06:43:03 -04005556static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557{
Benjamin Coddington4f656362015-10-22 13:38:14 -04005558 return locks_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559}
5560
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005561struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005562 struct nfs_locku_args arg;
5563 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005564 struct nfs4_lock_state *lsp;
5565 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005566 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005567 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005568 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005569};
5570
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005571static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5572 struct nfs_open_context *ctx,
5573 struct nfs4_lock_state *lsp,
5574 struct nfs_seqid *seqid)
5575{
5576 struct nfs4_unlockdata *p;
5577 struct inode *inode = lsp->ls_state->inode;
5578
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005579 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005580 if (p == NULL)
5581 return NULL;
5582 p->arg.fh = NFS_FH(inode);
5583 p->arg.fl = &p->fl;
5584 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005585 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005586 p->lsp = lsp;
5587 atomic_inc(&lsp->ls_count);
5588 /* Ensure we don't close file until we're done freeing locks! */
5589 p->ctx = get_nfs_open_context(ctx);
5590 memcpy(&p->fl, fl, sizeof(p->fl));
5591 p->server = NFS_SERVER(inode);
5592 return p;
5593}
5594
Trond Myklebust06f814a2006-01-03 09:55:07 +01005595static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005596{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005597 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005598 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005599 nfs4_put_lock_state(calldata->lsp);
5600 put_nfs_open_context(calldata->ctx);
5601 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005602}
5603
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005604static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005606 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005607
Trond Myklebust14516c32010-07-31 14:29:06 -04005608 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5609 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005610 switch (task->tk_status) {
5611 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005612 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005613 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005614 if (nfs4_update_lock_stateid(calldata->lsp,
5615 &calldata->res.stateid))
5616 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005617 case -NFS4ERR_BAD_STATEID:
5618 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005619 case -NFS4ERR_STALE_STATEID:
5620 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005621 if (!nfs4_stateid_match(&calldata->arg.stateid,
5622 &calldata->lsp->ls_stateid))
5623 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005624 break;
5625 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005626 if (nfs4_async_handle_error(task, calldata->server,
5627 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005628 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005629 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005630 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005631}
5632
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005633static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005634{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005635 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005637 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005638 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005639 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005640 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005641 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005642 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005643 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005644 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005645 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005646 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005647 &calldata->res.seq_res,
5648 task) != 0)
5649 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005650 return;
5651out_no_action:
5652 task->tk_action = NULL;
5653out_wait:
5654 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655}
5656
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005657static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005658 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005659 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005660 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005661};
5662
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005663static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5664 struct nfs_open_context *ctx,
5665 struct nfs4_lock_state *lsp,
5666 struct nfs_seqid *seqid)
5667{
5668 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5671 .rpc_cred = ctx->cred,
5672 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005673 struct rpc_task_setup task_setup_data = {
5674 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005675 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005676 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005677 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005678 .flags = RPC_TASK_ASYNC,
5679 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005680
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005681 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5682 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5683
Frank Filz137d6ac2007-07-09 15:32:29 -07005684 /* Ensure this is an unlock - when canceling a lock, the
5685 * canceled lock is passed in, and it won't be an unlock.
5686 */
5687 fl->fl_type = F_UNLCK;
5688
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005689 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5690 if (data == NULL) {
5691 nfs_free_seqid(seqid);
5692 return ERR_PTR(-ENOMEM);
5693 }
5694
Chuck Levera9c92d62013-08-09 12:48:18 -04005695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005696 msg.rpc_argp = &data->arg;
5697 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005698 task_setup_data.callback_data = data;
5699 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005700}
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5703{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005704 struct inode *inode = state->inode;
5705 struct nfs4_state_owner *sp = state->owner;
5706 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005707 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005708 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005709 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005710 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005711 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005712 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
Trond Myklebust9b073572006-06-29 16:38:34 -04005714 status = nfs4_set_lock_state(state, request);
5715 /* Unlock _before_ we do the RPC call */
5716 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005717 /* Exclude nfs_delegation_claim_locks() */
5718 mutex_lock(&sp->so_delegreturn_mutex);
5719 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005720 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005721 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005722 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005723 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005724 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005725 }
5726 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005727 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005728 if (status != 0)
5729 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005730 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005731 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005732 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5733 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005734 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5735 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005736 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005737 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005738 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005739 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005740 status = PTR_ERR(task);
5741 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005742 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005743 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005744 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005745out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005746 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005747 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005748 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005751struct nfs4_lockdata {
5752 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005753 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005754 struct nfs4_lock_state *lsp;
5755 struct nfs_open_context *ctx;
5756 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005757 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005758 int rpc_status;
5759 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005760 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005761};
5762
5763static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005764 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5765 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005766{
5767 struct nfs4_lockdata *p;
5768 struct inode *inode = lsp->ls_state->inode;
5769 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005770 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005771
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005772 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005773 if (p == NULL)
5774 return NULL;
5775
5776 p->arg.fh = NFS_FH(inode);
5777 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005778 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005779 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005780 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005781 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5782 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005783 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005784 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005785 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005786 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005787 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005788 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005789 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005790 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005791 atomic_inc(&lsp->ls_count);
5792 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005793 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005794 memcpy(&p->fl, fl, sizeof(p->fl));
5795 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005796out_free_seqid:
5797 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005798out_free:
5799 kfree(p);
5800 return NULL;
5801}
5802
5803static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5804{
5805 struct nfs4_lockdata *data = calldata;
5806 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
Harvey Harrison3110ff82008-05-02 13:42:44 -07005808 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005809 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005810 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005811 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005812 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005813 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005814 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005815 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005816 nfs4_stateid_copy(&data->arg.open_stateid,
5817 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005818 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005819 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005820 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005821 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005822 nfs4_stateid_copy(&data->arg.lock_stateid,
5823 &data->lsp->ls_stateid);
5824 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005825 if (!nfs4_valid_open_stateid(state)) {
5826 data->rpc_status = -EBADF;
5827 task->tk_action = NULL;
5828 goto out_release_open_seqid;
5829 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005830 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005831 if (nfs4_setup_sequence(data->server,
5832 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005833 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005834 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005835 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005836out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005837 nfs_release_seqid(data->arg.open_seqid);
5838out_release_lock_seqid:
5839 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005840out_wait:
5841 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005842 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005843}
5844
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005845static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5846{
5847 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005848 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005849
Harvey Harrison3110ff82008-05-02 13:42:44 -07005850 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005851
Trond Myklebust14516c32010-07-31 14:29:06 -04005852 if (!nfs4_sequence_done(task, &data->res.seq_res))
5853 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005854
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005855 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005856 switch (task->tk_status) {
5857 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005858 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005859 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005860 if (data->arg.new_lock) {
5861 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005862 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005863 rpc_restart_call_prepare(task);
5864 break;
5865 }
5866 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005867 if (data->arg.new_lock_owner != 0) {
5868 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5869 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5870 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5871 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5872 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005873 break;
5874 case -NFS4ERR_BAD_STATEID:
5875 case -NFS4ERR_OLD_STATEID:
5876 case -NFS4ERR_STALE_STATEID:
5877 case -NFS4ERR_EXPIRED:
5878 if (data->arg.new_lock_owner != 0) {
5879 if (!nfs4_stateid_match(&data->arg.open_stateid,
5880 &lsp->ls_state->open_stateid))
5881 rpc_restart_call_prepare(task);
5882 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5883 &lsp->ls_stateid))
5884 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005885 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005886 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005887}
5888
5889static void nfs4_lock_release(void *calldata)
5890{
5891 struct nfs4_lockdata *data = calldata;
5892
Harvey Harrison3110ff82008-05-02 13:42:44 -07005893 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005894 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895 if (data->cancelled != 0) {
5896 struct rpc_task *task;
5897 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5898 data->arg.lock_seqid);
5899 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005900 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005901 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005902 } else
5903 nfs_free_seqid(data->arg.lock_seqid);
5904 nfs4_put_lock_state(data->lsp);
5905 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005906 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005907 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005908 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005909}
5910
5911static const struct rpc_call_ops nfs4_lock_ops = {
5912 .rpc_call_prepare = nfs4_lock_prepare,
5913 .rpc_call_done = nfs4_lock_done,
5914 .rpc_release = nfs4_lock_release,
5915};
5916
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005917static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5918{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005919 switch (error) {
5920 case -NFS4ERR_ADMIN_REVOKED:
5921 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005922 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005923 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005924 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005925 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005926 break;
5927 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005928 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005929 case -NFS4ERR_EXPIRED:
5930 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005931 };
5932}
5933
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005934static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935{
5936 struct nfs4_lockdata *data;
5937 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005938 struct rpc_message msg = {
5939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5940 .rpc_cred = state->owner->so_cred,
5941 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005944 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005945 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005946 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005947 .flags = RPC_TASK_ASYNC,
5948 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949 int ret;
5950
Harvey Harrison3110ff82008-05-02 13:42:44 -07005951 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005952 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005953 fl->fl_u.nfs4_fl.owner,
5954 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005955 if (data == NULL)
5956 return -ENOMEM;
5957 if (IS_SETLKW(cmd))
5958 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005959 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005960 msg.rpc_argp = &data->arg;
5961 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005962 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005963 if (recovery_type > NFS_LOCK_NEW) {
5964 if (recovery_type == NFS_LOCK_RECLAIM)
5965 data->arg.reclaim = NFS_LOCK_RECLAIM;
5966 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005967 } else
5968 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005969 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005970 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005971 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005972 ret = nfs4_wait_for_completion_rpc_task(task);
5973 if (ret == 0) {
5974 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005975 if (ret)
5976 nfs4_handle_setlk_error(data->server, data->lsp,
5977 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005978 } else
5979 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005980 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005981 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005982 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984}
5985
5986static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5987{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005988 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005989 struct nfs4_exception exception = {
5990 .inode = state->inode,
5991 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005992 int err;
5993
5994 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005995 /* Cache the lock if possible... */
5996 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5997 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005998 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005999 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006000 break;
6001 nfs4_handle_exception(server, err, &exception);
6002 } while (exception.retry);
6003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004}
6005
6006static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6007{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006008 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006009 struct nfs4_exception exception = {
6010 .inode = state->inode,
6011 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006012 int err;
6013
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006014 err = nfs4_set_lock_state(state, request);
6015 if (err != 0)
6016 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006017 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006018 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6019 return 0;
6020 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006021 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006022 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6023 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006024 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006025 switch (err) {
6026 default:
6027 goto out;
6028 case -NFS4ERR_GRACE:
6029 case -NFS4ERR_DELAY:
6030 nfs4_handle_exception(server, err, &exception);
6031 err = 0;
6032 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006033 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006034out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006035 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036}
6037
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006038#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006039/**
6040 * nfs41_check_expired_locks - possibly free a lock stateid
6041 *
6042 * @state: NFSv4 state for an inode
6043 *
6044 * Returns NFS_OK if recovery for this stateid is now finished.
6045 * Otherwise a negative NFS4ERR value is returned.
6046 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006047static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006048{
Chuck Levereb64cf92012-07-11 16:30:05 -04006049 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006050 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006051 struct nfs_server *server = NFS_SERVER(state->inode);
6052
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006053 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006054 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006055 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6056
6057 status = nfs41_test_stateid(server,
6058 &lsp->ls_stateid,
6059 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006060 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006061 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006062 /* Free the stateid unless the server
6063 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006064 if (status != -NFS4ERR_BAD_STATEID)
6065 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006066 &lsp->ls_stateid,
6067 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006068 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006069 ret = status;
6070 }
6071 }
6072 };
6073
6074 return ret;
6075}
6076
6077static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6078{
6079 int status = NFS_OK;
6080
6081 if (test_bit(LK_STATE_IN_USE, &state->flags))
6082 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006083 if (status != NFS_OK)
6084 status = nfs4_lock_expired(state, request);
6085 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006086}
6087#endif
6088
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6090{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006091 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006092 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006093 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006094 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006096 if ((fl_flags & FL_POSIX) &&
6097 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6098 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006099 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006100 status = nfs4_set_lock_state(state, request);
6101 if (status != 0)
6102 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006104 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006105 if (status < 0)
6106 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006107 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006108 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006109 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006110 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006111 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006112 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006113 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006114 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006115 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006116 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006117 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006118 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006119 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006120 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006121out:
6122 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 return status;
6124}
6125
6126static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6127{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006128 struct nfs4_exception exception = {
6129 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006130 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 int err;
6133
6134 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006135 err = _nfs4_proc_setlk(state, cmd, request);
6136 if (err == -NFS4ERR_DENIED)
6137 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006139 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 } while (exception.retry);
6141 return err;
6142}
6143
6144static int
6145nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6146{
6147 struct nfs_open_context *ctx;
6148 struct nfs4_state *state;
6149 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6150 int status;
6151
6152 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006153 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 state = ctx->state;
6155
6156 if (request->fl_start < 0 || request->fl_end < 0)
6157 return -EINVAL;
6158
Trond Myklebustd9531262009-07-21 19:22:38 -04006159 if (IS_GETLK(cmd)) {
6160 if (state != NULL)
6161 return nfs4_proc_getlk(state, F_GETLK, request);
6162 return 0;
6163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
6165 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6166 return -EINVAL;
6167
Trond Myklebustd9531262009-07-21 19:22:38 -04006168 if (request->fl_type == F_UNLCK) {
6169 if (state != NULL)
6170 return nfs4_proc_unlck(state, cmd, request);
6171 return 0;
6172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173
Trond Myklebustd9531262009-07-21 19:22:38 -04006174 if (state == NULL)
6175 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006176 /*
6177 * Don't rely on the VFS having checked the file open mode,
6178 * since it won't do this for flock() locks.
6179 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006180 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006181 case F_RDLCK:
6182 if (!(filp->f_mode & FMODE_READ))
6183 return -EBADF;
6184 break;
6185 case F_WRLCK:
6186 if (!(filp->f_mode & FMODE_WRITE))
6187 return -EBADF;
6188 }
6189
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 do {
6191 status = nfs4_proc_setlk(state, cmd, request);
6192 if ((status != -EAGAIN) || IS_SETLK(cmd))
6193 break;
6194 timeout = nfs4_set_lock_task_retry(timeout);
6195 status = -ERESTARTSYS;
6196 if (signalled())
6197 break;
6198 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 return status;
6200}
6201
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006202int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006203{
6204 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006205 int err;
6206
6207 err = nfs4_set_lock_state(state, fl);
6208 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006209 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006210 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006211 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006212}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006213
Trond Myklebustcf470c32012-03-07 13:49:12 -05006214struct nfs_release_lockowner_data {
6215 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006216 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006217 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006218 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006219 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006220};
6221
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006222static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6223{
6224 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006225 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006226 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6227 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006228 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006229 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006230}
6231
6232static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6233{
6234 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006235 struct nfs_server *server = data->server;
6236
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006237 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006238
6239 switch (task->tk_status) {
6240 case 0:
6241 renew_lease(server, data->timestamp);
6242 break;
6243 case -NFS4ERR_STALE_CLIENTID:
6244 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006245 nfs4_schedule_lease_recovery(server->nfs_client);
6246 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006247 case -NFS4ERR_LEASE_MOVED:
6248 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006249 if (nfs4_async_handle_error(task, server,
6250 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006251 rpc_restart_call_prepare(task);
6252 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006253}
6254
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006255static void nfs4_release_lockowner_release(void *calldata)
6256{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006257 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006258 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006259 kfree(calldata);
6260}
6261
Trond Myklebust17280172012-03-11 13:11:00 -04006262static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006263 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6264 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006265 .rpc_release = nfs4_release_lockowner_release,
6266};
6267
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006268static void
6269nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006270{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006271 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006272 struct rpc_message msg = {
6273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6274 };
6275
6276 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006277 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006278
Trond Myklebustcf470c32012-03-07 13:49:12 -05006279 data = kmalloc(sizeof(*data), GFP_NOFS);
6280 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006281 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006282 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006283 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006284 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6285 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6286 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006287
Trond Myklebustcf470c32012-03-07 13:49:12 -05006288 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006289 msg.rpc_resp = &data->res;
6290 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006291 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006292}
6293
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006294#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6295
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006296static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006297 struct dentry *unused, struct inode *inode,
6298 const char *key, const void *buf,
6299 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006300{
Al Viro59301222016-05-27 10:19:30 -04006301 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006302}
6303
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006304static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006305 struct dentry *unused, struct inode *inode,
6306 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006307{
Al Virob2968212016-04-10 20:48:24 -04006308 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006309}
6310
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006311static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006312{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006313 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006314}
6315
David Quigleyc9bccef2013-05-22 12:50:45 -04006316#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006317
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006318static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006319 struct dentry *unused, struct inode *inode,
6320 const char *key, const void *buf,
6321 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006322{
6323 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006324 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006325
6326 return -EOPNOTSUPP;
6327}
6328
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006329static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006330 struct dentry *unused, struct inode *inode,
6331 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006332{
6333 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006334 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006335 return -EOPNOTSUPP;
6336}
6337
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006338static ssize_t
6339nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006340{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006341 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006342
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006343 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6344 len = security_inode_listsecurity(inode, list, list_len);
6345 if (list_len && len > list_len)
6346 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006347 }
6348 return len;
6349}
6350
6351static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6352 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006353 .get = nfs4_xattr_get_nfs4_label,
6354 .set = nfs4_xattr_set_nfs4_label,
6355};
David Quigleyc9bccef2013-05-22 12:50:45 -04006356
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006357#else
6358
6359static ssize_t
6360nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6361{
6362 return 0;
6363}
6364
6365#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006366
Andy Adamson533eb462011-06-13 18:25:56 -04006367/*
6368 * nfs_fhget will use either the mounted_on_fileid or the fileid
6369 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006370static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6371{
Andy Adamson533eb462011-06-13 18:25:56 -04006372 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6373 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6374 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006375 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006376 return;
6377
6378 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006379 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006380 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6381 fattr->nlink = 2;
6382}
6383
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006384static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6385 const struct qstr *name,
6386 struct nfs4_fs_locations *fs_locations,
6387 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006388{
6389 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006390 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006391 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006392 };
6393 struct nfs4_fs_locations_arg args = {
6394 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006395 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006396 .page = page,
6397 .bitmask = bitmask,
6398 };
Benny Halevy22958462009-04-01 09:22:02 -04006399 struct nfs4_fs_locations_res res = {
6400 .fs_locations = fs_locations,
6401 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006402 struct rpc_message msg = {
6403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6404 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006405 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006406 };
6407 int status;
6408
Harvey Harrison3110ff82008-05-02 13:42:44 -07006409 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006410
6411 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6412 * is not supported */
6413 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6414 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6415 else
6416 bitmask[0] |= FATTR4_WORD0_FILEID;
6417
Trond Myklebustc228fd32007-01-13 02:28:11 -05006418 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006419 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006420 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006421 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006422 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006423 return status;
6424}
6425
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006426int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6427 const struct qstr *name,
6428 struct nfs4_fs_locations *fs_locations,
6429 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006430{
6431 struct nfs4_exception exception = { };
6432 int err;
6433 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006434 err = _nfs4_proc_fs_locations(client, dir, name,
6435 fs_locations, page);
6436 trace_nfs4_get_fs_locations(dir, name, err);
6437 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006438 &exception);
6439 } while (exception.retry);
6440 return err;
6441}
6442
Chuck Leverb03d7352013-10-17 14:12:50 -04006443/*
6444 * This operation also signals the server that this client is
6445 * performing migration recovery. The server can stop returning
6446 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6447 * appended to this compound to identify the client ID which is
6448 * performing recovery.
6449 */
6450static int _nfs40_proc_get_locations(struct inode *inode,
6451 struct nfs4_fs_locations *locations,
6452 struct page *page, struct rpc_cred *cred)
6453{
6454 struct nfs_server *server = NFS_SERVER(inode);
6455 struct rpc_clnt *clnt = server->client;
6456 u32 bitmask[2] = {
6457 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6458 };
6459 struct nfs4_fs_locations_arg args = {
6460 .clientid = server->nfs_client->cl_clientid,
6461 .fh = NFS_FH(inode),
6462 .page = page,
6463 .bitmask = bitmask,
6464 .migration = 1, /* skip LOOKUP */
6465 .renew = 1, /* append RENEW */
6466 };
6467 struct nfs4_fs_locations_res res = {
6468 .fs_locations = locations,
6469 .migration = 1,
6470 .renew = 1,
6471 };
6472 struct rpc_message msg = {
6473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6474 .rpc_argp = &args,
6475 .rpc_resp = &res,
6476 .rpc_cred = cred,
6477 };
6478 unsigned long now = jiffies;
6479 int status;
6480
6481 nfs_fattr_init(&locations->fattr);
6482 locations->server = server;
6483 locations->nlocations = 0;
6484
6485 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6486 nfs4_set_sequence_privileged(&args.seq_args);
6487 status = nfs4_call_sync_sequence(clnt, server, &msg,
6488 &args.seq_args, &res.seq_res);
6489 if (status)
6490 return status;
6491
6492 renew_lease(server, now);
6493 return 0;
6494}
6495
6496#ifdef CONFIG_NFS_V4_1
6497
6498/*
6499 * This operation also signals the server that this client is
6500 * performing migration recovery. The server can stop asserting
6501 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6502 * performing this operation is identified in the SEQUENCE
6503 * operation in this compound.
6504 *
6505 * When the client supports GETATTR(fs_locations_info), it can
6506 * be plumbed in here.
6507 */
6508static int _nfs41_proc_get_locations(struct inode *inode,
6509 struct nfs4_fs_locations *locations,
6510 struct page *page, struct rpc_cred *cred)
6511{
6512 struct nfs_server *server = NFS_SERVER(inode);
6513 struct rpc_clnt *clnt = server->client;
6514 u32 bitmask[2] = {
6515 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6516 };
6517 struct nfs4_fs_locations_arg args = {
6518 .fh = NFS_FH(inode),
6519 .page = page,
6520 .bitmask = bitmask,
6521 .migration = 1, /* skip LOOKUP */
6522 };
6523 struct nfs4_fs_locations_res res = {
6524 .fs_locations = locations,
6525 .migration = 1,
6526 };
6527 struct rpc_message msg = {
6528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6529 .rpc_argp = &args,
6530 .rpc_resp = &res,
6531 .rpc_cred = cred,
6532 };
6533 int status;
6534
6535 nfs_fattr_init(&locations->fattr);
6536 locations->server = server;
6537 locations->nlocations = 0;
6538
6539 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6540 nfs4_set_sequence_privileged(&args.seq_args);
6541 status = nfs4_call_sync_sequence(clnt, server, &msg,
6542 &args.seq_args, &res.seq_res);
6543 if (status == NFS4_OK &&
6544 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6545 status = -NFS4ERR_LEASE_MOVED;
6546 return status;
6547}
6548
6549#endif /* CONFIG_NFS_V4_1 */
6550
6551/**
6552 * nfs4_proc_get_locations - discover locations for a migrated FSID
6553 * @inode: inode on FSID that is migrating
6554 * @locations: result of query
6555 * @page: buffer
6556 * @cred: credential to use for this operation
6557 *
6558 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6559 * operation failed, or a negative errno if a local error occurred.
6560 *
6561 * On success, "locations" is filled in, but if the server has
6562 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6563 * asserted.
6564 *
6565 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6566 * from this client that require migration recovery.
6567 */
6568int nfs4_proc_get_locations(struct inode *inode,
6569 struct nfs4_fs_locations *locations,
6570 struct page *page, struct rpc_cred *cred)
6571{
6572 struct nfs_server *server = NFS_SERVER(inode);
6573 struct nfs_client *clp = server->nfs_client;
6574 const struct nfs4_mig_recovery_ops *ops =
6575 clp->cl_mvops->mig_recovery_ops;
6576 struct nfs4_exception exception = { };
6577 int status;
6578
6579 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6580 (unsigned long long)server->fsid.major,
6581 (unsigned long long)server->fsid.minor,
6582 clp->cl_hostname);
6583 nfs_display_fhandle(NFS_FH(inode), __func__);
6584
6585 do {
6586 status = ops->get_locations(inode, locations, page, cred);
6587 if (status != -NFS4ERR_DELAY)
6588 break;
6589 nfs4_handle_exception(server, status, &exception);
6590 } while (exception.retry);
6591 return status;
6592}
6593
Chuck Lever44c99932013-10-17 14:13:30 -04006594/*
6595 * This operation also signals the server that this client is
6596 * performing "lease moved" recovery. The server can stop
6597 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6598 * is appended to this compound to identify the client ID which is
6599 * performing recovery.
6600 */
6601static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6602{
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6605 struct rpc_clnt *clnt = server->client;
6606 struct nfs4_fsid_present_arg args = {
6607 .fh = NFS_FH(inode),
6608 .clientid = clp->cl_clientid,
6609 .renew = 1, /* append RENEW */
6610 };
6611 struct nfs4_fsid_present_res res = {
6612 .renew = 1,
6613 };
6614 struct rpc_message msg = {
6615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6616 .rpc_argp = &args,
6617 .rpc_resp = &res,
6618 .rpc_cred = cred,
6619 };
6620 unsigned long now = jiffies;
6621 int status;
6622
6623 res.fh = nfs_alloc_fhandle();
6624 if (res.fh == NULL)
6625 return -ENOMEM;
6626
6627 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6628 nfs4_set_sequence_privileged(&args.seq_args);
6629 status = nfs4_call_sync_sequence(clnt, server, &msg,
6630 &args.seq_args, &res.seq_res);
6631 nfs_free_fhandle(res.fh);
6632 if (status)
6633 return status;
6634
6635 do_renew_lease(clp, now);
6636 return 0;
6637}
6638
6639#ifdef CONFIG_NFS_V4_1
6640
6641/*
6642 * This operation also signals the server that this client is
6643 * performing "lease moved" recovery. The server can stop asserting
6644 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6645 * this operation is identified in the SEQUENCE operation in this
6646 * compound.
6647 */
6648static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6649{
6650 struct nfs_server *server = NFS_SERVER(inode);
6651 struct rpc_clnt *clnt = server->client;
6652 struct nfs4_fsid_present_arg args = {
6653 .fh = NFS_FH(inode),
6654 };
6655 struct nfs4_fsid_present_res res = {
6656 };
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6659 .rpc_argp = &args,
6660 .rpc_resp = &res,
6661 .rpc_cred = cred,
6662 };
6663 int status;
6664
6665 res.fh = nfs_alloc_fhandle();
6666 if (res.fh == NULL)
6667 return -ENOMEM;
6668
6669 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6670 nfs4_set_sequence_privileged(&args.seq_args);
6671 status = nfs4_call_sync_sequence(clnt, server, &msg,
6672 &args.seq_args, &res.seq_res);
6673 nfs_free_fhandle(res.fh);
6674 if (status == NFS4_OK &&
6675 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6676 status = -NFS4ERR_LEASE_MOVED;
6677 return status;
6678}
6679
6680#endif /* CONFIG_NFS_V4_1 */
6681
6682/**
6683 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6684 * @inode: inode on FSID to check
6685 * @cred: credential to use for this operation
6686 *
6687 * Server indicates whether the FSID is present, moved, or not
6688 * recognized. This operation is necessary to clear a LEASE_MOVED
6689 * condition for this client ID.
6690 *
6691 * Returns NFS4_OK if the FSID is present on this server,
6692 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6693 * NFS4ERR code if some error occurred on the server, or a
6694 * negative errno if a local failure occurred.
6695 */
6696int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6697{
6698 struct nfs_server *server = NFS_SERVER(inode);
6699 struct nfs_client *clp = server->nfs_client;
6700 const struct nfs4_mig_recovery_ops *ops =
6701 clp->cl_mvops->mig_recovery_ops;
6702 struct nfs4_exception exception = { };
6703 int status;
6704
6705 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6706 (unsigned long long)server->fsid.major,
6707 (unsigned long long)server->fsid.minor,
6708 clp->cl_hostname);
6709 nfs_display_fhandle(NFS_FH(inode), __func__);
6710
6711 do {
6712 status = ops->fsid_present(inode, cred);
6713 if (status != -NFS4ERR_DELAY)
6714 break;
6715 nfs4_handle_exception(server, status, &exception);
6716 } while (exception.retry);
6717 return status;
6718}
6719
Andy Adamson5ec16a82013-08-08 10:57:55 -04006720/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006721 * If 'use_integrity' is true and the state managment nfs_client
6722 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6723 * and the machine credential as per RFC3530bis and RFC5661 Security
6724 * Considerations sections. Otherwise, just use the user cred with the
6725 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006726 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006727static 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 +00006728{
6729 int status;
6730 struct nfs4_secinfo_arg args = {
6731 .dir_fh = NFS_FH(dir),
6732 .name = name,
6733 };
6734 struct nfs4_secinfo_res res = {
6735 .flavors = flavors,
6736 };
6737 struct rpc_message msg = {
6738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6739 .rpc_argp = &args,
6740 .rpc_resp = &res,
6741 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006742 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006743 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006744
6745 if (use_integrity) {
6746 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006747 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6748 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006749 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006750
6751 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006752
6753 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6754 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6755
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006756 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6757 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006758 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006759
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006760 if (cred)
6761 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006762
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006763 return status;
6764}
6765
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006766int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6767 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006768{
6769 struct nfs4_exception exception = { };
6770 int err;
6771 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006772 err = -NFS4ERR_WRONGSEC;
6773
6774 /* try to use integrity protection with machine cred */
6775 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6776 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6777
6778 /*
6779 * if unable to use integrity protection, or SECINFO with
6780 * integrity protection returns NFS4ERR_WRONGSEC (which is
6781 * disallowed by spec, but exists in deployed servers) use
6782 * the current filesystem's rpc_client and the user cred.
6783 */
6784 if (err == -NFS4ERR_WRONGSEC)
6785 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6786
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006787 trace_nfs4_secinfo(dir, name, err);
6788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006789 &exception);
6790 } while (exception.retry);
6791 return err;
6792}
6793
Andy Adamson557134a2009-04-01 09:21:53 -04006794#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006795/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006796 * Check the exchange flags returned by the server for invalid flags, having
6797 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6798 * DS flags set.
6799 */
6800static int nfs4_check_cl_exchange_flags(u32 flags)
6801{
6802 if (flags & ~EXCHGID4_FLAG_MASK_R)
6803 goto out_inval;
6804 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6805 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6806 goto out_inval;
6807 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6808 goto out_inval;
6809 return NFS_OK;
6810out_inval:
6811 return -NFS4ERR_INVAL;
6812}
6813
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006814static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006815nfs41_same_server_scope(struct nfs41_server_scope *a,
6816 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006817{
6818 if (a->server_scope_sz == b->server_scope_sz &&
6819 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6820 return true;
6821
6822 return false;
6823}
6824
Andy Adamson02a95de2016-02-05 16:08:37 -05006825static void
6826nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6827{
6828}
6829
6830static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6831 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6832};
6833
Andy Adamson357f54d2010-12-14 10:11:57 -05006834/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006835 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006836 *
6837 * The 4.1 client currently uses the same TCP connection for the
6838 * fore and backchannel.
6839 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006840static
6841int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6842 struct rpc_xprt *xprt,
6843 struct nfs_client *clp,
6844 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006845{
6846 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006847 struct nfs41_bind_conn_to_session_args args = {
6848 .client = clp,
6849 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6850 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006851 struct nfs41_bind_conn_to_session_res res;
6852 struct rpc_message msg = {
6853 .rpc_proc =
6854 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006855 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006856 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006857 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006858 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006859 struct rpc_task_setup task_setup_data = {
6860 .rpc_client = clnt,
6861 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006862 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006863 .rpc_message = &msg,
6864 .flags = RPC_TASK_TIMEOUT,
6865 };
6866 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006867
6868 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006869
Trond Myklebust71a097c2015-02-18 09:27:18 -08006870 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6871 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6872 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006873
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006874 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6875 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6876 args.dir = NFS4_CDFC4_FORE;
6877
6878 task = rpc_run_task(&task_setup_data);
6879 if (!IS_ERR(task)) {
6880 status = task->tk_status;
6881 rpc_put_task(task);
6882 } else
6883 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006884 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006885 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006886 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006887 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6888 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6889 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006890 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006891 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006892 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006893 dprintk("NFS: %s: Unexpected direction from server\n",
6894 __func__);
6895 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006896 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006897 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006898 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006899 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6900 __func__);
6901 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006902 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006903 }
6904 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006905out:
6906 dprintk("<-- %s status= %d\n", __func__, status);
6907 return status;
6908}
6909
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006910struct rpc_bind_conn_calldata {
6911 struct nfs_client *clp;
6912 struct rpc_cred *cred;
6913};
6914
6915static int
6916nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6917 struct rpc_xprt *xprt,
6918 void *calldata)
6919{
6920 struct rpc_bind_conn_calldata *p = calldata;
6921
6922 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6923}
6924
6925int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6926{
6927 struct rpc_bind_conn_calldata data = {
6928 .clp = clp,
6929 .cred = cred,
6930 };
6931 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6932 nfs4_proc_bind_conn_to_session_callback, &data);
6933}
6934
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006935/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006936 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6937 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006938 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006939static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6940 .how = SP4_MACH_CRED,
6941 .enforce.u.words = {
6942 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6943 1 << (OP_EXCHANGE_ID - 32) |
6944 1 << (OP_CREATE_SESSION - 32) |
6945 1 << (OP_DESTROY_SESSION - 32) |
6946 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006947 },
6948 .allow.u.words = {
6949 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006950 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006951 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006952 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006953 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006954 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006955 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006956 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006957 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006958 1 << (OP_FREE_STATEID - 32) |
6959 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006960 }
6961};
6962
6963/*
6964 * Select the state protection mode for client `clp' given the server results
6965 * from exchange_id in `sp'.
6966 *
6967 * Returns 0 on success, negative errno otherwise.
6968 */
6969static int nfs4_sp4_select_mode(struct nfs_client *clp,
6970 struct nfs41_state_protection *sp)
6971{
6972 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6973 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6974 1 << (OP_EXCHANGE_ID - 32) |
6975 1 << (OP_CREATE_SESSION - 32) |
6976 1 << (OP_DESTROY_SESSION - 32) |
6977 1 << (OP_DESTROY_CLIENTID - 32)
6978 };
6979 unsigned int i;
6980
6981 if (sp->how == SP4_MACH_CRED) {
6982 /* Print state protect result */
6983 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6984 for (i = 0; i <= LAST_NFS4_OP; i++) {
6985 if (test_bit(i, sp->enforce.u.longs))
6986 dfprintk(MOUNT, " enforce op %d\n", i);
6987 if (test_bit(i, sp->allow.u.longs))
6988 dfprintk(MOUNT, " allow op %d\n", i);
6989 }
6990
6991 /* make sure nothing is on enforce list that isn't supported */
6992 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6993 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6994 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6995 return -EINVAL;
6996 }
6997 }
6998
6999 /*
7000 * Minimal mode - state operations are allowed to use machine
7001 * credential. Note this already happens by default, so the
7002 * client doesn't have to do anything more than the negotiation.
7003 *
7004 * NOTE: we don't care if EXCHANGE_ID is in the list -
7005 * we're already using the machine cred for exchange_id
7006 * and will never use a different cred.
7007 */
7008 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7009 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7010 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7011 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7012 dfprintk(MOUNT, "sp4_mach_cred:\n");
7013 dfprintk(MOUNT, " minimal mode enabled\n");
7014 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7015 } else {
7016 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7017 return -EINVAL;
7018 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007019
7020 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007021 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7022 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007023 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7024 dfprintk(MOUNT, " cleanup mode enabled\n");
7025 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7026 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007027
Andrew Elble99ade3c2015-12-02 09:39:51 -05007028 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7029 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7030 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7031 &clp->cl_sp4_flags);
7032 }
7033
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007034 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7035 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7036 dfprintk(MOUNT, " secinfo mode enabled\n");
7037 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7038 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007039
7040 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7041 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7042 dfprintk(MOUNT, " stateid mode enabled\n");
7043 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7044 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007045
7046 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7047 dfprintk(MOUNT, " write mode enabled\n");
7048 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7049 }
7050
7051 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7052 dfprintk(MOUNT, " commit mode enabled\n");
7053 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7054 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007055 }
7056
7057 return 0;
7058}
7059
7060/*
7061 * _nfs4_proc_exchange_id()
7062 *
7063 * Wrapper for EXCHANGE_ID operation.
7064 */
7065static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7066 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007067{
7068 nfs4_verifier verifier;
7069 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007070 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007071 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007072#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007073 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007074 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7075 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7076#else
7077 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7078 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7079#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007080 };
7081 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007082 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007083 };
7084 int status;
7085 struct rpc_message msg = {
7086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7087 .rpc_argp = &args,
7088 .rpc_resp = &res,
7089 .rpc_cred = cred,
7090 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007091
Chuck Leverf0920752012-05-21 22:45:41 -04007092 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007093
7094 status = nfs4_init_uniform_client_string(clp);
7095 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007096 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007097
7098 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007099 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007100 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007101
Chuck Leveracdeb692012-05-21 22:46:16 -04007102 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007103 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007104 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007105 status = -ENOMEM;
7106 goto out;
7107 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007108
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007109 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007110 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007111 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007112 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007113 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007114 }
7115
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007116 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007117 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007118 status = -ENOMEM;
7119 goto out_server_scope;
7120 }
7121
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007122 switch (sp4_how) {
7123 case SP4_NONE:
7124 args.state_protect.how = SP4_NONE;
7125 break;
7126
7127 case SP4_MACH_CRED:
7128 args.state_protect = nfs4_sp4_mach_cred_request;
7129 break;
7130
7131 default:
7132 /* unsupported! */
7133 WARN_ON_ONCE(1);
7134 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007135 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007136 }
7137
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007138 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007139 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007140 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007141 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007142
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007143 if (status == 0)
7144 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7145
Chuck Lever177313f2012-05-21 22:44:58 -04007146 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007147 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007148 clp->cl_exchange_flags = res.flags;
7149 /* Client ID is not confirmed */
7150 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7151 clear_bit(NFS4_SESSION_ESTABLISHED,
7152 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007153 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007154 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007155
Chuck Leveracdeb692012-05-21 22:46:16 -04007156 kfree(clp->cl_serverowner);
7157 clp->cl_serverowner = res.server_owner;
7158 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007159
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007160 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007161 kfree(clp->cl_implid);
7162 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007163 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007164
Chuck Lever177313f2012-05-21 22:44:58 -04007165 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007166 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007167 res.server_scope)) {
7168 dprintk("%s: server_scope mismatch detected\n",
7169 __func__);
7170 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007171 kfree(clp->cl_serverscope);
7172 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007173 }
7174
Chuck Lever177313f2012-05-21 22:44:58 -04007175 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007176 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007177 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007178 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007179 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007180
Kinglong Mee6b559702015-07-01 11:54:53 +08007181out_impl_id:
7182 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007183out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007184 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007185out_server_owner:
7186 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007187out:
Chuck Lever177313f2012-05-21 22:44:58 -04007188 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007189 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007190 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007191 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007192 clp->cl_implid->date.seconds,
7193 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007194 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007195 return status;
7196}
7197
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007198/*
7199 * nfs4_proc_exchange_id()
7200 *
7201 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7202 *
7203 * Since the clientid has expired, all compounds using sessions
7204 * associated with the stale clientid will be returning
7205 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7206 * be in some phase of session reset.
7207 *
7208 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7209 */
7210int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7211{
7212 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7213 int status;
7214
7215 /* try SP4_MACH_CRED if krb5i/p */
7216 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7217 authflavor == RPC_AUTH_GSS_KRB5P) {
7218 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7219 if (!status)
7220 return 0;
7221 }
7222
7223 /* try SP4_NONE */
7224 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7225}
7226
Trond Myklebust66245532012-05-25 17:18:09 -04007227static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7228 struct rpc_cred *cred)
7229{
7230 struct rpc_message msg = {
7231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7232 .rpc_argp = clp,
7233 .rpc_cred = cred,
7234 };
7235 int status;
7236
7237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007238 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007239 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007240 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007241 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7242 return status;
7243}
7244
7245static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7246 struct rpc_cred *cred)
7247{
7248 unsigned int loop;
7249 int ret;
7250
7251 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7252 ret = _nfs4_proc_destroy_clientid(clp, cred);
7253 switch (ret) {
7254 case -NFS4ERR_DELAY:
7255 case -NFS4ERR_CLIENTID_BUSY:
7256 ssleep(1);
7257 break;
7258 default:
7259 return ret;
7260 }
7261 }
7262 return 0;
7263}
7264
7265int nfs4_destroy_clientid(struct nfs_client *clp)
7266{
7267 struct rpc_cred *cred;
7268 int ret = 0;
7269
7270 if (clp->cl_mvops->minor_version < 1)
7271 goto out;
7272 if (clp->cl_exchange_flags == 0)
7273 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007274 if (clp->cl_preserve_clid)
7275 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007276 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007277 ret = nfs4_proc_destroy_clientid(clp, cred);
7278 if (cred)
7279 put_rpccred(cred);
7280 switch (ret) {
7281 case 0:
7282 case -NFS4ERR_STALE_CLIENTID:
7283 clp->cl_exchange_flags = 0;
7284 }
7285out:
7286 return ret;
7287}
7288
Andy Adamson2050f0c2009-04-01 09:22:30 -04007289struct nfs4_get_lease_time_data {
7290 struct nfs4_get_lease_time_args *args;
7291 struct nfs4_get_lease_time_res *res;
7292 struct nfs_client *clp;
7293};
7294
7295static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7296 void *calldata)
7297{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007298 struct nfs4_get_lease_time_data *data =
7299 (struct nfs4_get_lease_time_data *)calldata;
7300
7301 dprintk("--> %s\n", __func__);
7302 /* just setup sequence, do not trigger session recovery
7303 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007304 nfs41_setup_sequence(data->clp->cl_session,
7305 &data->args->la_seq_args,
7306 &data->res->lr_seq_res,
7307 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007308 dprintk("<-- %s\n", __func__);
7309}
7310
7311/*
7312 * Called from nfs4_state_manager thread for session setup, so don't recover
7313 * from sequence operation or clientid errors.
7314 */
7315static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7316{
7317 struct nfs4_get_lease_time_data *data =
7318 (struct nfs4_get_lease_time_data *)calldata;
7319
7320 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007321 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7322 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007323 switch (task->tk_status) {
7324 case -NFS4ERR_DELAY:
7325 case -NFS4ERR_GRACE:
7326 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7327 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7328 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007329 /* fall through */
7330 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007331 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007332 return;
7333 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007334 dprintk("<-- %s\n", __func__);
7335}
7336
Trond Myklebust17280172012-03-11 13:11:00 -04007337static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007338 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7339 .rpc_call_done = nfs4_get_lease_time_done,
7340};
7341
7342int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7343{
7344 struct rpc_task *task;
7345 struct nfs4_get_lease_time_args args;
7346 struct nfs4_get_lease_time_res res = {
7347 .lr_fsinfo = fsinfo,
7348 };
7349 struct nfs4_get_lease_time_data data = {
7350 .args = &args,
7351 .res = &res,
7352 .clp = clp,
7353 };
7354 struct rpc_message msg = {
7355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7356 .rpc_argp = &args,
7357 .rpc_resp = &res,
7358 };
7359 struct rpc_task_setup task_setup = {
7360 .rpc_client = clp->cl_rpcclient,
7361 .rpc_message = &msg,
7362 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007363 .callback_data = &data,
7364 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007365 };
7366 int status;
7367
Chuck Levera9c92d62013-08-09 12:48:18 -04007368 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007369 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007370 dprintk("--> %s\n", __func__);
7371 task = rpc_run_task(&task_setup);
7372
7373 if (IS_ERR(task))
7374 status = PTR_ERR(task);
7375 else {
7376 status = task->tk_status;
7377 rpc_put_task(task);
7378 }
7379 dprintk("<-- %s return %d\n", __func__, status);
7380
7381 return status;
7382}
7383
Andy Adamsonfc931582009-04-01 09:22:31 -04007384/*
7385 * Initialize the values to be used by the client in CREATE_SESSION
7386 * If nfs4_init_session set the fore channel request and response sizes,
7387 * use them.
7388 *
7389 * Set the back channel max_resp_sz_cached to zero to force the client to
7390 * always set csa_cachethis to FALSE because the current implementation
7391 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7392 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007393static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7394 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007395{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007396 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007397 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007398
Andy Adamson18aad3d2013-06-26 12:21:49 -04007399 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7400 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7401
Andy Adamsonfc931582009-04-01 09:22:31 -04007402 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007403 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7404 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007405 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007406 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007407
7408 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007409 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007410 __func__,
7411 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007412 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007413
7414 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007415 args->bc_attrs.max_rqst_sz = max_bc_payload;
7416 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007417 args->bc_attrs.max_resp_sz_cached = 0;
7418 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebustf4f58ed2016-01-23 14:12:52 -05007419 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
Andy Adamsonfc931582009-04-01 09:22:31 -04007420
7421 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7422 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7423 __func__,
7424 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7425 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7426 args->bc_attrs.max_reqs);
7427}
7428
Trond Myklebust79969dd2015-02-18 11:30:18 -08007429static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7430 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007431{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007432 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007433 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007434
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007435 if (rcvd->max_resp_sz > sent->max_resp_sz)
7436 return -EINVAL;
7437 /*
7438 * Our requested max_ops is the minimum we need; we're not
7439 * prepared to break up compounds into smaller pieces than that.
7440 * So, no point even trying to continue if the server won't
7441 * cooperate:
7442 */
7443 if (rcvd->max_ops < sent->max_ops)
7444 return -EINVAL;
7445 if (rcvd->max_reqs == 0)
7446 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007447 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7448 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007449 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007450}
7451
Trond Myklebust79969dd2015-02-18 11:30:18 -08007452static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7453 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007454{
7455 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007456 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007457
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007458 if (!(res->flags & SESSION4_BACK_CHAN))
7459 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007460 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7461 return -EINVAL;
7462 if (rcvd->max_resp_sz < sent->max_resp_sz)
7463 return -EINVAL;
7464 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7465 return -EINVAL;
7466 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007467 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007468 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007469 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007470 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007471out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007472 return 0;
7473}
Andy Adamson8d353012009-04-01 09:22:32 -04007474
Andy Adamson8d353012009-04-01 09:22:32 -04007475static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007476 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007477{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007478 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007479
Trond Myklebust79969dd2015-02-18 11:30:18 -08007480 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007481 if (ret)
7482 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007483 return nfs4_verify_back_channel_attrs(args, res);
7484}
7485
7486static void nfs4_update_session(struct nfs4_session *session,
7487 struct nfs41_create_session_res *res)
7488{
7489 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007490 /* Mark client id and session as being confirmed */
7491 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7492 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007493 session->flags = res->flags;
7494 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007495 if (res->flags & SESSION4_BACK_CHAN)
7496 memcpy(&session->bc_attrs, &res->bc_attrs,
7497 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007498}
7499
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007500static int _nfs4_proc_create_session(struct nfs_client *clp,
7501 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007502{
7503 struct nfs4_session *session = clp->cl_session;
7504 struct nfs41_create_session_args args = {
7505 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007506 .clientid = clp->cl_clientid,
7507 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007508 .cb_program = NFS4_CALLBACK,
7509 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007510 struct nfs41_create_session_res res;
7511
Andy Adamsonfc931582009-04-01 09:22:31 -04007512 struct rpc_message msg = {
7513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7514 .rpc_argp = &args,
7515 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007516 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007517 };
7518 int status;
7519
Chuck Lever6b26cc82016-05-02 14:40:40 -04007520 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007521 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007522
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007523 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007524 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007525
Trond Myklebust43095d32012-11-20 11:13:12 -05007526 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007527 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007528 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007529 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007530 if (clp->cl_seqid == res.seqid)
7531 clp->cl_seqid++;
7532 if (status)
7533 goto out;
7534 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007535 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007536out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007537 return status;
7538}
7539
7540/*
7541 * Issues a CREATE_SESSION operation to the server.
7542 * It is the responsibility of the caller to verify the session is
7543 * expired before calling this routine.
7544 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007545int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007546{
7547 int status;
7548 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007549 struct nfs4_session *session = clp->cl_session;
7550
7551 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7552
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007553 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007554 if (status)
7555 goto out;
7556
Andy Adamsonaacd5532011-11-09 13:58:21 -05007557 /* Init or reset the session slot tables */
7558 status = nfs4_setup_session_slot_tables(session);
7559 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007560 if (status)
7561 goto out;
7562
7563 ptr = (unsigned *)&session->sess_id.data[0];
7564 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7565 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007566out:
7567 dprintk("<-- %s\n", __func__);
7568 return status;
7569}
7570
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007571/*
7572 * Issue the over-the-wire RPC DESTROY_SESSION.
7573 * The caller must serialize access to this routine.
7574 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007575int nfs4_proc_destroy_session(struct nfs4_session *session,
7576 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007577{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007578 struct rpc_message msg = {
7579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7580 .rpc_argp = session,
7581 .rpc_cred = cred,
7582 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007583 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007584
7585 dprintk("--> nfs4_proc_destroy_session\n");
7586
7587 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007588 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7589 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007590
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007591 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007592 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007593
7594 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007595 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007596 "Session has been destroyed regardless...\n", status);
7597
7598 dprintk("<-- nfs4_proc_destroy_session\n");
7599 return status;
7600}
7601
Trond Myklebust7b38c362012-05-23 13:23:31 -04007602/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007603 * Renew the cl_session lease.
7604 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007605struct nfs4_sequence_data {
7606 struct nfs_client *clp;
7607 struct nfs4_sequence_args args;
7608 struct nfs4_sequence_res res;
7609};
7610
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007611static void nfs41_sequence_release(void *data)
7612{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007613 struct nfs4_sequence_data *calldata = data;
7614 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007615
Alexandros Batsakis71358402010-02-05 03:45:05 -08007616 if (atomic_read(&clp->cl_count) > 1)
7617 nfs4_schedule_state_renewal(clp);
7618 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007619 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007620}
7621
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007622static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7623{
7624 switch(task->tk_status) {
7625 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007626 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7627 return -EAGAIN;
7628 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007629 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007630 }
7631 return 0;
7632}
7633
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007634static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007635{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007636 struct nfs4_sequence_data *calldata = data;
7637 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007638
Trond Myklebust14516c32010-07-31 14:29:06 -04007639 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7640 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007641
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007642 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007643 if (task->tk_status < 0) {
7644 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007645 if (atomic_read(&clp->cl_count) == 1)
7646 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007647
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007648 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7649 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007650 return;
7651 }
7652 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007653 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007654out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007655 dprintk("<-- %s\n", __func__);
7656}
7657
7658static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7659{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007660 struct nfs4_sequence_data *calldata = data;
7661 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007662 struct nfs4_sequence_args *args;
7663 struct nfs4_sequence_res *res;
7664
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007665 args = task->tk_msg.rpc_argp;
7666 res = task->tk_msg.rpc_resp;
7667
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007668 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007669}
7670
7671static const struct rpc_call_ops nfs41_sequence_ops = {
7672 .rpc_call_done = nfs41_sequence_call_done,
7673 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007674 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007675};
7676
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007677static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7678 struct rpc_cred *cred,
7679 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007680{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007681 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007682 struct rpc_message msg = {
7683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7684 .rpc_cred = cred,
7685 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007686 struct rpc_task_setup task_setup_data = {
7687 .rpc_client = clp->cl_rpcclient,
7688 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007689 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007690 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007691 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007692
Alexandros Batsakis71358402010-02-05 03:45:05 -08007693 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007694 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007695 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007696 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007697 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007698 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007699 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007700 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007701 if (is_privileged)
7702 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007703 msg.rpc_argp = &calldata->args;
7704 msg.rpc_resp = &calldata->res;
7705 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007706 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007707
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007708 return rpc_run_task(&task_setup_data);
7709}
7710
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007711static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007712{
7713 struct rpc_task *task;
7714 int ret = 0;
7715
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007716 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007717 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007718 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007719 if (IS_ERR(task))
7720 ret = PTR_ERR(task);
7721 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007722 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007723 dprintk("<-- %s status=%d\n", __func__, ret);
7724 return ret;
7725}
7726
7727static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7728{
7729 struct rpc_task *task;
7730 int ret;
7731
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007732 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007733 if (IS_ERR(task)) {
7734 ret = PTR_ERR(task);
7735 goto out;
7736 }
7737 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007738 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007739 ret = task->tk_status;
7740 rpc_put_task(task);
7741out:
7742 dprintk("<-- %s status=%d\n", __func__, ret);
7743 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007744}
7745
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007746struct nfs4_reclaim_complete_data {
7747 struct nfs_client *clp;
7748 struct nfs41_reclaim_complete_args arg;
7749 struct nfs41_reclaim_complete_res res;
7750};
7751
7752static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7753{
7754 struct nfs4_reclaim_complete_data *calldata = data;
7755
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007756 nfs41_setup_sequence(calldata->clp->cl_session,
7757 &calldata->arg.seq_args,
7758 &calldata->res.seq_res,
7759 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007760}
7761
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007762static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7763{
7764 switch(task->tk_status) {
7765 case 0:
7766 case -NFS4ERR_COMPLETE_ALREADY:
7767 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7768 break;
7769 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007770 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007771 /* fall through */
7772 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007773 return -EAGAIN;
7774 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007775 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007776 }
7777 return 0;
7778}
7779
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007780static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7781{
7782 struct nfs4_reclaim_complete_data *calldata = data;
7783 struct nfs_client *clp = calldata->clp;
7784 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7785
7786 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007787 if (!nfs41_sequence_done(task, res))
7788 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007789
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007790 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007791 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7792 rpc_restart_call_prepare(task);
7793 return;
7794 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007795 dprintk("<-- %s\n", __func__);
7796}
7797
7798static void nfs4_free_reclaim_complete_data(void *data)
7799{
7800 struct nfs4_reclaim_complete_data *calldata = data;
7801
7802 kfree(calldata);
7803}
7804
7805static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7806 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7807 .rpc_call_done = nfs4_reclaim_complete_done,
7808 .rpc_release = nfs4_free_reclaim_complete_data,
7809};
7810
7811/*
7812 * Issue a global reclaim complete.
7813 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007814static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7815 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007816{
7817 struct nfs4_reclaim_complete_data *calldata;
7818 struct rpc_task *task;
7819 struct rpc_message msg = {
7820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007821 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007822 };
7823 struct rpc_task_setup task_setup_data = {
7824 .rpc_client = clp->cl_rpcclient,
7825 .rpc_message = &msg,
7826 .callback_ops = &nfs4_reclaim_complete_call_ops,
7827 .flags = RPC_TASK_ASYNC,
7828 };
7829 int status = -ENOMEM;
7830
7831 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007832 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007833 if (calldata == NULL)
7834 goto out;
7835 calldata->clp = clp;
7836 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007837
Chuck Levera9c92d62013-08-09 12:48:18 -04007838 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007839 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007840 msg.rpc_argp = &calldata->arg;
7841 msg.rpc_resp = &calldata->res;
7842 task_setup_data.callback_data = calldata;
7843 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007844 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007845 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007846 goto out;
7847 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007848 status = nfs4_wait_for_completion_rpc_task(task);
7849 if (status == 0)
7850 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007851 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007852 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007853out:
7854 dprintk("<-- %s status=%d\n", __func__, status);
7855 return status;
7856}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007857
7858static void
7859nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7860{
7861 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007862 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007863 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007864
7865 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007866 nfs41_setup_sequence(session, &lgp->args.seq_args,
7867 &lgp->res.seq_res, task);
7868 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007869}
7870
7871static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7872{
7873 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04007874
7875 dprintk("--> %s\n", __func__);
7876 nfs41_sequence_done(task, &lgp->res.seq_res);
7877 dprintk("<-- %s\n", __func__);
7878}
7879
7880static int
7881nfs4_layoutget_handle_exception(struct rpc_task *task,
7882 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
7883{
Trond Myklebustee314c22012-10-01 17:25:48 -07007884 struct inode *inode = lgp->args.inode;
7885 struct nfs_server *server = NFS_SERVER(inode);
7886 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007887 int nfs4err = task->tk_status;
7888 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007889 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007890
Boaz Harroshed7e5422014-01-22 20:34:54 +02007891 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007892
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007893 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007894 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007895 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007896
7897 /*
7898 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7899 * on the file. set tk_status to -ENODATA to tell upper layer to
7900 * retry go inband.
7901 */
7902 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04007903 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007904 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007905 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007906 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7907 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7908 */
7909 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007910 status = -EOVERFLOW;
7911 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007912 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007913 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007914 * (or clients) writing to the same RAID stripe except when
7915 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04007916 *
7917 * Treat it like we would RECALLCONFLICT -- we retry for a little
7918 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02007919 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007920 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04007921 if (lgp->args.minlength == 0) {
7922 status = -EOVERFLOW;
7923 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007924 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007925 status = -EBUSY;
7926 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04007927 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007928 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007929 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07007930 case -NFS4ERR_EXPIRED:
7931 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04007932 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07007933 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007934 lo = NFS_I(inode)->layout;
7935 /* If the open stateid was bad, then recover it. */
7936 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
7937 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04007938 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007939 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04007940 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007941 break;
7942 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007943
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007944 /*
7945 * Mark the bad layout state as invalid, then retry
7946 */
Trond Myklebust668f4552016-07-24 17:08:59 -04007947 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04007948 spin_unlock(&inode->i_lock);
7949 pnfs_free_lseg_list(&head);
7950 status = -EAGAIN;
7951 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007952 }
Jeff Layton183d9e72016-05-17 12:28:47 -04007953
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04007954 err = nfs4_handle_exception(server, nfs4err, exception);
7955 if (!status) {
7956 if (exception->retry)
7957 status = -EAGAIN;
7958 else
7959 status = err;
7960 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007961out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007962 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007963 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007964}
7965
Idan Kedar85541162012-08-02 11:47:10 +03007966static size_t max_response_pages(struct nfs_server *server)
7967{
7968 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7969 return nfs_page_array_len(0, max_resp_sz);
7970}
7971
7972static void nfs4_free_pages(struct page **pages, size_t size)
7973{
7974 int i;
7975
7976 if (!pages)
7977 return;
7978
7979 for (i = 0; i < size; i++) {
7980 if (!pages[i])
7981 break;
7982 __free_page(pages[i]);
7983 }
7984 kfree(pages);
7985}
7986
7987static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7988{
7989 struct page **pages;
7990 int i;
7991
7992 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7993 if (!pages) {
7994 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7995 return NULL;
7996 }
7997
7998 for (i = 0; i < size; i++) {
7999 pages[i] = alloc_page(gfp_flags);
8000 if (!pages[i]) {
8001 dprintk("%s: failed to allocate page\n", __func__);
8002 nfs4_free_pages(pages, size);
8003 return NULL;
8004 }
8005 }
8006
8007 return pages;
8008}
8009
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008010static void nfs4_layoutget_release(void *calldata)
8011{
8012 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008013 struct inode *inode = lgp->args.inode;
8014 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008015 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008016
8017 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008018 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008019 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008020 put_nfs_open_context(lgp->args.ctx);
8021 kfree(calldata);
8022 dprintk("<-- %s\n", __func__);
8023}
8024
8025static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8026 .rpc_call_prepare = nfs4_layoutget_prepare,
8027 .rpc_call_done = nfs4_layoutget_done,
8028 .rpc_release = nfs4_layoutget_release,
8029};
8030
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008031struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008032nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008033{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008034 struct inode *inode = lgp->args.inode;
8035 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008036 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008037 struct rpc_task *task;
8038 struct rpc_message msg = {
8039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8040 .rpc_argp = &lgp->args,
8041 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008042 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008043 };
8044 struct rpc_task_setup task_setup_data = {
8045 .rpc_client = server->client,
8046 .rpc_message = &msg,
8047 .callback_ops = &nfs4_layoutget_call_ops,
8048 .callback_data = lgp,
8049 .flags = RPC_TASK_ASYNC,
8050 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008051 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008052 struct nfs4_exception exception = {
8053 .inode = inode,
8054 .timeout = *timeout,
8055 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008056 int status = 0;
8057
8058 dprintk("--> %s\n", __func__);
8059
Peng Tao4bd5a982014-11-17 11:05:17 +08008060 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8061 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8062
Idan Kedar85541162012-08-02 11:47:10 +03008063 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8064 if (!lgp->args.layout.pages) {
8065 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008066 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008067 }
8068 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8069
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008070 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008071 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008072 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008073
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008074 task = rpc_run_task(&task_setup_data);
8075 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008076 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008077 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008078 if (status == 0) {
8079 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8080 *timeout = exception.timeout;
8081 }
8082
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008083 trace_nfs4_layoutget(lgp->args.ctx,
8084 &lgp->args.range,
8085 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008086 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008087 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008088
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008089 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8090 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008091 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008092 rpc_put_task(task);
8093 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008094 if (status)
8095 return ERR_PTR(status);
8096 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008097}
8098
Benny Halevycbe82602011-05-22 19:52:37 +03008099static void
8100nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8101{
8102 struct nfs4_layoutreturn *lrp = calldata;
8103
8104 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008105 nfs41_setup_sequence(lrp->clp->cl_session,
8106 &lrp->args.seq_args,
8107 &lrp->res.seq_res,
8108 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008109}
8110
8111static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8112{
8113 struct nfs4_layoutreturn *lrp = calldata;
8114 struct nfs_server *server;
8115
8116 dprintk("--> %s\n", __func__);
8117
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008118 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008119 return;
8120
8121 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008122 switch (task->tk_status) {
8123 default:
8124 task->tk_status = 0;
8125 case 0:
8126 break;
8127 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008128 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008129 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008130 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008131 return;
8132 }
Benny Halevycbe82602011-05-22 19:52:37 +03008133 dprintk("<-- %s\n", __func__);
8134}
8135
8136static void nfs4_layoutreturn_release(void *calldata)
8137{
8138 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008139 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008140 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008141
8142 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008143 spin_lock(&lo->plh_inode->i_lock);
Jeff Layton6d597e12016-05-17 12:28:42 -04008144 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range,
8145 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust45fcc7b2016-07-24 12:26:34 -04008146 if (lrp->res.lrs_present && pnfs_layout_is_valid(lo))
Trond Myklebust849b2862012-09-24 14:18:39 -04008147 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05008148 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008149 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008150 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008151 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008152 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008153 kfree(calldata);
8154 dprintk("<-- %s\n", __func__);
8155}
8156
8157static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8158 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8159 .rpc_call_done = nfs4_layoutreturn_done,
8160 .rpc_release = nfs4_layoutreturn_release,
8161};
8162
Peng Tao6c166052014-11-17 09:30:40 +08008163int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008164{
8165 struct rpc_task *task;
8166 struct rpc_message msg = {
8167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8168 .rpc_argp = &lrp->args,
8169 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008170 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008171 };
8172 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008173 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008174 .rpc_message = &msg,
8175 .callback_ops = &nfs4_layoutreturn_call_ops,
8176 .callback_data = lrp,
8177 };
Peng Tao6c166052014-11-17 09:30:40 +08008178 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008179
Andrew Elble99ade3c2015-12-02 09:39:51 -05008180 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8181 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8182 &task_setup_data.rpc_client, &msg);
8183
Benny Halevycbe82602011-05-22 19:52:37 +03008184 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008185 if (!sync) {
8186 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8187 if (!lrp->inode) {
8188 nfs4_layoutreturn_release(lrp);
8189 return -EAGAIN;
8190 }
8191 task_setup_data.flags |= RPC_TASK_ASYNC;
8192 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008193 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008194 task = rpc_run_task(&task_setup_data);
8195 if (IS_ERR(task))
8196 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008197 if (sync)
8198 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008199 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008200 dprintk("<-- %s status=%d\n", __func__, status);
8201 rpc_put_task(task);
8202 return status;
8203}
8204
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008205static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008206_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8207 struct pnfs_device *pdev,
8208 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008209{
8210 struct nfs4_getdeviceinfo_args args = {
8211 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008212 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8213 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008214 };
8215 struct nfs4_getdeviceinfo_res res = {
8216 .pdev = pdev,
8217 };
8218 struct rpc_message msg = {
8219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8220 .rpc_argp = &args,
8221 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008222 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008223 };
8224 int status;
8225
8226 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008227 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008228 if (res.notification & ~args.notify_types)
8229 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008230 if (res.notification != args.notify_types)
8231 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008232
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008233 dprintk("<-- %s status=%d\n", __func__, status);
8234
8235 return status;
8236}
8237
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008238int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8239 struct pnfs_device *pdev,
8240 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008241{
8242 struct nfs4_exception exception = { };
8243 int err;
8244
8245 do {
8246 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008247 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008248 &exception);
8249 } while (exception.retry);
8250 return err;
8251}
8252EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8253
Andy Adamson863a3c62011-03-23 13:27:54 +00008254static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8255{
8256 struct nfs4_layoutcommit_data *data = calldata;
8257 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008258 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008259
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008260 nfs41_setup_sequence(session,
8261 &data->args.seq_args,
8262 &data->res.seq_res,
8263 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008264}
8265
8266static void
8267nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8268{
8269 struct nfs4_layoutcommit_data *data = calldata;
8270 struct nfs_server *server = NFS_SERVER(data->args.inode);
8271
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008272 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008273 return;
8274
8275 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008276 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8277 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8278 case -NFS4ERR_BADLAYOUT: /* no layout */
8279 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008280 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008281 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008282 break;
8283 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008284 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008285 rpc_restart_call_prepare(task);
8286 return;
8287 }
8288 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008289}
8290
8291static void nfs4_layoutcommit_release(void *calldata)
8292{
8293 struct nfs4_layoutcommit_data *data = calldata;
8294
Andy Adamsondb29c082011-07-30 20:52:38 -04008295 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008296 nfs_post_op_update_inode_force_wcc(data->args.inode,
8297 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008298 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008299 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008300 kfree(data);
8301}
8302
8303static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8304 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8305 .rpc_call_done = nfs4_layoutcommit_done,
8306 .rpc_release = nfs4_layoutcommit_release,
8307};
8308
8309int
Andy Adamsonef311532011-03-12 02:58:10 -05008310nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008311{
8312 struct rpc_message msg = {
8313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8314 .rpc_argp = &data->args,
8315 .rpc_resp = &data->res,
8316 .rpc_cred = data->cred,
8317 };
8318 struct rpc_task_setup task_setup_data = {
8319 .task = &data->task,
8320 .rpc_client = NFS_CLIENT(data->args.inode),
8321 .rpc_message = &msg,
8322 .callback_ops = &nfs4_layoutcommit_ops,
8323 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008324 };
8325 struct rpc_task *task;
8326 int status = 0;
8327
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008328 dprintk("NFS: initiating layoutcommit call. sync %d "
8329 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008330 data->args.lastbytewritten,
8331 data->args.inode->i_ino);
8332
Trond Myklebust472e2592015-02-05 16:50:30 -05008333 if (!sync) {
8334 data->inode = nfs_igrab_and_active(data->args.inode);
8335 if (data->inode == NULL) {
8336 nfs4_layoutcommit_release(data);
8337 return -EAGAIN;
8338 }
8339 task_setup_data.flags = RPC_TASK_ASYNC;
8340 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008341 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008342 task = rpc_run_task(&task_setup_data);
8343 if (IS_ERR(task))
8344 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008345 if (sync)
8346 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008347 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008348 dprintk("%s: status %d\n", __func__, status);
8349 rpc_put_task(task);
8350 return status;
8351}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008352
Andy Adamson97431202013-08-08 10:57:56 -04008353/**
8354 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8355 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8356 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008357static int
8358_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008359 struct nfs_fsinfo *info,
8360 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008361{
8362 struct nfs41_secinfo_no_name_args args = {
8363 .style = SECINFO_STYLE_CURRENT_FH,
8364 };
8365 struct nfs4_secinfo_res res = {
8366 .flavors = flavors,
8367 };
8368 struct rpc_message msg = {
8369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8370 .rpc_argp = &args,
8371 .rpc_resp = &res,
8372 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008373 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008374 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008375 int status;
8376
8377 if (use_integrity) {
8378 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008379 cred = nfs4_get_clid_cred(server->nfs_client);
8380 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008381 }
8382
8383 dprintk("--> %s\n", __func__);
8384 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8385 &res.seq_res, 0);
8386 dprintk("<-- %s status=%d\n", __func__, status);
8387
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008388 if (cred)
8389 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008390
8391 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008392}
8393
8394static int
8395nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8396 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8397{
8398 struct nfs4_exception exception = { };
8399 int err;
8400 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008401 /* first try using integrity protection */
8402 err = -NFS4ERR_WRONGSEC;
8403
8404 /* try to use integrity protection with machine cred */
8405 if (_nfs4_is_integrity_protected(server->nfs_client))
8406 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8407 flavors, true);
8408
8409 /*
8410 * if unable to use integrity protection, or SECINFO with
8411 * integrity protection returns NFS4ERR_WRONGSEC (which is
8412 * disallowed by spec, but exists in deployed servers) use
8413 * the current filesystem's rpc_client and the user cred.
8414 */
8415 if (err == -NFS4ERR_WRONGSEC)
8416 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8417 flavors, false);
8418
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008419 switch (err) {
8420 case 0:
8421 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008422 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008423 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008424 default:
8425 err = nfs4_handle_exception(server, err, &exception);
8426 }
8427 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008428out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008429 return err;
8430}
8431
8432static int
8433nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8434 struct nfs_fsinfo *info)
8435{
8436 int err;
8437 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008438 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008439 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008440 struct nfs4_secinfo4 *secinfo;
8441 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008442
8443 page = alloc_page(GFP_KERNEL);
8444 if (!page) {
8445 err = -ENOMEM;
8446 goto out;
8447 }
8448
8449 flavors = page_address(page);
8450 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8451
8452 /*
8453 * Fall back on "guess and check" method if
8454 * the server doesn't support SECINFO_NO_NAME
8455 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008456 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008457 err = nfs4_find_root_sec(server, fhandle, info);
8458 goto out_freepage;
8459 }
8460 if (err)
8461 goto out_freepage;
8462
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008463 for (i = 0; i < flavors->num_flavors; i++) {
8464 secinfo = &flavors->flavors[i];
8465
8466 switch (secinfo->flavor) {
8467 case RPC_AUTH_NULL:
8468 case RPC_AUTH_UNIX:
8469 case RPC_AUTH_GSS:
8470 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8471 &secinfo->flavor_info);
8472 break;
8473 default:
8474 flavor = RPC_AUTH_MAXFLAVOR;
8475 break;
8476 }
8477
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008478 if (!nfs_auth_info_match(&server->auth_info, flavor))
8479 flavor = RPC_AUTH_MAXFLAVOR;
8480
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008481 if (flavor != RPC_AUTH_MAXFLAVOR) {
8482 err = nfs4_lookup_root_sec(server, fhandle,
8483 info, flavor);
8484 if (!err)
8485 break;
8486 }
8487 }
8488
8489 if (flavor == RPC_AUTH_MAXFLAVOR)
8490 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008491
8492out_freepage:
8493 put_page(page);
8494 if (err == -EACCES)
8495 return -EPERM;
8496out:
8497 return err;
8498}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008499
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008500static int _nfs41_test_stateid(struct nfs_server *server,
8501 nfs4_stateid *stateid,
8502 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008503{
8504 int status;
8505 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008506 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008507 };
8508 struct nfs41_test_stateid_res res;
8509 struct rpc_message msg = {
8510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8511 .rpc_argp = &args,
8512 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008513 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008514 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008515 struct rpc_clnt *rpc_client = server->client;
8516
8517 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8518 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008519
Chuck Lever38527b12012-07-11 16:30:23 -04008520 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008521 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008522 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008523 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008524 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008525 if (status != NFS_OK) {
8526 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008527 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008528 }
8529 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008530 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008531}
8532
Chuck Lever38527b12012-07-11 16:30:23 -04008533/**
8534 * nfs41_test_stateid - perform a TEST_STATEID operation
8535 *
8536 * @server: server / transport on which to perform the operation
8537 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008538 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008539 *
8540 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8541 * Otherwise a negative NFS4ERR value is returned if the operation
8542 * failed or the state ID is not currently valid.
8543 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008544static int nfs41_test_stateid(struct nfs_server *server,
8545 nfs4_stateid *stateid,
8546 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008547{
8548 struct nfs4_exception exception = { };
8549 int err;
8550 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008551 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008552 if (err != -NFS4ERR_DELAY)
8553 break;
8554 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008555 } while (exception.retry);
8556 return err;
8557}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008558
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008559struct nfs_free_stateid_data {
8560 struct nfs_server *server;
8561 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008562 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008563};
8564
8565static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8566{
8567 struct nfs_free_stateid_data *data = calldata;
8568 nfs41_setup_sequence(nfs4_get_session(data->server),
8569 &data->args.seq_args,
8570 &data->res.seq_res,
8571 task);
8572}
8573
8574static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8575{
8576 struct nfs_free_stateid_data *data = calldata;
8577
8578 nfs41_sequence_done(task, &data->res.seq_res);
8579
8580 switch (task->tk_status) {
8581 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008582 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008583 rpc_restart_call_prepare(task);
8584 }
8585}
8586
8587static void nfs41_free_stateid_release(void *calldata)
8588{
8589 kfree(calldata);
8590}
8591
Trond Myklebust17f26b12013-08-21 15:48:42 -04008592static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008593 .rpc_call_prepare = nfs41_free_stateid_prepare,
8594 .rpc_call_done = nfs41_free_stateid_done,
8595 .rpc_release = nfs41_free_stateid_release,
8596};
8597
8598static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8599 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008600 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008601 bool privileged)
8602{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008603 struct rpc_message msg = {
8604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008605 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008606 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008607 struct rpc_task_setup task_setup = {
8608 .rpc_client = server->client,
8609 .rpc_message = &msg,
8610 .callback_ops = &nfs41_free_stateid_ops,
8611 .flags = RPC_TASK_ASYNC,
8612 };
8613 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008614
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008615 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8616 &task_setup.rpc_client, &msg);
8617
Chuck Lever38527b12012-07-11 16:30:23 -04008618 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008619 data = kmalloc(sizeof(*data), GFP_NOFS);
8620 if (!data)
8621 return ERR_PTR(-ENOMEM);
8622 data->server = server;
8623 nfs4_stateid_copy(&data->args.stateid, stateid);
8624
8625 task_setup.callback_data = data;
8626
8627 msg.rpc_argp = &data->args;
8628 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008629 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008630 if (privileged)
8631 nfs4_set_sequence_privileged(&data->args.seq_args);
8632
8633 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008634}
8635
Chuck Lever38527b12012-07-11 16:30:23 -04008636/**
8637 * nfs41_free_stateid - perform a FREE_STATEID operation
8638 *
8639 * @server: server / transport on which to perform the operation
8640 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008641 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008642 *
8643 * Returns NFS_OK if the server freed "stateid". Otherwise a
8644 * negative NFS4ERR value is returned.
8645 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008646static int nfs41_free_stateid(struct nfs_server *server,
8647 nfs4_stateid *stateid,
8648 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008649{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008650 struct rpc_task *task;
8651 int ret;
8652
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008653 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008654 if (IS_ERR(task))
8655 return PTR_ERR(task);
8656 ret = rpc_wait_for_completion_task(task);
8657 if (!ret)
8658 ret = task->tk_status;
8659 rpc_put_task(task);
8660 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008661}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008662
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008663static void
8664nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008665{
8666 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008667 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008668
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008669 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008670 nfs4_free_lock_state(server, lsp);
8671 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008672 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008673 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008674}
8675
Trond Myklebust36281ca2012-03-04 18:13:56 -05008676static bool nfs41_match_stateid(const nfs4_stateid *s1,
8677 const nfs4_stateid *s2)
8678{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008679 if (s1->type != s2->type)
8680 return false;
8681
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008682 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008683 return false;
8684
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008685 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008686 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008687 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008688 return true;
8689
8690 return false;
8691}
8692
Andy Adamson557134a2009-04-01 09:21:53 -04008693#endif /* CONFIG_NFS_V4_1 */
8694
Trond Myklebust36281ca2012-03-04 18:13:56 -05008695static bool nfs4_match_stateid(const nfs4_stateid *s1,
8696 const nfs4_stateid *s2)
8697{
Trond Myklebustf597c532012-03-04 18:13:56 -05008698 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008699}
8700
8701
Trond Myklebust17280172012-03-11 13:11:00 -04008702static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008703 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008704 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 .recover_open = nfs4_open_reclaim,
8706 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008707 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008708 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709};
8710
Andy Adamson591d71c2009-04-01 09:22:47 -04008711#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008712static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008713 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8714 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8715 .recover_open = nfs4_open_reclaim,
8716 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008717 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008718 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008719 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008720};
8721#endif /* CONFIG_NFS_V4_1 */
8722
Trond Myklebust17280172012-03-11 13:11:00 -04008723static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008724 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008725 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008726 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008728 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729};
8730
Andy Adamson591d71c2009-04-01 09:22:47 -04008731#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008732static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008733 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8734 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008735 .recover_open = nfs41_open_expired,
8736 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008737 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008738};
8739#endif /* CONFIG_NFS_V4_1 */
8740
Trond Myklebust17280172012-03-11 13:11:00 -04008741static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008742 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008743 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008744 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008745};
8746
8747#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008748static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008749 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008750 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008751 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008752};
8753#endif
8754
Chuck Leverec011fe2013-10-17 14:12:39 -04008755static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008756 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008757 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008758};
8759
8760#if defined(CONFIG_NFS_V4_1)
8761static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008762 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008763 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008764};
8765#endif /* CONFIG_NFS_V4_1 */
8766
Trond Myklebust97dc1352010-06-16 09:52:26 -04008767static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8768 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008769 .init_caps = NFS_CAP_READDIRPLUS
8770 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008771 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008772 .init_client = nfs40_init_client,
8773 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008774 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008775 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008776 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008777 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008778 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008779 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8780 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8781 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008782 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008783};
8784
8785#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008786static struct nfs_seqid *
8787nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8788{
8789 return NULL;
8790}
8791
Trond Myklebust97dc1352010-06-16 09:52:26 -04008792static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8793 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008794 .init_caps = NFS_CAP_READDIRPLUS
8795 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008796 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008797 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008798 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008799 .init_client = nfs41_init_client,
8800 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008801 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008802 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008803 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008804 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008805 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008806 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8807 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8808 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008809 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008810};
8811#endif
8812
Steve Dickson42c2c422013-05-22 12:50:38 -04008813#if defined(CONFIG_NFS_V4_2)
8814static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8815 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008816 .init_caps = NFS_CAP_READDIRPLUS
8817 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008818 | NFS_CAP_POSIX_LOCK
8819 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008820 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008821 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008822 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008823 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008824 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008825 | NFS_CAP_LAYOUTSTATS
8826 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008827 .init_client = nfs41_init_client,
8828 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008829 .match_stateid = nfs41_match_stateid,
8830 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008831 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008832 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008833 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008834 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8835 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8836 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008837 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008838};
8839#endif
8840
Trond Myklebust97dc1352010-06-16 09:52:26 -04008841const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8842 [0] = &nfs_v4_0_minor_ops,
8843#if defined(CONFIG_NFS_V4_1)
8844 [1] = &nfs_v4_1_minor_ops,
8845#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008846#if defined(CONFIG_NFS_V4_2)
8847 [2] = &nfs_v4_2_minor_ops,
8848#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008849};
8850
Trond Myklebust13997822016-07-24 17:10:52 -04008851static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008852{
8853 ssize_t error, error2;
8854
8855 error = generic_listxattr(dentry, list, size);
8856 if (error < 0)
8857 return error;
8858 if (list) {
8859 list += error;
8860 size -= error;
8861 }
8862
8863 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8864 if (error2 < 0)
8865 return error2;
8866 return error + error2;
8867}
8868
Trond Myklebust17f26b12013-08-21 15:48:42 -04008869static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008870 .create = nfs_create,
8871 .lookup = nfs_lookup,
8872 .atomic_open = nfs_atomic_open,
8873 .link = nfs_link,
8874 .unlink = nfs_unlink,
8875 .symlink = nfs_symlink,
8876 .mkdir = nfs_mkdir,
8877 .rmdir = nfs_rmdir,
8878 .mknod = nfs_mknod,
8879 .rename = nfs_rename,
8880 .permission = nfs_permission,
8881 .getattr = nfs_getattr,
8882 .setattr = nfs_setattr,
8883 .getxattr = generic_getxattr,
8884 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008885 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04008886 .removexattr = generic_removexattr,
8887};
8888
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008889static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008890 .permission = nfs_permission,
8891 .getattr = nfs_getattr,
8892 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008893 .getxattr = generic_getxattr,
8894 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008895 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008896 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008897};
8898
David Howells509de812006-08-22 20:06:11 -04008899const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 .version = 4, /* protocol version */
8901 .dentry_ops = &nfs4_dentry_operations,
8902 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008903 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008904 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008906 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008907 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 .getattr = nfs4_proc_getattr,
8909 .setattr = nfs4_proc_setattr,
8910 .lookup = nfs4_proc_lookup,
8911 .access = nfs4_proc_access,
8912 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913 .create = nfs4_proc_create,
8914 .remove = nfs4_proc_remove,
8915 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008916 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008918 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008919 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008920 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 .link = nfs4_proc_link,
8922 .symlink = nfs4_proc_symlink,
8923 .mkdir = nfs4_proc_mkdir,
8924 .rmdir = nfs4_proc_remove,
8925 .readdir = nfs4_proc_readdir,
8926 .mknod = nfs4_proc_mknod,
8927 .statfs = nfs4_proc_statfs,
8928 .fsinfo = nfs4_proc_fsinfo,
8929 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008930 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008932 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008934 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008936 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008938 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008939 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008941 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008942 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008943 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008944 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008945 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008946 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008947 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008948 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008949 .create_server = nfs4_create_server,
8950 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951};
8952
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008953static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01008954 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008955 .list = nfs4_xattr_list_nfs4_acl,
8956 .get = nfs4_xattr_get_nfs4_acl,
8957 .set = nfs4_xattr_set_nfs4_acl,
8958};
8959
8960const struct xattr_handler *nfs4_xattr_handlers[] = {
8961 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008962#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8963 &nfs4_xattr_nfs4_label_handler,
8964#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008965 NULL
8966};
8967
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968/*
8969 * Local variables:
8970 * c-basic-offset: 8
8971 * End:
8972 */