blob: a74c4167b5b08a9882009f2fce6ab15f50dde8dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400366 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500368 int ret = errorcode;
369
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400370 exception->delay = 0;
371 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 exception->retry = 0;
373 switch(errorcode) {
374 case 0:
375 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500376 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400377 case -NFS4ERR_DELEG_REVOKED:
378 case -NFS4ERR_ADMIN_REVOKED:
379 case -NFS4ERR_BAD_STATEID:
Trond Myklebust8487c472016-06-26 08:44:35 -0400380 if (inode) {
381 int err;
382
383 err = nfs_async_inode_return_delegation(inode,
384 stateid);
385 if (err == 0)
386 goto wait_on_recovery;
387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
388 exception->retry = 1;
389 break;
390 }
391 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 if (state == NULL)
393 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400394 ret = nfs4_schedule_stateid_recovery(server, state);
395 if (ret < 0)
396 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400398 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400399 if (state != NULL) {
400 ret = nfs4_schedule_stateid_recovery(server, state);
401 if (ret < 0)
402 break;
403 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500405 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500406 nfs4_schedule_lease_recovery(clp);
407 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400408 case -NFS4ERR_MOVED:
409 ret = nfs4_schedule_migration_recovery(server);
410 if (ret < 0)
411 break;
412 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400413 case -NFS4ERR_LEASE_MOVED:
414 nfs4_schedule_lease_moved_recovery(clp);
415 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500416#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400417 case -NFS4ERR_BADSESSION:
418 case -NFS4ERR_BADSLOT:
419 case -NFS4ERR_BAD_HIGH_SLOT:
420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
421 case -NFS4ERR_DEADSESSION:
422 case -NFS4ERR_SEQ_FALSE_RETRY:
423 case -NFS4ERR_SEQ_MISORDERED:
424 dprintk("%s ERROR: %d Reset session\n", __func__,
425 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400426 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400427 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500428#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500430 if (exception->timeout > HZ) {
431 /* We have retried a decent amount, time to
432 * fail
433 */
434 ret = -EBUSY;
435 break;
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400438 nfs_inc_server_stats(server, NFSIOS_DELAY);
439 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400440 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400441 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400442 exception->delay = 1;
443 return 0;
444
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500446 case -NFS4ERR_OLD_STATEID:
447 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800448 break;
449 case -NFS4ERR_BADOWNER:
450 /* The following works around a Linux server bug! */
451 case -NFS4ERR_BADNAME:
452 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
453 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
454 exception->retry = 1;
455 printk(KERN_WARNING "NFS: v4 server %s "
456 "does not accept raw "
457 "uid/gids. "
458 "Reenabling the idmapper.\n",
459 server->nfs_client->cl_hostname);
460 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 }
462 /* We failed to handle the error */
463 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500464wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->recovering = 1;
466 return 0;
467}
468
469/* This is the error handling routine for processes that are allowed
470 * to sleep.
471 */
472int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
473{
474 struct nfs_client *clp = server->nfs_client;
475 int ret;
476
477 ret = nfs4_do_handle_exception(server, errorcode, exception);
478 if (exception->delay) {
479 ret = nfs4_delay(server->client, &exception->timeout);
480 goto out_retry;
481 }
482 if (exception->recovering) {
483 ret = nfs4_wait_clnt_recover(clp);
484 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
485 return -EIO;
486 goto out_retry;
487 }
488 return ret;
489out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500490 if (ret == 0)
491 exception->retry = 1;
492 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500493}
494
Trond Myklebust037fc982015-09-20 15:51:00 -0400495static int
496nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
497 int errorcode, struct nfs4_exception *exception)
498{
499 struct nfs_client *clp = server->nfs_client;
500 int ret;
501
502 ret = nfs4_do_handle_exception(server, errorcode, exception);
503 if (exception->delay) {
504 rpc_delay(task, nfs4_update_delay(&exception->timeout));
505 goto out_retry;
506 }
507 if (exception->recovering) {
508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
511 goto out_retry;
512 }
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 ret = -EIO;
515 return ret;
516out_retry:
517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
520}
521
522static int
523nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
524 struct nfs4_state *state, long *timeout)
525{
526 struct nfs4_exception exception = {
527 .state = state,
528 };
529
530 if (task->tk_status >= 0)
531 return 0;
532 if (timeout)
533 exception.timeout = *timeout;
534 task->tk_status = nfs4_async_handle_exception(task, server,
535 task->tk_status,
536 &exception);
537 if (exception.delay && timeout)
538 *timeout = exception.timeout;
539 if (exception.retry)
540 return -EAGAIN;
541 return 0;
542}
543
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400544/*
545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
546 * or 'false' otherwise.
547 */
548static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
549{
550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
551
552 if (flavor == RPC_AUTH_GSS_KRB5I ||
553 flavor == RPC_AUTH_GSS_KRB5P)
554 return true;
555
556 return false;
557}
Trond Myklebust65de8722008-12-23 15:21:44 -0500558
Trond Myklebust452e9352010-07-31 14:29:06 -0400559static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&clp->cl_lock);
562 if (time_before(clp->cl_last_renewal,timestamp))
563 clp->cl_last_renewal = timestamp;
564 spin_unlock(&clp->cl_lock);
565}
566
Trond Myklebust452e9352010-07-31 14:29:06 -0400567static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
568{
Trond Myklebustbe824162015-07-05 14:50:46 -0400569 struct nfs_client *clp = server->nfs_client;
570
571 if (!nfs4_has_session(clp))
572 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400573}
574
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400575struct nfs4_call_sync_data {
576 const struct nfs_server *seq_server;
577 struct nfs4_sequence_args *seq_args;
578 struct nfs4_sequence_res *seq_res;
579};
580
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800581void nfs4_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400583{
584 args->sa_slot = NULL;
585 args->sa_cache_this = cache_reply;
586 args->sa_privileged = 0;
587
588 res->sr_slot = NULL;
589}
590
591static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
592{
593 args->sa_privileged = 1;
594}
595
Peng Taocb04ad22014-06-11 05:24:15 +0800596int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400600{
Chuck Lever3bd23842013-08-09 12:49:19 -0400601 struct nfs4_slot *slot;
602
603 /* slot already allocated? */
604 if (res->sr_slot != NULL)
605 goto out_start;
606
607 spin_lock(&tbl->slot_tbl_lock);
608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
609 goto out_sleep;
610
611 slot = nfs4_alloc_slot(tbl);
612 if (IS_ERR(slot)) {
613 if (slot == ERR_PTR(-ENOMEM))
614 task->tk_timeout = HZ >> 2;
615 goto out_sleep;
616 }
617 spin_unlock(&tbl->slot_tbl_lock);
618
619 args->sa_slot = slot;
620 res->sr_slot = slot;
621
622out_start:
623 rpc_call_start(task);
624 return 0;
625
626out_sleep:
627 if (args->sa_privileged)
628 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
629 NULL, RPC_PRIORITY_PRIVILEGED);
630 else
631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
633 return -EAGAIN;
634}
Peng Taocb04ad22014-06-11 05:24:15 +0800635EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400636
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400637static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400638{
639 struct nfs4_slot *slot = res->sr_slot;
640 struct nfs4_slot_table *tbl;
641
Chuck Lever3bd23842013-08-09 12:49:19 -0400642 tbl = slot->table;
643 spin_lock(&tbl->slot_tbl_lock);
644 if (!nfs41_wake_and_assign_slot(tbl, slot))
645 nfs4_free_slot(tbl, slot);
646 spin_unlock(&tbl->slot_tbl_lock);
647
648 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400649}
650
651static int nfs40_sequence_done(struct rpc_task *task,
652 struct nfs4_sequence_res *res)
653{
654 if (res->sr_slot != NULL)
655 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400656 return 1;
657}
658
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400659#if defined(CONFIG_NFS_V4_1)
660
Trond Myklebustd185a332010-06-16 09:52:25 -0400661static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400662{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500663 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400664 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500665 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500666 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400667
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500668 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500669 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500670
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400671 /* Bump the slot sequence number */
672 if (slot->seq_done)
673 slot->seq_nr++;
674 slot->seq_done = 0;
675
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500676 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 /* Be nice to the server: try to ensure that the last transmitted
678 * value for highest_user_slotid <= target_highest_slotid
679 */
680 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
681 send_new_highest_used_slotid = true;
682
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500683 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500684 send_new_highest_used_slotid = false;
685 goto out_unlock;
686 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500688
689 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
690 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500691out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500692 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400693 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500694 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400695 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400696 if (waitqueue_active(&tbl->slot_waitq))
697 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400698}
699
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400700static int nfs41_sequence_process(struct rpc_task *task,
701 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400702{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500703 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500704 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400705 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500706 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500707 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400708
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500709 if (slot == NULL)
710 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400711 /* don't increment the sequence number if the task wasn't sent */
712 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400713 goto out;
714
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500715 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500716
Trond Myklebustac20d162012-12-15 15:36:07 -0500717 if (slot->interrupted) {
718 slot->interrupted = 0;
719 interrupted = true;
720 }
721
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400722 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500723 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 switch (res->sr_status) {
725 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400726 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400727 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500728 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500729 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500730 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400731 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500732 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400733 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500734 case 1:
735 /*
736 * sr_status remains 1 if an RPC level error occurred.
737 * The server may or may not have processed the sequence
738 * operation..
739 * Mark the slot as having hosted an interrupted RPC call.
740 */
741 slot->interrupted = 1;
742 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 case -NFS4ERR_DELAY:
744 /* The server detected a resend of the RPC call and
745 * returned NFS4ERR_DELAY as per Section 2.10.6.2
746 * of RFC5661.
747 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500748 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400749 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500750 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500751 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400752 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500753 case -NFS4ERR_BADSLOT:
754 /*
755 * The slot id we used was probably retired. Try again
756 * using a different slot id.
757 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500758 goto retry_nowait;
759 case -NFS4ERR_SEQ_MISORDERED:
760 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500761 * Was the last operation on this sequence interrupted?
762 * If so, retry after bumping the sequence number.
763 */
764 if (interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
768 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500769 * Could this slot have been previously retired?
770 * If so, then the server may be expecting seq_nr = 1!
771 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500772 if (slot->seq_nr != 1) {
773 slot->seq_nr = 1;
774 goto retry_nowait;
775 }
776 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500777 case -NFS4ERR_SEQ_FALSE_RETRY:
778 ++slot->seq_nr;
779 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400780 default:
781 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400782 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400783 }
784out:
785 /* The session may be reset by one of the error handlers. */
786 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500787out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500788 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500789retry_nowait:
790 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400791 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500792 task->tk_status = 0;
793 ret = 0;
794 }
795 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400796out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400797 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 goto out;
799 rpc_delay(task, NFS4_POLL_RETRY_MAX);
800 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400802
803int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
804{
805 if (!nfs41_sequence_process(task, res))
806 return 0;
807 if (res->sr_slot != NULL)
808 nfs41_sequence_free_slot(res);
809 return 1;
810
811}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500812EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400813
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400814static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
815{
816 if (res->sr_slot == NULL)
817 return 1;
818 if (res->sr_slot->table->session != NULL)
819 return nfs41_sequence_process(task, res);
820 return nfs40_sequence_done(task, res);
821}
822
823static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
824{
825 if (res->sr_slot != NULL) {
826 if (res->sr_slot->table->session != NULL)
827 nfs41_sequence_free_slot(res);
828 else
829 nfs40_sequence_free_slot(res);
830 }
831}
832
Peng Tao2c4b1312014-06-11 05:24:16 +0800833int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400834{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500835 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400837 if (!res->sr_slot->table->session)
838 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400840}
Peng Tao2c4b1312014-06-11 05:24:16 +0800841EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000843int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844 struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846 struct rpc_task *task)
847{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400848 struct nfs4_slot *slot;
849 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400850
851 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400853 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400854 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400856 tbl = &session->fc_slot_table;
857
Trond Myklebust69d206b2012-11-22 13:21:02 -0500858 task->tk_timeout = 0;
859
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400860 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400861 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400862 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500863 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500864 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400865 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400866 }
867
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500868 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500869 if (IS_ERR(slot)) {
870 /* If out of memory, try again in 1/4 second */
871 if (slot == ERR_PTR(-ENOMEM))
872 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400873 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400874 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400875 }
876 spin_unlock(&tbl->slot_tbl_lock);
877
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500878 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400879
Chuck Levere8d92382013-08-09 12:47:51 -0400880 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500881 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400882
Benny Halevydfb4f3092010-09-24 09:17:01 -0400883 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500884 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400885 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400886 /*
887 * sr_status is only set in decode_sequence, and so will remain
888 * set to 1 if an rpc level failure occurs.
889 */
890 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400891 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400892out_success:
893 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400895out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400896 /* Privileged tasks are queued with top priority */
897 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400898 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
899 NULL, RPC_PRIORITY_PRIVILEGED);
900 else
901 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400902 spin_unlock(&tbl->slot_tbl_lock);
903 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400904}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000905EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400906
Chuck Lever5a580e02013-08-09 12:48:09 -0400907static int nfs4_setup_sequence(const struct nfs_server *server,
908 struct nfs4_sequence_args *args,
909 struct nfs4_sequence_res *res,
910 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400911{
Trond Myklebust035168a2010-06-16 09:52:26 -0400912 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400913 int ret = 0;
914
Chuck Lever3bd23842013-08-09 12:49:19 -0400915 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800916 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
917 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400918
Chuck Levere8d92382013-08-09 12:47:51 -0400919 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400920 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400921 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400922
Trond Myklebust9d12b212012-01-17 22:04:25 -0500923 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400924
Andy Adamsonce5039c2009-04-01 09:22:13 -0400925 dprintk("<-- %s status=%d\n", __func__, ret);
926 return ret;
927}
928
Andy Adamsonce5039c2009-04-01 09:22:13 -0400929static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
930{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400931 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400932 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400933
Trond Myklebust035168a2010-06-16 09:52:26 -0400934 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
935
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937}
938
Andy Adamson69ab40c2009-04-01 09:22:19 -0400939static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
940{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400941 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400942
Trond Myklebust14516c32010-07-31 14:29:06 -0400943 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400944}
945
Trond Myklebust17280172012-03-11 13:11:00 -0400946static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400948 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400949};
950
Chuck Lever3bd23842013-08-09 12:49:19 -0400951#else /* !CONFIG_NFS_V4_1 */
952
Chuck Lever5a580e02013-08-09 12:48:09 -0400953static int nfs4_setup_sequence(const struct nfs_server *server,
954 struct nfs4_sequence_args *args,
955 struct nfs4_sequence_res *res,
956 struct rpc_task *task)
957{
Peng Taocb04ad22014-06-11 05:24:15 +0800958 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
959 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400960}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400961
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400962static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
963{
964 return nfs40_sequence_done(task, res);
965}
966
967static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
968{
969 if (res->sr_slot != NULL)
970 nfs40_sequence_free_slot(res);
971}
972
Peng Tao2c4b1312014-06-11 05:24:16 +0800973int nfs4_sequence_done(struct rpc_task *task,
974 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400975{
Chuck Lever3bd23842013-08-09 12:49:19 -0400976 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400977}
Peng Tao2c4b1312014-06-11 05:24:16 +0800978EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400979
980#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400981
Chuck Lever9915ea72013-08-09 12:48:27 -0400982static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
983{
984 struct nfs4_call_sync_data *data = calldata;
985 nfs4_setup_sequence(data->seq_server,
986 data->seq_args, data->seq_res, task);
987}
988
989static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
990{
991 struct nfs4_call_sync_data *data = calldata;
992 nfs4_sequence_done(task, data->seq_res);
993}
994
995static const struct rpc_call_ops nfs40_call_sync_ops = {
996 .rpc_call_prepare = nfs40_call_sync_prepare,
997 .rpc_call_done = nfs40_call_sync_done,
998};
999
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001001 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 struct nfs4_sequence_args *args,
1004 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001005{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 int ret;
1007 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001008 struct nfs_client *clp = server->nfs_client;
1009 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 .seq_server = server,
1011 .seq_args = args,
1012 .seq_res = res,
1013 };
1014 struct rpc_task_setup task_setup = {
1015 .rpc_client = clnt,
1016 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001017 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 .callback_data = &data
1019 };
1020
1021 task = rpc_run_task(&task_setup);
1022 if (IS_ERR(task))
1023 ret = PTR_ERR(task);
1024 else {
1025 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001026 rpc_put_task(task);
1027 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001028 return ret;
1029}
1030
Bryan Schumaker7c513052011-03-24 17:12:24 +00001031int nfs4_call_sync(struct rpc_clnt *clnt,
1032 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001033 struct rpc_message *msg,
1034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1036 int cache_reply)
1037{
Chuck Levera9c92d62013-08-09 12:48:18 -04001038 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001039 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1043{
1044 struct nfs_inode *nfsi = NFS_I(dir);
1045
1046 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001047 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001048 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001050 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001051 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001052 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 spin_unlock(&dir->i_lock);
1054}
1055
1056struct nfs4_opendata {
1057 struct kref kref;
1058 struct nfs_openargs o_arg;
1059 struct nfs_openres o_res;
1060 struct nfs_open_confirmargs c_arg;
1061 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001062 struct nfs4_string owner_name;
1063 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001064 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001066 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001068 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct nfs4_state_owner *owner;
1070 struct nfs4_state *state;
1071 struct iattr attrs;
1072 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001073 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001074 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001075 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001076 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int cancelled;
1078};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001079
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001080static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1081 int err, struct nfs4_exception *exception)
1082{
1083 if (err != -EINVAL)
1084 return false;
1085 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1086 return false;
1087 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1088 exception->retry = 1;
1089 return true;
1090}
1091
Trond Myklebust6ae37332015-01-30 14:21:14 -05001092static u32
1093nfs4_map_atomic_open_share(struct nfs_server *server,
1094 fmode_t fmode, int openflags)
1095{
1096 u32 res = 0;
1097
1098 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1099 case FMODE_READ:
1100 res = NFS4_SHARE_ACCESS_READ;
1101 break;
1102 case FMODE_WRITE:
1103 res = NFS4_SHARE_ACCESS_WRITE;
1104 break;
1105 case FMODE_READ|FMODE_WRITE:
1106 res = NFS4_SHARE_ACCESS_BOTH;
1107 }
1108 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1109 goto out;
1110 /* Want no delegation if we're using O_DIRECT */
1111 if (openflags & O_DIRECT)
1112 res |= NFS4_SHARE_WANT_NO_DELEG;
1113out:
1114 return res;
1115}
1116
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001117static enum open_claim_type4
1118nfs4_map_atomic_open_claim(struct nfs_server *server,
1119 enum open_claim_type4 claim)
1120{
1121 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1122 return claim;
1123 switch (claim) {
1124 default:
1125 return claim;
1126 case NFS4_OPEN_CLAIM_FH:
1127 return NFS4_OPEN_CLAIM_NULL;
1128 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1129 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1130 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1131 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1132 }
1133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001136{
1137 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001138 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139 p->o_res.seqid = p->o_arg.seqid;
1140 p->c_res.seqid = p->c_arg.seqid;
1141 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001142 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001144 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145}
1146
Al Viro82a2c1b2011-06-22 18:30:55 -04001147static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001148 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001149 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001150 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001151 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001152 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153{
Al Viro82a2c1b2011-06-22 18:30:55 -04001154 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001155 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001157 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001158 struct nfs4_opendata *p;
1159
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001160 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 if (p == NULL)
1162 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001163
1164 p->f_label = nfs4_label_alloc(server, gfp_mask);
1165 if (IS_ERR(p->f_label))
1166 goto err_free_p;
1167
Kinglong Meea49c2692015-07-27 15:31:38 +08001168 p->a_label = nfs4_label_alloc(server, gfp_mask);
1169 if (IS_ERR(p->a_label))
1170 goto err_free_f;
1171
Trond Myklebust63f5f792015-01-23 19:19:25 -05001172 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1173 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001174 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001175 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001176 nfs_sb_active(dentry->d_sb);
1177 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 p->dir = parent;
1179 p->owner = sp;
1180 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001181 p->o_arg.open_flags = flags;
1182 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001183 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1184 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001185 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1186 * will return permission denied for all bits until close */
1187 if (!(flags & O_EXCL)) {
1188 /* ask server to check for all possible rights as results
1189 * are cached */
1190 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1191 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001192 }
David Howells7539bba2006-08-22 20:06:09 -04001193 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001194 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1195 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001196 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001198 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001199 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001200 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001201 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1202 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001203 case NFS4_OPEN_CLAIM_NULL:
1204 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1205 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1206 p->o_arg.fh = NFS_FH(dir);
1207 break;
1208 case NFS4_OPEN_CLAIM_PREVIOUS:
1209 case NFS4_OPEN_CLAIM_FH:
1210 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1211 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001212 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001213 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001214 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001215 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001216
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 p->o_arg.u.attrs = &p->attrs;
1218 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001219
1220 verf[0] = jiffies;
1221 verf[1] = current->pid;
1222 memcpy(p->o_arg.u.verifier.data, verf,
1223 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001225 p->c_arg.fh = &p->o_res.fh;
1226 p->c_arg.stateid = &p->o_res.stateid;
1227 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001230 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001231
1232err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001233 nfs4_label_free(p->a_label);
1234err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001235 nfs4_label_free(p->f_label);
1236err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 kfree(p);
1238err:
1239 dput(parent);
1240 return NULL;
1241}
1242
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001243static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001244{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001245 struct nfs4_opendata *p = container_of(kref,
1246 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001247 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248
1249 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001250 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001251 if (p->state != NULL)
1252 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001253 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001254
Kinglong Meea49c2692015-07-27 15:31:38 +08001255 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001256 nfs4_label_free(p->f_label);
1257
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001258 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001259 dput(p->dentry);
1260 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001261 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001262 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 kfree(p);
1264}
1265
1266static void nfs4_opendata_put(struct nfs4_opendata *p)
1267{
1268 if (p != NULL)
1269 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270}
1271
Trond Myklebust06f814a2006-01-03 09:55:07 +01001272static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1273{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001274 int ret;
1275
Trond Myklebust06f814a2006-01-03 09:55:07 +01001276 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001277 return ret;
1278}
1279
Trond Myklebust24311f82015-09-20 10:50:17 -04001280static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1281 fmode_t fmode)
1282{
1283 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1284 case FMODE_READ|FMODE_WRITE:
1285 return state->n_rdwr != 0;
1286 case FMODE_WRITE:
1287 return state->n_wronly != 0;
1288 case FMODE_READ:
1289 return state->n_rdonly != 0;
1290 }
1291 WARN_ON_ONCE(1);
1292 return false;
1293}
1294
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001295static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001296{
1297 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298
Trond Myklebust536e43d2012-01-17 22:04:26 -05001299 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001300 goto out;
1301 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001302 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001303 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1304 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 break;
1306 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001307 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1308 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001309 break;
1310 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001311 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1312 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001315 return ret;
1316}
1317
Trond Myklebust2a606182015-08-19 22:30:00 -05001318static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1319 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001320{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001321 if (delegation == NULL)
1322 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001324 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001325 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1326 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001327 switch (claim) {
1328 case NFS4_OPEN_CLAIM_NULL:
1329 case NFS4_OPEN_CLAIM_FH:
1330 break;
1331 case NFS4_OPEN_CLAIM_PREVIOUS:
1332 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1333 break;
1334 default:
1335 return 0;
1336 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001337 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 return 1;
1339}
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001342{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001343 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001344 case FMODE_WRITE:
1345 state->n_wronly++;
1346 break;
1347 case FMODE_READ:
1348 state->n_rdonly++;
1349 break;
1350 case FMODE_READ|FMODE_WRITE:
1351 state->n_rdwr++;
1352 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001354}
1355
Trond Myklebust4f14c192014-02-12 19:15:06 -05001356static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001357{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001358 struct nfs_client *clp = state->owner->so_server->nfs_client;
1359 bool need_recover = false;
1360
1361 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1362 need_recover = true;
1363 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1364 need_recover = true;
1365 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1366 need_recover = true;
1367 if (need_recover)
1368 nfs4_state_mark_reclaim_nograce(clp, state);
1369}
1370
Trond Myklebuste999e802014-02-10 18:20:47 -05001371static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1372 nfs4_stateid *stateid)
1373{
1374 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1375 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001376 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1377 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001378 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001379 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001380 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1381 return true;
1382 return false;
1383}
1384
Trond Myklebustf95549c2015-01-23 18:06:09 -05001385static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1386{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001387 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1388 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001389 if (state->n_wronly)
1390 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1391 if (state->n_rdonly)
1392 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 if (state->n_rdwr)
1394 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001395 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001396}
1397
Trond Myklebust226056c2014-02-11 10:41:07 -05001398static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001399 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001400 nfs4_stateid *stateid, fmode_t fmode)
1401{
1402 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1403 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1404 case FMODE_WRITE:
1405 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1406 break;
1407 case FMODE_READ:
1408 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1409 break;
1410 case 0:
1411 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1412 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1413 clear_bit(NFS_OPEN_STATE, &state->flags);
1414 }
1415 if (stateid == NULL)
1416 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001417 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001418 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1419 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1420 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001421 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001422 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001423 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001424 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001425 nfs4_stateid_copy(&state->stateid, stateid);
1426 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001427}
1428
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001429static void nfs_clear_open_stateid(struct nfs4_state *state,
1430 nfs4_stateid *arg_stateid,
1431 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001432{
1433 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001434 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001435 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001436 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1437 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001438}
1439
Trond Myklebust003707c2007-07-05 18:07:55 -04001440static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1441{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001442 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001443 case FMODE_READ:
1444 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1445 break;
1446 case FMODE_WRITE:
1447 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1448 break;
1449 case FMODE_READ|FMODE_WRITE:
1450 set_bit(NFS_O_RDWR_STATE, &state->flags);
1451 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001452 if (!nfs_need_update_open_stateid(state, stateid))
1453 return;
1454 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1455 nfs4_stateid_copy(&state->stateid, stateid);
1456 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457}
1458
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 /*
1462 * Protect the call to nfs4_state_set_mode_locked and
1463 * serialise the stateid update
1464 */
Andrew Elble361cad32015-12-02 09:20:57 -05001465 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001466 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001467 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001468 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001469 set_bit(NFS_DELEGATED_STATE, &state->flags);
1470 }
1471 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001472 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001473 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001474 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001475 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001479{
1480 struct nfs_inode *nfsi = NFS_I(state->inode);
1481 struct nfs_delegation *deleg_cur;
1482 int ret = 0;
1483
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001485
1486 rcu_read_lock();
1487 deleg_cur = rcu_dereference(nfsi->delegation);
1488 if (deleg_cur == NULL)
1489 goto no_delegation;
1490
1491 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001492 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001493 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001495 goto no_delegation_unlock;
1496
1497 if (delegation == NULL)
1498 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001499 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001500 goto no_delegation_unlock;
1501
Trond Myklebustb7391f42008-12-23 15:21:52 -05001502 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001504 ret = 1;
1505no_delegation_unlock:
1506 spin_unlock(&deleg_cur->lock);
1507no_delegation:
1508 rcu_read_unlock();
1509
1510 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001511 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001512 ret = 1;
1513 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001514 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1515 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001516
1517 return ret;
1518}
1519
Trond Myklebust39071e62015-01-24 15:07:56 -05001520static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1521 const nfs4_stateid *stateid)
1522{
1523 struct nfs4_state *state = lsp->ls_state;
1524 bool ret = false;
1525
1526 spin_lock(&state->state_lock);
1527 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1530 goto out_noupdate;
1531 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1532 ret = true;
1533out_noupdate:
1534 spin_unlock(&state->state_lock);
1535 return ret;
1536}
Trond Myklebust34310432008-12-23 15:21:38 -05001537
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001539{
1540 struct nfs_delegation *delegation;
1541
1542 rcu_read_lock();
1543 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001544 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001545 rcu_read_unlock();
1546 return;
1547 }
1548 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001549 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001550}
1551
Trond Myklebust6ee41262007-07-08 14:11:36 -04001552static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553{
1554 struct nfs4_state *state = opendata->state;
1555 struct nfs_inode *nfsi = NFS_I(state->inode);
1556 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001557 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001559 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001560 nfs4_stateid stateid;
1561 int ret = -EAGAIN;
1562
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001564 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001565 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001566 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001568 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001569 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001570 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001571 rcu_read_lock();
1572 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001573 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001574 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001575 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001576 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001578 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001579 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001580 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001581 if (!opendata->is_recover) {
1582 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1583 if (ret != 0)
1584 goto out;
1585 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001586 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001587
1588 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001589 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001590 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001591 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001592out:
1593 return ERR_PTR(ret);
1594out_return_state:
1595 atomic_inc(&state->count);
1596 return state;
1597}
1598
Andy Adamsone23008e2012-10-02 21:07:32 -04001599static void
1600nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1601{
1602 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1603 struct nfs_delegation *delegation;
1604 int delegation_flags = 0;
1605
1606 rcu_read_lock();
1607 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1608 if (delegation)
1609 delegation_flags = delegation->flags;
1610 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001611 switch (data->o_arg.claim) {
1612 default:
1613 break;
1614 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1615 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001616 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1617 "returning a delegation for "
1618 "OPEN(CLAIM_DELEGATE_CUR)\n",
1619 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001620 return;
1621 }
1622 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001623 nfs_inode_set_delegation(state->inode,
1624 data->owner->so_cred,
1625 &data->o_res);
1626 else
1627 nfs_inode_reclaim_delegation(state->inode,
1628 data->owner->so_cred,
1629 &data->o_res);
1630}
1631
1632/*
1633 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1634 * and update the nfs4_state.
1635 */
1636static struct nfs4_state *
1637_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1638{
1639 struct inode *inode = data->state->inode;
1640 struct nfs4_state *state = data->state;
1641 int ret;
1642
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001643 if (!data->rpc_done) {
1644 if (data->rpc_status) {
1645 ret = data->rpc_status;
1646 goto err;
1647 }
1648 /* cached opens have already been processed */
1649 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001650 }
1651
Andy Adamsone23008e2012-10-02 21:07:32 -04001652 ret = nfs_refresh_inode(inode, &data->f_attr);
1653 if (ret)
1654 goto err;
1655
1656 if (data->o_res.delegation_type != 0)
1657 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001658update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001659 update_open_stateid(state, &data->o_res.stateid, NULL,
1660 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001661 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001662
1663 return state;
1664err:
1665 return ERR_PTR(ret);
1666
1667}
1668
1669static struct nfs4_state *
1670_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671{
1672 struct inode *inode;
1673 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001674 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001675
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001677 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001678 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 goto out;
1680 }
1681
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001682 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001684 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001685 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001686 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001687 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001688 goto err;
1689 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 state = nfs4_get_open_state(inode, data->owner);
1691 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001692 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001693 if (data->o_res.delegation_type != 0)
1694 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001695 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001698out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001699 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001701err_put_inode:
1702 iput(inode);
1703err:
1704 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705}
1706
Andy Adamsone23008e2012-10-02 21:07:32 -04001707static struct nfs4_state *
1708nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1709{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001710 struct nfs4_state *ret;
1711
Andy Adamsone23008e2012-10-02 21:07:32 -04001712 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001713 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1714 else
1715 ret = _nfs4_opendata_to_nfs4_state(data);
1716 nfs4_sequence_free_slot(&data->o_res.seq_res);
1717 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001718}
1719
Trond Myklebust864472e2006-01-03 09:55:15 +01001720static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1721{
1722 struct nfs_inode *nfsi = NFS_I(state->inode);
1723 struct nfs_open_context *ctx;
1724
1725 spin_lock(&state->inode->i_lock);
1726 list_for_each_entry(ctx, &nfsi->open_files, list) {
1727 if (ctx->state != state)
1728 continue;
1729 get_nfs_open_context(ctx);
1730 spin_unlock(&state->inode->i_lock);
1731 return ctx;
1732 }
1733 spin_unlock(&state->inode->i_lock);
1734 return ERR_PTR(-ENOENT);
1735}
1736
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001737static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1738 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001739{
1740 struct nfs4_opendata *opendata;
1741
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001742 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001743 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001744 if (opendata == NULL)
1745 return ERR_PTR(-ENOMEM);
1746 opendata->state = state;
1747 atomic_inc(&state->count);
1748 return opendata;
1749}
1750
Trond Myklebust24311f82015-09-20 10:50:17 -04001751static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1752 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001753{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001754 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001755 int ret;
1756
Trond Myklebust24311f82015-09-20 10:50:17 -04001757 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001758 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001759 opendata->o_arg.open_flags = 0;
1760 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001761 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1762 NFS_SB(opendata->dentry->d_sb),
1763 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001764 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1765 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1766 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001767 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001768 if (ret != 0)
1769 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001770 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001771 if (IS_ERR(newstate))
1772 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001773 if (newstate != opendata->state)
1774 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001775 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001776 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001777}
1778
1779static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1780{
Trond Myklebust864472e2006-01-03 09:55:15 +01001781 int ret;
1782
Trond Myklebust4f14c192014-02-12 19:15:06 -05001783 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1784 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1785 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1786 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001787 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001788 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001789 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001791 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1792 if (ret != 0)
1793 return ret;
1794 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1795 if (ret != 0)
1796 return ret;
1797 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1798 if (ret != 0)
1799 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 /*
1801 * We may have performed cached opens for all three recoveries.
1802 * Check if we need to update the current stateid.
1803 */
1804 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001805 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001806 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001808 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001809 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001810 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001811 return 0;
1812}
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/*
1815 * OPEN_RECLAIM:
1816 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001818static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001820 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001821 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001822 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int status;
1824
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001825 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1826 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001827 if (IS_ERR(opendata))
1828 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001829 rcu_read_lock();
1830 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001831 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001832 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001833 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001834 opendata->o_arg.u.delegation_type = delegation_type;
1835 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001836 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return status;
1838}
1839
Trond Myklebust539cd032007-06-05 11:46:42 -04001840static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct nfs_server *server = NFS_SERVER(state->inode);
1843 struct nfs4_exception exception = { };
1844 int err;
1845 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001846 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001847 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001848 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1849 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001850 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001851 break;
1852 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 } while (exception.retry);
1854 return err;
1855}
1856
Trond Myklebust864472e2006-01-03 09:55:15 +01001857static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1858{
1859 struct nfs_open_context *ctx;
1860 int ret;
1861
1862 ctx = nfs4_state_find_open_context(state);
1863 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001864 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001865 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001866 put_nfs_open_context(ctx);
1867 return ret;
1868}
1869
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001870static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 switch (err) {
1873 default:
1874 printk(KERN_ERR "NFS: %s: unhandled error "
1875 "%d.\n", __func__, err);
1876 case 0:
1877 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001878 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001879 case -ESTALE:
1880 break;
1881 case -NFS4ERR_BADSESSION:
1882 case -NFS4ERR_BADSLOT:
1883 case -NFS4ERR_BAD_HIGH_SLOT:
1884 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1885 case -NFS4ERR_DEADSESSION:
1886 set_bit(NFS_DELEGATED_STATE, &state->flags);
1887 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1888 return -EAGAIN;
1889 case -NFS4ERR_STALE_CLIENTID:
1890 case -NFS4ERR_STALE_STATEID:
1891 set_bit(NFS_DELEGATED_STATE, &state->flags);
1892 case -NFS4ERR_EXPIRED:
1893 /* Don't recall a delegation if it was lost */
1894 nfs4_schedule_lease_recovery(server->nfs_client);
1895 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001896 case -NFS4ERR_MOVED:
1897 nfs4_schedule_migration_recovery(server);
1898 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001899 case -NFS4ERR_LEASE_MOVED:
1900 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1901 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001902 case -NFS4ERR_DELEG_REVOKED:
1903 case -NFS4ERR_ADMIN_REVOKED:
1904 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001905 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001906 nfs_inode_find_state_and_recover(state->inode,
1907 stateid);
1908 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001909 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001910 case -NFS4ERR_DELAY:
1911 case -NFS4ERR_GRACE:
1912 set_bit(NFS_DELEGATED_STATE, &state->flags);
1913 ssleep(1);
1914 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001915 case -ENOMEM:
1916 case -NFS4ERR_DENIED:
1917 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1918 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return err;
1921}
1922
Trond Myklebust24311f82015-09-20 10:50:17 -04001923int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1924 struct nfs4_state *state, const nfs4_stateid *stateid,
1925 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001926{
1927 struct nfs_server *server = NFS_SERVER(state->inode);
1928 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001929 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001930
1931 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1932 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1933 if (IS_ERR(opendata))
1934 return PTR_ERR(opendata);
1935 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001936 write_seqlock(&state->seqlock);
1937 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1938 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001939 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1940 switch (type & (FMODE_READ|FMODE_WRITE)) {
1941 case FMODE_READ|FMODE_WRITE:
1942 case FMODE_WRITE:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1944 if (err)
1945 break;
1946 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1947 if (err)
1948 break;
1949 case FMODE_READ:
1950 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1951 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001952 nfs4_opendata_put(opendata);
1953 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1954}
1955
Chuck Leverbe05c862013-08-09 12:49:47 -04001956static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1957{
1958 struct nfs4_opendata *data = calldata;
1959
Peng Taocb04ad22014-06-11 05:24:15 +08001960 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1961 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962}
1963
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001964static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1965{
1966 struct nfs4_opendata *data = calldata;
1967
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001968 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001969
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001971 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001972 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001973 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001974 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001975 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001976 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001977}
1978
1979static void nfs4_open_confirm_release(void *calldata)
1980{
1981 struct nfs4_opendata *data = calldata;
1982 struct nfs4_state *state = NULL;
1983
1984 /* If this request hasn't been cancelled, do nothing */
1985 if (data->cancelled == 0)
1986 goto out_free;
1987 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001988 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001989 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001990 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001991 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001992 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001993out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001994 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001995}
1996
1997static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001998 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001999 .rpc_call_done = nfs4_open_confirm_done,
2000 .rpc_release = nfs4_open_confirm_release,
2001};
2002
2003/*
2004 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2005 */
2006static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2007{
David Howells2b0143b2015-03-17 22:25:59 +00002008 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002009 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002010 struct rpc_message msg = {
2011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2012 .rpc_argp = &data->c_arg,
2013 .rpc_resp = &data->c_res,
2014 .rpc_cred = data->owner->so_cred,
2015 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002016 struct rpc_task_setup task_setup_data = {
2017 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002018 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002019 .callback_ops = &nfs4_open_confirm_ops,
2020 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002021 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 .flags = RPC_TASK_ASYNC,
2023 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int status;
2025
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002026 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002027 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002028 data->rpc_done = 0;
2029 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002030 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002031 if (data->is_recover)
2032 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002033 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002034 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002036 status = nfs4_wait_for_completion_rpc_task(task);
2037 if (status != 0) {
2038 data->cancelled = 1;
2039 smp_wmb();
2040 } else
2041 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002042 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return status;
2044}
2045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002048 struct nfs4_opendata *data = calldata;
2049 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002050 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002051 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002052
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002053 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002054 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002055 /*
2056 * Check if we still need to send an OPEN call, or if we can use
2057 * a delegation instead.
2058 */
2059 if (data->state != NULL) {
2060 struct nfs_delegation *delegation;
2061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002062 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002063 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002064 rcu_read_lock();
2065 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002066 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002067 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002068 rcu_read_unlock();
2069 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002070 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002071 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002072 switch (claim) {
2073 default:
2074 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002075 case NFS4_OPEN_CLAIM_PREVIOUS:
2076 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2077 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002078 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002079 case NFS4_OPEN_CLAIM_FH:
2080 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002081 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2082 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002083 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002084 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002085 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002086 &data->o_res.seq_res,
2087 task) != 0)
2088 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002089
2090 /* Set the create mode (note dependency on the session type) */
2091 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2092 if (data->o_arg.open_flags & O_EXCL) {
2093 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2094 if (nfs4_has_persistent_session(clp))
2095 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2096 else if (clp->cl_mvops->minor_version > 0)
2097 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2098 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002099 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002100unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002101 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002102 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002103out_no_action:
2104 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002105out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002106 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002107}
2108
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002109static void nfs4_open_done(struct rpc_task *task, void *calldata)
2110{
2111 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002114
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002115 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002116 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002118 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002119 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2120 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 case S_IFREG:
2122 break;
2123 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 break;
2126 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128 break;
2129 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002131 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002133 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002134 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2135 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002136 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002137 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002139
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140static void nfs4_open_release(void *calldata)
2141{
2142 struct nfs4_opendata *data = calldata;
2143 struct nfs4_state *state = NULL;
2144
2145 /* If this request hasn't been cancelled, do nothing */
2146 if (data->cancelled == 0)
2147 goto out_free;
2148 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002149 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150 goto out_free;
2151 /* In case we need an open_confirm, no cleanup! */
2152 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2153 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002154 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002155 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002156 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002158 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002159}
2160
2161static const struct rpc_call_ops nfs4_open_ops = {
2162 .rpc_call_prepare = nfs4_open_prepare,
2163 .rpc_call_done = nfs4_open_done,
2164 .rpc_release = nfs4_open_release,
2165};
2166
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002167static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002168{
David Howells2b0143b2015-03-17 22:25:59 +00002169 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002170 struct nfs_server *server = NFS_SERVER(dir);
2171 struct nfs_openargs *o_arg = &data->o_arg;
2172 struct nfs_openres *o_res = &data->o_res;
2173 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002174 struct rpc_message msg = {
2175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2176 .rpc_argp = o_arg,
2177 .rpc_resp = o_res,
2178 .rpc_cred = data->owner->so_cred,
2179 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002180 struct rpc_task_setup task_setup_data = {
2181 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 .callback_ops = &nfs4_open_ops,
2184 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002185 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 .flags = RPC_TASK_ASYNC,
2187 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188 int status;
2189
Chuck Levera9c92d62013-08-09 12:48:18 -04002190 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002191 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002192 data->rpc_done = 0;
2193 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002194 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002195 data->is_recover = 0;
2196 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002197 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002198 data->is_recover = 1;
2199 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002200 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002201 if (IS_ERR(task))
2202 return PTR_ERR(task);
2203 status = nfs4_wait_for_completion_rpc_task(task);
2204 if (status != 0) {
2205 data->cancelled = 1;
2206 smp_wmb();
2207 } else
2208 status = data->rpc_status;
2209 rpc_put_task(task);
2210
2211 return status;
2212}
2213
2214static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2215{
David Howells2b0143b2015-03-17 22:25:59 +00002216 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002217 struct nfs_openres *o_res = &data->o_res;
2218 int status;
2219
2220 status = nfs4_run_open_task(data, 1);
2221 if (status != 0 || !data->rpc_done)
2222 return status;
2223
Trond Myklebust6926afd2012-01-07 13:22:46 -05002224 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2225
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002226 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2227 status = _nfs4_proc_open_confirm(data);
2228 if (status != 0)
2229 return status;
2230 }
2231
2232 return status;
2233}
2234
Trond Myklebustf3792d62014-07-10 08:54:32 -04002235/*
2236 * Additional permission checks in order to distinguish between an
2237 * open for read, and an open for execute. This works around the
2238 * fact that NFSv4 OPEN treats read and execute permissions as being
2239 * the same.
2240 * Note that in the non-execute case, we want to turn off permission
2241 * checking if we just created a new file (POSIX open() semantics).
2242 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002243static int nfs4_opendata_access(struct rpc_cred *cred,
2244 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002245 struct nfs4_state *state, fmode_t fmode,
2246 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002247{
2248 struct nfs_access_entry cache;
2249 u32 mask;
2250
2251 /* access call failed or for some reason the server doesn't
2252 * support any access modes -- defer access call until later */
2253 if (opendata->o_res.access_supported == 0)
2254 return 0;
2255
2256 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002257 /*
2258 * Use openflags to check for exec, because fmode won't
2259 * always have FMODE_EXEC set when file open for exec.
2260 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002261 if (openflags & __FMODE_EXEC) {
2262 /* ONLY check for exec rights */
2263 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002264 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002265 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002266
2267 cache.cred = cred;
2268 cache.jiffies = jiffies;
2269 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2270 nfs_access_add_cache(state->inode, &cache);
2271
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002272 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002273 return 0;
2274
2275 /* even though OPEN succeeded, access is denied. Close the file */
2276 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002277 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002278}
2279
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002280/*
2281 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2282 */
2283static int _nfs4_proc_open(struct nfs4_opendata *data)
2284{
David Howells2b0143b2015-03-17 22:25:59 +00002285 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002286 struct nfs_server *server = NFS_SERVER(dir);
2287 struct nfs_openargs *o_arg = &data->o_arg;
2288 struct nfs_openres *o_res = &data->o_res;
2289 int status;
2290
2291 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002292 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002294 if (status != 0) {
2295 if (status == -NFS4ERR_BADNAME &&
2296 !(o_arg->open_flags & O_CREAT))
2297 return -ENOENT;
2298 return status;
2299 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002300
Trond Myklebust6926afd2012-01-07 13:22:46 -05002301 nfs_fattr_map_and_free_names(server, &data->f_attr);
2302
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002303 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002304 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002305 if (o_arg->open_flags & O_EXCL)
2306 data->file_created = 1;
2307 else if (o_res->cinfo.before != o_res->cinfo.after)
2308 data->file_created = 1;
2309 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002310 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2311 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002318 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Andy Adamsond83217c2011-03-01 01:34:17 +00002322static int nfs4_recover_expired_lease(struct nfs_server *server)
2323{
2324 return nfs4_client_recover_expired_lease(server->nfs_client);
2325}
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/*
2328 * OPEN_EXPIRED:
2329 * reclaim state on the server after a network partition.
2330 * Assumes caller holds the appropriate lock
2331 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002332static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002334 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002335 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002337 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002338 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002339 if (IS_ERR(opendata))
2340 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002341 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002342 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002343 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002344 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002345 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002348static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002349{
Trond Myklebust539cd032007-06-05 11:46:42 -04002350 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002351 struct nfs4_exception exception = { };
2352 int err;
2353
2354 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002355 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002356 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002357 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2358 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002359 switch (err) {
2360 default:
2361 goto out;
2362 case -NFS4ERR_GRACE:
2363 case -NFS4ERR_DELAY:
2364 nfs4_handle_exception(server, err, &exception);
2365 err = 0;
2366 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002367 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002368out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002369 return err;
2370}
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002375 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Trond Myklebust864472e2006-01-03 09:55:15 +01002377 ctx = nfs4_state_find_open_context(state);
2378 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002379 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002380 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002381 put_nfs_open_context(ctx);
2382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002385static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2386{
2387 nfs_remove_bad_delegation(state->inode);
2388 write_seqlock(&state->seqlock);
2389 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2390 write_sequnlock(&state->seqlock);
2391 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2392}
2393
2394static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2395{
2396 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2397 nfs_finish_clear_delegation_stateid(state);
2398}
2399
2400static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2401{
2402 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2403 nfs40_clear_delegation_stateid(state);
2404 return nfs4_open_expired(sp, state);
2405}
2406
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002407#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002408static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002409{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002410 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002411 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002412 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002413 struct rpc_cred *cred;
2414 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002415
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002416 /* Get the delegation credential for use by test/free_stateid */
2417 rcu_read_lock();
2418 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002419 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002420 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002421 return;
2422 }
2423
2424 nfs4_stateid_copy(&stateid, &delegation->stateid);
2425 cred = get_rpccred(delegation->cred);
2426 rcu_read_unlock();
2427 status = nfs41_test_stateid(server, &stateid, cred);
2428 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002429
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002430 if (status != NFS_OK) {
2431 /* Free the stateid unless the server explicitly
2432 * informs us the stateid is unrecognized. */
2433 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434 nfs41_free_stateid(server, &stateid, cred);
2435 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002436 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002437
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002438 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002439}
2440
2441/**
2442 * nfs41_check_open_stateid - possibly free an open stateid
2443 *
2444 * @state: NFSv4 state for an inode
2445 *
2446 * Returns NFS_OK if recovery for this stateid is now finished.
2447 * Otherwise a negative NFS4ERR value is returned.
2448 */
2449static int nfs41_check_open_stateid(struct nfs4_state *state)
2450{
2451 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002452 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002453 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002454 int status;
2455
2456 /* If a state reset has been done, test_stateid is unneeded */
2457 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2458 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2459 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2460 return -NFS4ERR_BAD_STATEID;
2461
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002462 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002463 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002464 if (status != NFS_OK) {
2465 /* Free the stateid unless the server explicitly
2466 * informs us the stateid is unrecognized. */
2467 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002468 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002469
2470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2471 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2472 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002473 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002474 }
2475 return status;
2476}
2477
2478static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2479{
Chuck Levereb64cf92012-07-11 16:30:05 -04002480 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002481
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002482 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002483 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002484 if (status != NFS_OK)
2485 status = nfs4_open_expired(sp, state);
2486 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002487}
2488#endif
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002491 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2492 * fields corresponding to attributes that were used to store the verifier.
2493 * Make sure we clobber those fields in the later setattr call
2494 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002495static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2496 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002497{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002498 const u32 *attrset = opendata->o_res.attrset;
2499
2500 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002501 !(sattr->ia_valid & ATTR_ATIME_SET))
2502 sattr->ia_valid |= ATTR_ATIME;
2503
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002504 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002505 !(sattr->ia_valid & ATTR_MTIME_SET))
2506 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002507
2508 /* Except MODE, it seems harmless of setting twice. */
2509 if ((attrset[1] & FATTR4_WORD1_MODE))
2510 sattr->ia_valid &= ~ATTR_MODE;
2511
2512 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2513 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002514}
2515
Trond Myklebustc21443c2013-02-07 14:26:21 -05002516static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2517 fmode_t fmode,
2518 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002519 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002520{
2521 struct nfs4_state_owner *sp = opendata->owner;
2522 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002523 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002524 struct nfs4_state *state;
2525 unsigned int seq;
2526 int ret;
2527
2528 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2529
2530 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002531 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002532 goto out;
2533
2534 state = nfs4_opendata_to_nfs4_state(opendata);
2535 ret = PTR_ERR(state);
2536 if (IS_ERR(state))
2537 goto out;
2538 if (server->caps & NFS_CAP_POSIX_LOCK)
2539 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002540 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2541 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002542
Trond Myklebust275bb302013-05-29 13:11:28 -04002543 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002544 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002545 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002546 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002547 alias = d_exact_alias(dentry, state->inode);
2548 if (!alias)
2549 alias = d_splice_alias(igrab(state->inode), dentry);
2550 /* d_splice_alias() can't fail here - it's a non-directory */
2551 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002552 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002553 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002554 }
2555 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002556 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002557 }
2558
Trond Myklebustc21443c2013-02-07 14:26:21 -05002559 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2560 if (ret != 0)
2561 goto out;
2562
Trond Myklebust3efb9722013-05-29 13:17:04 -04002563 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002564 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002565 nfs_inode_attach_open_context(ctx);
2566 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2567 nfs4_schedule_stateid_recovery(server, state);
2568 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002569out:
2570 return ret;
2571}
2572
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002573/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002574 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 */
Andy Adamson82be4172012-05-23 05:02:35 -04002576static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002577 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002578 int flags,
2579 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002580 struct nfs4_label *label,
2581 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 struct nfs4_state_owner *sp;
2584 struct nfs4_state *state = NULL;
2585 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002586 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002587 struct dentry *dentry = ctx->dentry;
2588 struct rpc_cred *cred = ctx->cred;
2589 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2590 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002591 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002592 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002593 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002597 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2598 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2600 goto out_err;
2601 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002602 status = nfs4_recover_expired_lease(server);
2603 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002604 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002605 if (d_really_is_positive(dentry))
2606 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002607 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002608 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002609 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002610 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002611 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002612 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002613 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
David Quigley14c43f72013-05-22 12:50:43 -04002615 if (label) {
2616 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2617 if (IS_ERR(olabel)) {
2618 status = PTR_ERR(olabel);
2619 goto err_opendata_put;
2620 }
2621 }
2622
Trond Myklebuste911b812014-03-26 13:24:37 -07002623 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2624 if (!opendata->f_attr.mdsthreshold) {
2625 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2626 if (!opendata->f_attr.mdsthreshold)
2627 goto err_free_label;
2628 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002629 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002630 }
David Howells2b0143b2015-03-17 22:25:59 +00002631 if (d_really_is_positive(dentry))
2632 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002633
Trond Myklebust3efb9722013-05-29 13:17:04 -04002634 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002635 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002636 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002637 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002638
NeilBrownefcbc042015-07-30 13:00:56 +10002639 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002640 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002641 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002642 /*
2643 * send create attributes which was not set by open
2644 * with an extra setattr.
2645 */
2646 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2647 nfs_fattr_init(opendata->o_res.f_attr);
2648 status = nfs4_do_setattr(state->inode, cred,
2649 opendata->o_res.f_attr, sattr,
2650 state, label, olabel);
2651 if (status == 0) {
2652 nfs_setattr_update_inode(state->inode, sattr,
2653 opendata->o_res.f_attr);
2654 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2655 }
David Quigley1775fd32013-05-22 12:50:42 -04002656 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002657 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002658 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002659 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002660
Trond Myklebuste911b812014-03-26 13:24:37 -07002661 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002662 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002663 opendata->f_attr.mdsthreshold = NULL;
2664 }
Andy Adamson82be4172012-05-23 05:02:35 -04002665
David Quigley14c43f72013-05-22 12:50:43 -04002666 nfs4_label_free(olabel);
2667
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002668 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002671err_free_label:
2672 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002673err_opendata_put:
2674 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002675err_put_state_owner:
2676 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return status;
2679}
2680
2681
Andy Adamson82be4172012-05-23 05:02:35 -04002682static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002683 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002684 int flags,
2685 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002686 struct nfs4_label *label,
2687 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002689 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct nfs4_exception exception = { };
2691 struct nfs4_state *res;
2692 int status;
2693
2694 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002695 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002696 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002697 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (status == 0)
2699 break;
2700 /* NOTE: BAD_SEQID means the server and client disagree about the
2701 * book-keeping w.r.t. state-changing operations
2702 * (OPEN/CLOSE/LOCK/LOCKU...)
2703 * It is actually a sign of a bug on the client or on the server.
2704 *
2705 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002706 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 * have unhashed the old state_owner for us, and that we can
2708 * therefore safely retry using a new one. We should still warn
2709 * the user though...
2710 */
2711 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002712 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002713 " returned a bad sequence-id error!\n",
2714 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 exception.retry = 1;
2716 continue;
2717 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002718 /*
2719 * BAD_STATEID on OPEN means that the server cancelled our
2720 * state before it received the OPEN_CONFIRM.
2721 * Recover by retrying the request as per the discussion
2722 * on Page 181 of RFC3530.
2723 */
2724 if (status == -NFS4ERR_BAD_STATEID) {
2725 exception.retry = 1;
2726 continue;
2727 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002728 if (status == -EAGAIN) {
2729 /* We must have found a delegation */
2730 exception.retry = 1;
2731 continue;
2732 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002733 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2734 continue;
2735 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 status, &exception));
2737 } while (exception.retry);
2738 return res;
2739}
2740
Trond Myklebust8487c472016-06-26 08:44:35 -04002741static int _nfs4_do_setattr(struct inode *inode,
2742 struct nfs_setattrargs *arg,
2743 struct nfs_setattrres *res,
2744 struct rpc_cred *cred,
2745 struct nfs4_state *state)
2746{
2747 struct nfs_server *server = NFS_SERVER(inode);
2748 struct rpc_message msg = {
2749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2750 .rpc_argp = arg,
2751 .rpc_resp = res,
2752 .rpc_cred = cred,
2753 };
2754 struct rpc_cred *delegation_cred = NULL;
2755 unsigned long timestamp = jiffies;
2756 fmode_t fmode;
2757 bool truncate;
2758 int status;
2759
2760 nfs_fattr_init(res->fattr);
2761
2762 /* Servers should only apply open mode checks for file size changes */
2763 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2764 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2765
2766 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2767 /* Use that stateid */
2768 } else if (truncate && state != NULL) {
2769 struct nfs_lockowner lockowner = {
2770 .l_owner = current->files,
2771 .l_pid = current->tgid,
2772 };
2773 if (!nfs4_valid_open_stateid(state))
2774 return -EBADF;
2775 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2776 &arg->stateid, &delegation_cred) == -EIO)
2777 return -EBADF;
2778 } else
2779 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2780 if (delegation_cred)
2781 msg.rpc_cred = delegation_cred;
2782
2783 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2784
2785 put_rpccred(delegation_cred);
2786 if (status == 0 && state != NULL)
2787 renew_lease(server, timestamp);
2788 trace_nfs4_setattr(inode, &arg->stateid, status);
2789 return status;
2790}
2791
2792static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2793 struct nfs_fattr *fattr, struct iattr *sattr,
2794 struct nfs4_state *state, struct nfs4_label *ilabel,
2795 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002797 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002799 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 .iap = sattr,
2801 .server = server,
2802 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002803 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 };
2805 struct nfs_setattrres res = {
2806 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002807 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .server = server,
2809 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002810 struct nfs4_exception exception = {
2811 .state = state,
2812 .inode = inode,
2813 .stateid = &arg.stateid,
2814 };
2815 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
David Quigleyaa9c2662013-05-22 12:50:44 -04002817 arg.bitmask = nfs4_bitmask(server, ilabel);
2818 if (ilabel)
2819 arg.bitmask = nfs4_bitmask(server, olabel);
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002822 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002823 switch (err) {
2824 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002825 if (!(sattr->ia_valid & ATTR_SIZE)) {
2826 pr_warn_once("NFSv4: server %s is incorrectly "
2827 "applying open mode checks to "
2828 "a SETATTR that is not "
2829 "changing file size.\n",
2830 server->nfs_client->cl_hostname);
2831 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002832 if (state && !(state->state & FMODE_WRITE)) {
2833 err = -EBADF;
2834 if (sattr->ia_valid & ATTR_OPEN)
2835 err = -EACCES;
2836 goto out;
2837 }
2838 }
2839 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002841out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return err;
2843}
2844
Peng Tao500d7012015-09-22 11:35:22 +08002845static bool
2846nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2847{
2848 if (inode == NULL || !nfs_have_layout(inode))
2849 return false;
2850
2851 return pnfs_wait_on_layoutreturn(inode, task);
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854struct nfs4_closedata {
2855 struct inode *inode;
2856 struct nfs4_state *state;
2857 struct nfs_closeargs arg;
2858 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002859 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002860 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002861 bool roc;
2862 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863};
2864
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002865static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002866{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002867 struct nfs4_closedata *calldata = data;
2868 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002869 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002870
Fred Isamanf7e89172011-01-06 11:36:32 +00002871 if (calldata->roc)
2872 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002873 nfs4_put_open_state(calldata->state);
2874 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002875 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002876 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002877 kfree(calldata);
2878}
2879
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002880static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002882 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002885 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Chuck Levera3ca5652012-03-01 17:00:40 -05002887 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002888 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2889 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002890 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 /* hmm. we are done with the inode, and in the process of freeing
2892 * the state_owner. we keep this around to process errors
2893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 switch (task->tk_status) {
2895 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002896 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002897 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002898 pnfs_roc_set_barrier(state->inode,
2899 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002900 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002901 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002902 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002904 case -NFS4ERR_OLD_STATEID:
2905 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002907 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002908 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002909 rpc_restart_call_prepare(task);
2910 goto out_release;
2911 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002912 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002915 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002916 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002917 goto out_release;
2918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002920 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2921 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002922out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002923 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002924 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002925 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002928static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002930 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002931 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002932 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002933 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002934 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002935
Chuck Levera3ca5652012-03-01 17:00:40 -05002936 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002937 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002938 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002939
Trond Myklebust88069f72009-12-08 08:33:16 -05002940 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002941 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002942 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2943 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2944 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002945 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002946 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002947 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002948 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002949 if (state->n_rdonly == 0)
2950 call_close |= is_rdonly;
2951 else if (is_rdonly)
2952 calldata->arg.fmode |= FMODE_READ;
2953 if (state->n_wronly == 0)
2954 call_close |= is_wronly;
2955 else if (is_wronly)
2956 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04002957 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2958 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002959 } else if (is_rdwr)
2960 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2961
Trond Myklebust5d422302013-03-14 16:57:48 -04002962 if (!nfs4_valid_open_stateid(state))
2963 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002964 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002965
2966 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002967 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002968 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002969 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002970
Peng Tao500d7012015-09-22 11:35:22 +08002971 if (nfs4_wait_on_layoutreturn(inode, task)) {
2972 nfs_release_seqid(calldata->arg.seqid);
2973 goto out_wait;
2974 }
2975
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002976 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002977 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002978 if (calldata->roc)
2979 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2980
Trond Myklebust6ae37332015-01-30 14:21:14 -05002981 calldata->arg.share_access =
2982 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2983 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002984
Trond Myklebust516a6af2005-10-27 22:12:41 -04002985 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002986 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002987 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002988 &calldata->arg.seq_args,
2989 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002990 task) != 0)
2991 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002992 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002993 return;
2994out_no_action:
2995 task->tk_action = NULL;
2996out_wait:
2997 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003000static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003001 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003002 .rpc_call_done = nfs4_close_done,
3003 .rpc_release = nfs4_free_closedata,
3004};
3005
Peng Taofe08c542014-07-03 13:05:01 +08003006static bool nfs4_roc(struct inode *inode)
3007{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003008 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003009 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003010 return pnfs_roc(inode);
3011}
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013/*
3014 * It is possible for data to be read/written from a mem-mapped file
3015 * after the sys_close call (which hits the vfs layer as a flush).
3016 * This means that we can't safely call nfsv4 close on a file until
3017 * the inode is cleared. This in turn means that we are not good
3018 * NFSv4 citizens - we do not indicate to the server to update the file's
3019 * share state even when we are done with one of the three share
3020 * stateid's in the inode.
3021 *
3022 * NOTE: Caller must be holding the sp->so_owner semaphore!
3023 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003024int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003026 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003027 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003029 struct nfs4_state_owner *sp = state->owner;
3030 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003031 struct rpc_message msg = {
3032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3033 .rpc_cred = state->owner->so_cred,
3034 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003035 struct rpc_task_setup task_setup_data = {
3036 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003037 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003038 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003039 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003040 .flags = RPC_TASK_ASYNC,
3041 };
Trond Myklebust95121352005-10-18 14:20:12 -07003042 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003044 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3045 &task_setup_data.rpc_client, &msg);
3046
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003047 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003049 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003050 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003051 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003053 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003055 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3056 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003057 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003058 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003059 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003060 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003061 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003062 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003063 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003064 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003065 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003066
Trond Myklebust1174dd12010-12-21 10:52:24 -05003067 msg.rpc_argp = &calldata->arg;
3068 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003069 task_setup_data.callback_data = calldata;
3070 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003071 if (IS_ERR(task))
3072 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003073 status = 0;
3074 if (wait)
3075 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003076 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003077 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003078out_free_calldata:
3079 kfree(calldata);
3080out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003081 nfs4_put_open_state(state);
3082 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003083 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Trond Myklebust2b484292010-09-17 10:56:51 -04003086static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003087nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3088 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003091 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3092
3093 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Trond Myklebust565277f2007-10-15 18:17:53 -04003095 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003096 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003097
3098 nfs4_label_release_security(label);
3099
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003100 if (IS_ERR(state))
3101 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003102 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Trond Myklebust1185a552009-12-03 15:54:02 -05003105static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003106{
3107 if (ctx->state == NULL)
3108 return;
3109 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003110 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003111 else
Al Viro643168c2011-06-22 18:20:23 -04003112 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Trond Myklebustb944dba2013-11-04 15:20:20 -05003115#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3116#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003117#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3120{
Kinglong Mee8c612822015-08-26 21:12:58 +08003121 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003122 struct nfs4_server_caps_arg args = {
3123 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003124 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003125 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct nfs4_server_caps_res res = {};
3127 struct rpc_message msg = {
3128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003129 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 .rpc_resp = &res,
3131 };
3132 int status;
3133
Kinglong Mee8c612822015-08-26 21:12:58 +08003134 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3135 FATTR4_WORD0_FH_EXPIRE_TYPE |
3136 FATTR4_WORD0_LINK_SUPPORT |
3137 FATTR4_WORD0_SYMLINK_SUPPORT |
3138 FATTR4_WORD0_ACLSUPPORT;
3139 if (minorversion)
3140 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3141
Bryan Schumaker7c513052011-03-24 17:12:24 +00003142 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003144 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003145 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003146 case 0:
3147 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3148 res.attr_bitmask[2] = 0;
3149 break;
3150 case 1:
3151 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3152 break;
3153 case 2:
3154 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003157 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3158 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3159 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3160 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003161 NFS_CAP_CTIME|NFS_CAP_MTIME|
3162 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003163 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3164 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 server->caps |= NFS_CAP_ACLS;
3166 if (res.has_links != 0)
3167 server->caps |= NFS_CAP_HARDLINKS;
3168 if (res.has_symlinks != 0)
3169 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003170 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3171 server->caps |= NFS_CAP_FILEID;
3172 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3173 server->caps |= NFS_CAP_MODE;
3174 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3175 server->caps |= NFS_CAP_NLINK;
3176 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3177 server->caps |= NFS_CAP_OWNER;
3178 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3179 server->caps |= NFS_CAP_OWNER_GROUP;
3180 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3181 server->caps |= NFS_CAP_ATIME;
3182 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3183 server->caps |= NFS_CAP_CTIME;
3184 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3185 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003186#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3187 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3188 server->caps |= NFS_CAP_SECURITY_LABEL;
3189#endif
3190 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3191 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003192 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003193
Trond Myklebusta65318b2009-03-11 14:10:28 -04003194 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3195 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3196 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003197 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003198 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3199 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003201 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return status;
3205}
3206
Trond Myklebust55a97592006-06-09 09:34:19 -04003207int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct nfs4_exception exception = { };
3210 int err;
3211 do {
3212 err = nfs4_handle_exception(server,
3213 _nfs4_server_capabilities(server, fhandle),
3214 &exception);
3215 } while (exception.retry);
3216 return err;
3217}
3218
3219static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3220 struct nfs_fsinfo *info)
3221{
David Quigleyaa9c2662013-05-22 12:50:44 -04003222 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003224 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 };
3226 struct nfs4_lookup_res res = {
3227 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003228 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 .fh = fhandle,
3230 };
3231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3233 .rpc_argp = &args,
3234 .rpc_resp = &res,
3235 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003236
David Quigleyaa9c2662013-05-22 12:50:44 -04003237 bitmask[0] = nfs4_fattr_bitmap[0];
3238 bitmask[1] = nfs4_fattr_bitmap[1];
3239 /*
3240 * Process the label in the upcoming getfattr
3241 */
3242 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3243
Trond Myklebust0e574af2005-10-27 22:12:38 -04003244 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003245 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
3248static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3249 struct nfs_fsinfo *info)
3250{
3251 struct nfs4_exception exception = { };
3252 int err;
3253 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003254 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003255 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003256 switch (err) {
3257 case 0:
3258 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003259 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003260 default:
3261 err = nfs4_handle_exception(server, err, &exception);
3262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003264out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return err;
3266}
3267
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003268static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3269 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3270{
Trond Myklebustc2190662013-08-26 19:23:04 -04003271 struct rpc_auth_create_args auth_args = {
3272 .pseudoflavor = flavor,
3273 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003274 struct rpc_auth *auth;
3275 int ret;
3276
Trond Myklebustc2190662013-08-26 19:23:04 -04003277 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003278 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003279 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003280 goto out;
3281 }
3282 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003283out:
3284 return ret;
3285}
3286
Chuck Lever9a744ba2013-03-16 15:56:02 -04003287/*
3288 * Retry pseudoroot lookup with various security flavors. We do this when:
3289 *
3290 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3291 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3292 *
3293 * Returns zero on success, or a negative NFS4ERR value, or a
3294 * negative errno value.
3295 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003296static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003297 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003299 /* Per 3530bis 15.33.5 */
3300 static const rpc_authflavor_t flav_array[] = {
3301 RPC_AUTH_GSS_KRB5P,
3302 RPC_AUTH_GSS_KRB5I,
3303 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003304 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003305 RPC_AUTH_NULL,
3306 };
3307 int status = -EPERM;
3308 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003310 if (server->auth_info.flavor_len > 0) {
3311 /* try each flavor specified by user */
3312 for (i = 0; i < server->auth_info.flavor_len; i++) {
3313 status = nfs4_lookup_root_sec(server, fhandle, info,
3314 server->auth_info.flavors[i]);
3315 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3316 continue;
3317 break;
3318 }
3319 } else {
3320 /* no flavors specified by user, try default list */
3321 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3322 status = nfs4_lookup_root_sec(server, fhandle, info,
3323 flav_array[i]);
3324 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3325 continue;
3326 break;
3327 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003328 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003329
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003330 /*
3331 * -EACCESS could mean that the user doesn't have correct permissions
3332 * to access the mount. It could also mean that we tried to mount
3333 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3334 * existing mount programs don't handle -EACCES very well so it should
3335 * be mapped to -EPERM instead.
3336 */
3337 if (status == -EACCES)
3338 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003339 return status;
3340}
3341
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003342/**
3343 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3344 * @server: initialized nfs_server handle
3345 * @fhandle: we fill in the pseudo-fs root file handle
3346 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003347 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003348 *
3349 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003350 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003351int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003352 struct nfs_fsinfo *info,
3353 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003354{
Andre Przywarac7757072015-04-23 17:17:40 +01003355 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003356
Andre Przywarac7757072015-04-23 17:17:40 +01003357 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003358 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003359
3360 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003361 status = server->nfs_client->cl_mvops->find_root_sec(server,
3362 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 if (status == 0)
3365 status = nfs4_server_capabilities(server, fhandle);
3366 if (status == 0)
3367 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003368
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003369 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Bryan Schumakerbae36242012-05-10 15:07:31 -04003372static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3373 struct nfs_fsinfo *info)
3374{
3375 int error;
3376 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003377 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003378
3379 error = nfs4_server_capabilities(server, mntfh);
3380 if (error < 0) {
3381 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3382 return error;
3383 }
3384
David Quigley14c43f72013-05-22 12:50:43 -04003385 label = nfs4_label_alloc(server, GFP_KERNEL);
3386 if (IS_ERR(label))
3387 return PTR_ERR(label);
3388
David Quigley1775fd32013-05-22 12:50:42 -04003389 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003390 if (error < 0) {
3391 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003392 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003393 }
3394
3395 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3396 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3397 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3398
David Quigley14c43f72013-05-22 12:50:43 -04003399err_free_label:
3400 nfs4_label_free(label);
3401
Bryan Schumakerbae36242012-05-10 15:07:31 -04003402 return error;
3403}
3404
Manoj Naik6b97fd32006-06-09 09:34:29 -04003405/*
3406 * Get locations and (maybe) other attributes of a referral.
3407 * Note that we'll actually follow the referral later when
3408 * we detect fsid mismatch in inode revalidation
3409 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003410static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3411 const struct qstr *name, struct nfs_fattr *fattr,
3412 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003413{
3414 int status = -ENOMEM;
3415 struct page *page = NULL;
3416 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003417
3418 page = alloc_page(GFP_KERNEL);
3419 if (page == NULL)
3420 goto out;
3421 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3422 if (locations == NULL)
3423 goto out;
3424
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003425 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003426 if (status != 0)
3427 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003428
3429 /*
3430 * If the fsid didn't change, this is a migration event, not a
3431 * referral. Cause us to drop into the exception handler, which
3432 * will kick off migration recovery.
3433 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003434 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003435 dprintk("%s: server did not return a different fsid for"
3436 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003437 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003438 goto out;
3439 }
Andy Adamson533eb462011-06-13 18:25:56 -04003440 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3441 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003442
Andy Adamson533eb462011-06-13 18:25:56 -04003443 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003444 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003445 memset(fhandle, 0, sizeof(struct nfs_fh));
3446out:
3447 if (page)
3448 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003449 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003450 return status;
3451}
3452
David Quigley1775fd32013-05-22 12:50:42 -04003453static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3454 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 struct nfs4_getattr_arg args = {
3457 .fh = fhandle,
3458 .bitmask = server->attr_bitmask,
3459 };
3460 struct nfs4_getattr_res res = {
3461 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003462 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 .server = server,
3464 };
3465 struct rpc_message msg = {
3466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3467 .rpc_argp = &args,
3468 .rpc_resp = &res,
3469 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003470
3471 args.bitmask = nfs4_bitmask(server, label);
3472
Trond Myklebust0e574af2005-10-27 22:12:38 -04003473 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003474 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
David Quigley1775fd32013-05-22 12:50:42 -04003477static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3478 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 struct nfs4_exception exception = { };
3481 int err;
3482 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003483 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3484 trace_nfs4_getattr(server, fhandle, fattr, err);
3485 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 &exception);
3487 } while (exception.retry);
3488 return err;
3489}
3490
3491/*
3492 * The file is not closed if it is opened due to the a request to change
3493 * the size of the file. The open call will not be needed once the
3494 * VFS layer lookup-intents are implemented.
3495 *
3496 * Close is called when the inode is destroyed.
3497 * If we haven't opened the file for O_WRONLY, we
3498 * need to in the size_change case to obtain a stateid.
3499 *
3500 * Got race?
3501 * Because OPEN is always done by name in nfsv4, it is
3502 * possible that we opened a different file by the same
3503 * name. We can recognize this race condition, but we
3504 * can't do anything about it besides returning an error.
3505 *
3506 * This will be fixed with VFS changes (lookup-intent).
3507 */
3508static int
3509nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3510 struct iattr *sattr)
3511{
David Howells2b0143b2015-03-17 22:25:59 +00003512 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003513 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003514 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003515 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 int status;
3517
Peng Tao88ac8152014-09-12 11:04:10 +08003518 if (pnfs_ld_layoutret_on_setattr(inode) &&
3519 sattr->ia_valid & ATTR_SIZE &&
3520 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003521 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003522
Trond Myklebust0e574af2005-10-27 22:12:38 -04003523 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Andy Adamson26699402012-05-30 16:12:24 -04003525 /* Deal with open(O_TRUNC) */
3526 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003527 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003528
3529 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003530 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003531 return 0;
3532
Trond Myklebustd5308382005-11-04 15:33:38 -05003533 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003534 if (sattr->ia_valid & ATTR_FILE) {
3535 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003536
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003537 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003538 if (ctx) {
3539 cred = ctx->cred;
3540 state = ctx->state;
3541 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003542 }
3543
David Quigley14c43f72013-05-22 12:50:43 -04003544 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3545 if (IS_ERR(label))
3546 return PTR_ERR(label);
3547
3548 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003550 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003551 nfs_setsecurity(inode, fattr, label);
3552 }
David Quigley14c43f72013-05-22 12:50:43 -04003553 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return status;
3555}
3556
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003557static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3558 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003559 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003560{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003561 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003562 int status;
3563 struct nfs4_lookup_arg args = {
3564 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003565 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003566 .name = name,
3567 };
3568 struct nfs4_lookup_res res = {
3569 .server = server,
3570 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003571 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003572 .fh = fhandle,
3573 };
3574 struct rpc_message msg = {
3575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3576 .rpc_argp = &args,
3577 .rpc_resp = &res,
3578 };
3579
David Quigleyaa9c2662013-05-22 12:50:44 -04003580 args.bitmask = nfs4_bitmask(server, label);
3581
David Howells2b3de442006-08-22 20:06:09 -04003582 nfs_fattr_init(fattr);
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003585 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 dprintk("NFS reply lookup: %d\n", status);
3587 return status;
3588}
3589
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003590static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003591{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003592 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003593 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003594 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3595 fattr->nlink = 2;
3596}
3597
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003598static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003599 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003600 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003601{
3602 struct nfs4_exception exception = { };
3603 struct rpc_clnt *client = *clnt;
3604 int err;
3605 do {
David Quigley1775fd32013-05-22 12:50:42 -04003606 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003607 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003608 switch (err) {
3609 case -NFS4ERR_BADNAME:
3610 err = -ENOENT;
3611 goto out;
3612 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003613 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003614 if (err == -NFS4ERR_MOVED)
3615 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003616 goto out;
3617 case -NFS4ERR_WRONGSEC:
3618 err = -EPERM;
3619 if (client != *clnt)
3620 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003621 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003622 if (IS_ERR(client))
3623 return PTR_ERR(client);
3624
3625 exception.retry = 1;
3626 break;
3627 default:
3628 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3629 }
3630 } while (exception.retry);
3631
3632out:
3633 if (err == 0)
3634 *clnt = client;
3635 else if (client != *clnt)
3636 rpc_shutdown_client(client);
3637
3638 return err;
3639}
3640
Al Virobeffb8f2016-07-20 16:34:42 -04003641static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003642 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3643 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003645 int status;
3646 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003647
David Quigley1775fd32013-05-22 12:50:42 -04003648 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003649 if (client != NFS_CLIENT(dir)) {
3650 rpc_shutdown_client(client);
3651 nfs_fixup_secinfo_attributes(fattr);
3652 }
3653 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003656struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003657nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003658 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3659{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003660 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003661 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003662
David Quigley1775fd32013-05-22 12:50:42 -04003663 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003664 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003665 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003666 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3670{
Trond Myklebust76b32992007-08-10 17:45:11 -04003671 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 struct nfs4_accessargs args = {
3673 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003674 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003676 struct nfs4_accessres res = {
3677 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003678 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 struct rpc_message msg = {
3680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3681 .rpc_argp = &args,
3682 .rpc_resp = &res,
3683 .rpc_cred = entry->cred,
3684 };
3685 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003686 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
3688 /*
3689 * Determine which access bits we want to ask for...
3690 */
3691 if (mode & MAY_READ)
3692 args.access |= NFS4_ACCESS_READ;
3693 if (S_ISDIR(inode->i_mode)) {
3694 if (mode & MAY_WRITE)
3695 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3696 if (mode & MAY_EXEC)
3697 args.access |= NFS4_ACCESS_LOOKUP;
3698 } else {
3699 if (mode & MAY_WRITE)
3700 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3701 if (mode & MAY_EXEC)
3702 args.access |= NFS4_ACCESS_EXECUTE;
3703 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003704
3705 res.fattr = nfs_alloc_fattr();
3706 if (res.fattr == NULL)
3707 return -ENOMEM;
3708
Bryan Schumaker7c513052011-03-24 17:12:24 +00003709 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003711 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003712 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003714 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return status;
3716}
3717
3718static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3719{
3720 struct nfs4_exception exception = { };
3721 int err;
3722 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003723 err = _nfs4_proc_access(inode, entry);
3724 trace_nfs4_access(inode, err);
3725 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 &exception);
3727 } while (exception.retry);
3728 return err;
3729}
3730
3731/*
3732 * TODO: For the time being, we don't try to get any attributes
3733 * along with any of the zero-copy operations READ, READDIR,
3734 * READLINK, WRITE.
3735 *
3736 * In the case of the first three, we want to put the GETATTR
3737 * after the read-type operation -- this is because it is hard
3738 * to predict the length of a GETATTR response in v4, and thus
3739 * align the READ data correctly. This means that the GETATTR
3740 * may end up partially falling into the page cache, and we should
3741 * shift it into the 'tail' of the xdr_buf before processing.
3742 * To do this efficiently, we need to know the total length
3743 * of data received, which doesn't seem to be available outside
3744 * of the RPC layer.
3745 *
3746 * In the case of WRITE, we also want to put the GETATTR after
3747 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003748 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 *
3750 * Both of these changes to the XDR layer would in fact be quite
3751 * minor, but I decided to leave them for a subsequent patch.
3752 */
3753static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3754 unsigned int pgbase, unsigned int pglen)
3755{
3756 struct nfs4_readlink args = {
3757 .fh = NFS_FH(inode),
3758 .pgbase = pgbase,
3759 .pglen = pglen,
3760 .pages = &page,
3761 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003762 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 struct rpc_message msg = {
3764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3765 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003766 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 };
3768
Bryan Schumaker7c513052011-03-24 17:12:24 +00003769 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 -07003770}
3771
3772static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3773 unsigned int pgbase, unsigned int pglen)
3774{
3775 struct nfs4_exception exception = { };
3776 int err;
3777 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003778 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3779 trace_nfs4_readlink(inode, err);
3780 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 &exception);
3782 } while (exception.retry);
3783 return err;
3784}
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003787 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789static int
3790nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003791 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
David Quigleyaa9c2662013-05-22 12:50:44 -04003793 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003794 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 int status = 0;
3797
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003798 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3799 if (IS_ERR(ctx))
3800 return PTR_ERR(ctx);
3801
David Quigleyaa9c2662013-05-22 12:50:44 -04003802 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3803
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003804 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003805 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (IS_ERR(state)) {
3807 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003811 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003812 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return status;
3814}
3815
Al Virobeffb8f2016-07-20 16:34:42 -04003816static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
Trond Myklebust16e42952005-10-27 22:12:44 -04003818 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003819 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003821 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003823 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003824 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3828 .rpc_argp = &args,
3829 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003831 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003832
Bryan Schumaker7c513052011-03-24 17:12:24 +00003833 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003834 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003835 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return status;
3837}
3838
Al Virobeffb8f2016-07-20 16:34:42 -04003839static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
3841 struct nfs4_exception exception = { };
3842 int err;
3843 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003844 err = _nfs4_proc_remove(dir, name);
3845 trace_nfs4_remove(dir, name, err);
3846 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 &exception);
3848 } while (exception.retry);
3849 return err;
3850}
3851
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003852static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003854 struct nfs_server *server = NFS_SERVER(dir);
3855 struct nfs_removeargs *args = msg->rpc_argp;
3856 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003858 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003860 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003861
3862 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003865static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3866{
Al Viro884be172016-04-28 23:56:31 -04003867 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003868 &data->args.seq_args,
3869 &data->res.seq_res,
3870 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871}
3872
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003873static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003875 struct nfs_unlinkdata *data = task->tk_calldata;
3876 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003877
Trond Myklebust14516c32010-07-31 14:29:06 -04003878 if (!nfs4_sequence_done(task, &res->seq_res))
3879 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003880 if (nfs4_async_handle_error(task, res->server, NULL,
3881 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003882 return 0;
3883 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003884 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
Jeff Laytond3d41522010-09-17 17:31:57 -04003887static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3888{
3889 struct nfs_server *server = NFS_SERVER(dir);
3890 struct nfs_renameargs *arg = msg->rpc_argp;
3891 struct nfs_renameres *res = msg->rpc_resp;
3892
3893 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003894 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003895 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003896}
3897
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003898static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3899{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003900 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3901 &data->args.seq_args,
3902 &data->res.seq_res,
3903 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003904}
3905
3906static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3907 struct inode *new_dir)
3908{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003909 struct nfs_renamedata *data = task->tk_calldata;
3910 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003911
3912 if (!nfs4_sequence_done(task, &res->seq_res))
3913 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003914 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003915 return 0;
3916
3917 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003918 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003919 return 1;
3920}
3921
Al Virobeffb8f2016-07-20 16:34:42 -04003922static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003924 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 struct nfs4_link_arg arg = {
3926 .fh = NFS_FH(inode),
3927 .dir_fh = NFS_FH(dir),
3928 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003929 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003931 struct nfs4_link_res res = {
3932 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003933 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003934 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 struct rpc_message msg = {
3936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3937 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003938 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003940 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Trond Myklebust136f2622010-04-16 16:22:49 -04003942 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003943 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003944 goto out;
3945
David Quigley14c43f72013-05-22 12:50:43 -04003946 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3947 if (IS_ERR(res.label)) {
3948 status = PTR_ERR(res.label);
3949 goto out;
3950 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003951 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003952
Bryan Schumaker7c513052011-03-24 17:12:24 +00003953 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003954 if (!status) {
3955 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003956 status = nfs_post_op_update_inode(inode, res.fattr);
3957 if (!status)
3958 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003959 }
David Quigley14c43f72013-05-22 12:50:43 -04003960
3961
3962 nfs4_label_free(res.label);
3963
Trond Myklebust136f2622010-04-16 16:22:49 -04003964out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003965 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return status;
3967}
3968
Al Virobeffb8f2016-07-20 16:34:42 -04003969static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
3971 struct nfs4_exception exception = { };
3972 int err;
3973 do {
3974 err = nfs4_handle_exception(NFS_SERVER(inode),
3975 _nfs4_proc_link(inode, dir, name),
3976 &exception);
3977 } while (exception.retry);
3978 return err;
3979}
3980
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003981struct nfs4_createdata {
3982 struct rpc_message msg;
3983 struct nfs4_create_arg arg;
3984 struct nfs4_create_res res;
3985 struct nfs_fh fh;
3986 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003987 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003988};
3989
3990static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003991 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003992{
3993 struct nfs4_createdata *data;
3994
3995 data = kzalloc(sizeof(*data), GFP_KERNEL);
3996 if (data != NULL) {
3997 struct nfs_server *server = NFS_SERVER(dir);
3998
David Quigley14c43f72013-05-22 12:50:43 -04003999 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4000 if (IS_ERR(data->label))
4001 goto out_free;
4002
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004003 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4004 data->msg.rpc_argp = &data->arg;
4005 data->msg.rpc_resp = &data->res;
4006 data->arg.dir_fh = NFS_FH(dir);
4007 data->arg.server = server;
4008 data->arg.name = name;
4009 data->arg.attrs = sattr;
4010 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004011 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004012 data->res.server = server;
4013 data->res.fh = &data->fh;
4014 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004015 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004016 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004017 }
4018 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004019out_free:
4020 kfree(data);
4021 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004022}
4023
4024static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4025{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004026 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004027 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004028 if (status == 0) {
4029 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004030 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004031 }
4032 return status;
4033}
4034
4035static void nfs4_free_createdata(struct nfs4_createdata *data)
4036{
David Quigley14c43f72013-05-22 12:50:43 -04004037 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004038 kfree(data);
4039}
4040
Chuck Lever4f390c12006-08-22 20:06:22 -04004041static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004042 struct page *page, unsigned int len, struct iattr *sattr,
4043 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004045 struct nfs4_createdata *data;
4046 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Chuck Lever94a6d752006-08-22 20:06:23 -04004048 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004049 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004050
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004051 status = -ENOMEM;
4052 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4053 if (data == NULL)
4054 goto out;
4055
4056 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4057 data->arg.u.symlink.pages = &page;
4058 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004059 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004061 status = nfs4_do_create(dir, dentry, data);
4062
4063 nfs4_free_createdata(data);
4064out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return status;
4066}
4067
Chuck Lever4f390c12006-08-22 20:06:22 -04004068static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004069 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004072 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004074
4075 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004078 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4079 trace_nfs4_symlink(dir, &dentry->d_name, err);
4080 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 &exception);
4082 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004083
4084 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return err;
4086}
4087
4088static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004089 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004091 struct nfs4_createdata *data;
4092 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004094 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4095 if (data == NULL)
4096 goto out;
4097
David Quigley1775fd32013-05-22 12:50:42 -04004098 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004099 status = nfs4_do_create(dir, dentry, data);
4100
4101 nfs4_free_createdata(data);
4102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return status;
4104}
4105
4106static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4107 struct iattr *sattr)
4108{
4109 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004110 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004112
David Quigleyaa9c2662013-05-22 12:50:44 -04004113 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4114
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004115 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004117 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4118 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4119 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 &exception);
4121 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004122 nfs4_label_release_security(label);
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 return err;
4125}
4126
4127static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004128 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
David Howells2b0143b2015-03-17 22:25:59 +00004130 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 struct nfs4_readdir_arg args = {
4132 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004133 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 .pgbase = 0,
4135 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004136 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004137 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 };
4139 struct nfs4_readdir_res res;
4140 struct rpc_message msg = {
4141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4142 .rpc_argp = &args,
4143 .rpc_resp = &res,
4144 .rpc_cred = cred,
4145 };
4146 int status;
4147
Al Viro6de14722013-09-16 10:53:17 -04004148 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4149 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004150 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004151 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004153 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 -05004154 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004155 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004156 status += args.pgbase;
4157 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004158
4159 nfs_invalidate_atime(dir);
4160
Harvey Harrison3110ff82008-05-02 13:42:44 -07004161 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return status;
4163}
4164
4165static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004166 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
4168 struct nfs4_exception exception = { };
4169 int err;
4170 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004171 err = _nfs4_proc_readdir(dentry, cred, cookie,
4172 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004173 trace_nfs4_readdir(d_inode(dentry), err);
4174 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 &exception);
4176 } while (exception.retry);
4177 return err;
4178}
4179
4180static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004181 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004183 struct nfs4_createdata *data;
4184 int mode = sattr->ia_mode;
4185 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4188 if (data == NULL)
4189 goto out;
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194 data->arg.ftype = NF4BLK;
4195 data->arg.u.device.specdata1 = MAJOR(rdev);
4196 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004199 data->arg.ftype = NF4CHR;
4200 data->arg.u.device.specdata1 = MAJOR(rdev);
4201 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004202 } else if (!S_ISSOCK(mode)) {
4203 status = -EINVAL;
4204 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
David Quigley1775fd32013-05-22 12:50:42 -04004206
David Quigleyaa9c2662013-05-22 12:50:44 -04004207 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004209out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004210 nfs4_free_createdata(data);
4211out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return status;
4213}
4214
4215static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4216 struct iattr *sattr, dev_t rdev)
4217{
4218 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004219 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004221
David Quigleyaa9c2662013-05-22 12:50:44 -04004222 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4223
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004224 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004226 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4227 trace_nfs4_mknod(dir, &dentry->d_name, err);
4228 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 &exception);
4230 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004231
4232 nfs4_label_release_security(label);
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 return err;
4235}
4236
4237static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4238 struct nfs_fsstat *fsstat)
4239{
4240 struct nfs4_statfs_arg args = {
4241 .fh = fhandle,
4242 .bitmask = server->attr_bitmask,
4243 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004244 struct nfs4_statfs_res res = {
4245 .fsstat = fsstat,
4246 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 struct rpc_message msg = {
4248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4249 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004250 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 };
4252
Trond Myklebust0e574af2005-10-27 22:12:38 -04004253 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004254 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
4257static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4258{
4259 struct nfs4_exception exception = { };
4260 int err;
4261 do {
4262 err = nfs4_handle_exception(server,
4263 _nfs4_proc_statfs(server, fhandle, fsstat),
4264 &exception);
4265 } while (exception.retry);
4266 return err;
4267}
4268
4269static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4270 struct nfs_fsinfo *fsinfo)
4271{
4272 struct nfs4_fsinfo_arg args = {
4273 .fh = fhandle,
4274 .bitmask = server->attr_bitmask,
4275 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004276 struct nfs4_fsinfo_res res = {
4277 .fsinfo = fsinfo,
4278 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 struct rpc_message msg = {
4280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4281 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004282 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 };
4284
Bryan Schumaker7c513052011-03-24 17:12:24 +00004285 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
4288static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4289{
4290 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004291 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 int err;
4293
4294 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004295 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004296 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004297 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004298 nfs4_set_lease_period(server->nfs_client,
4299 fsinfo->lease_time * HZ,
4300 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004301 break;
4302 }
4303 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 } while (exception.retry);
4305 return err;
4306}
4307
4308static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4309{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004310 int error;
4311
Trond Myklebust0e574af2005-10-27 22:12:38 -04004312 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004313 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004314 if (error == 0) {
4315 /* block layout checks this! */
4316 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004317 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004318 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004319
4320 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}
4322
4323static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4324 struct nfs_pathconf *pathconf)
4325{
4326 struct nfs4_pathconf_arg args = {
4327 .fh = fhandle,
4328 .bitmask = server->attr_bitmask,
4329 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004330 struct nfs4_pathconf_res res = {
4331 .pathconf = pathconf,
4332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 struct rpc_message msg = {
4334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4335 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004336 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 };
4338
4339 /* None of the pathconf attributes are mandatory to implement */
4340 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4341 memset(pathconf, 0, sizeof(*pathconf));
4342 return 0;
4343 }
4344
Trond Myklebust0e574af2005-10-27 22:12:38 -04004345 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004346 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
4349static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4350 struct nfs_pathconf *pathconf)
4351{
4352 struct nfs4_exception exception = { };
4353 int err;
4354
4355 do {
4356 err = nfs4_handle_exception(server,
4357 _nfs4_proc_pathconf(server, fhandle, pathconf),
4358 &exception);
4359 } while (exception.retry);
4360 return err;
4361}
4362
Trond Myklebust5521abf2013-03-16 20:54:34 -04004363int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004364 const struct nfs_open_context *ctx,
4365 const struct nfs_lock_context *l_ctx,
4366 fmode_t fmode)
4367{
4368 const struct nfs_lockowner *lockowner = NULL;
4369
4370 if (l_ctx != NULL)
4371 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004372 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004373}
4374EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4375
Trond Myklebust5521abf2013-03-16 20:54:34 -04004376static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4377 const struct nfs_open_context *ctx,
4378 const struct nfs_lock_context *l_ctx,
4379 fmode_t fmode)
4380{
4381 nfs4_stateid current_stateid;
4382
Trond Myklebuste1253be2014-03-05 08:44:23 -05004383 /* If the current stateid represents a lost lock, then exit */
4384 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4385 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004386 return nfs4_stateid_match(stateid, &current_stateid);
4387}
4388
4389static bool nfs4_error_stateid_expired(int err)
4390{
4391 switch (err) {
4392 case -NFS4ERR_DELEG_REVOKED:
4393 case -NFS4ERR_ADMIN_REVOKED:
4394 case -NFS4ERR_BAD_STATEID:
4395 case -NFS4ERR_STALE_STATEID:
4396 case -NFS4ERR_OLD_STATEID:
4397 case -NFS4ERR_OPENMODE:
4398 case -NFS4ERR_EXPIRED:
4399 return true;
4400 }
4401 return false;
4402}
4403
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004404void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004405{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004406 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004407}
4408
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004409static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004411 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004413 trace_nfs4_read(hdr, task->tk_status);
4414 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004415 hdr->args.context->state,
4416 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004417 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004418 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004420
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004421 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004423 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Trond Myklebust5521abf2013-03-16 20:54:34 -04004427static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004428 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004429{
4430
4431 if (!nfs4_error_stateid_expired(task->tk_status) ||
4432 nfs4_stateid_is_current(&args->stateid,
4433 args->context,
4434 args->lock_context,
4435 FMODE_READ))
4436 return false;
4437 rpc_restart_call_prepare(task);
4438 return true;
4439}
4440
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004441static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004442{
4443
4444 dprintk("--> %s\n", __func__);
4445
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004446 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004447 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004448 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004449 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004450 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4451 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004452}
4453
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004454static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4455 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004457 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004458 if (!hdr->pgio_done_cb)
4459 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004460 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004461 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004464static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4465 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004466{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004467 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4468 &hdr->args.seq_args,
4469 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004470 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004471 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004472 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4473 hdr->args.lock_context,
4474 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004475 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004476 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004477 return -EIO;
4478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004481static int nfs4_write_done_cb(struct rpc_task *task,
4482 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004484 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004485
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004486 trace_nfs4_write(hdr, task->tk_status);
4487 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004488 hdr->args.context->state,
4489 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004490 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004491 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004493 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004494 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004495 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004496 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
Trond Myklebust5521abf2013-03-16 20:54:34 -04004500static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004501 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004502{
4503
4504 if (!nfs4_error_stateid_expired(task->tk_status) ||
4505 nfs4_stateid_is_current(&args->stateid,
4506 args->context,
4507 args->lock_context,
4508 FMODE_WRITE))
4509 return false;
4510 rpc_restart_call_prepare(task);
4511 return true;
4512}
4513
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004514static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004515{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004516 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004517 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004518 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004519 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004520 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4521 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004522}
4523
Trond Myklebust5a37f852012-04-28 14:55:16 -04004524static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004525bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004526{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004527 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004528 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004529 return false;
4530 /* Otherwise, request attributes if and only if we don't hold
4531 * a delegation
4532 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004533 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004534}
Fred Isamana69aef12011-03-03 15:13:47 +00004535
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004536static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4537 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004539 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004540
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004541 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4542 hdr->args.bitmask = NULL;
4543 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004544 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004545 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004546
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004547 if (!hdr->pgio_done_cb)
4548 hdr->pgio_done_cb = nfs4_write_done_cb;
4549 hdr->res.server = server;
4550 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004552 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004553 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554}
4555
Fred Isaman0b7c0152012-04-20 14:47:39 -04004556static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4557{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004558 nfs4_setup_sequence(NFS_SERVER(data->inode),
4559 &data->args.seq_args,
4560 &data->res.seq_res,
4561 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Fred Isaman0b7c0152012-04-20 14:47:39 -04004564static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004567
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004568 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004569 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4570 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004571 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004572 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
Fred Isaman0b7c0152012-04-20 14:47:39 -04004577static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004578{
4579 if (!nfs4_sequence_done(task, &data->res.seq_res))
4580 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004581 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004582}
4583
Fred Isaman0b7c0152012-04-20 14:47:39 -04004584static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004586 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004587
Fred Isaman0b7c0152012-04-20 14:47:39 -04004588 if (data->commit_done_cb == NULL)
4589 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004590 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004591 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004592 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593}
4594
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004595struct nfs4_renewdata {
4596 struct nfs_client *client;
4597 unsigned long timestamp;
4598};
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600/*
4601 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4602 * standalone procedure for queueing an asynchronous RENEW.
4603 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004604static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004605{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004606 struct nfs4_renewdata *data = calldata;
4607 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004608
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004609 if (atomic_read(&clp->cl_count) > 1)
4610 nfs4_schedule_state_renewal(clp);
4611 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004612 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004613}
4614
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004615static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004617 struct nfs4_renewdata *data = calldata;
4618 struct nfs_client *clp = data->client;
4619 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004621 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004622 switch (task->tk_status) {
4623 case 0:
4624 break;
4625 case -NFS4ERR_LEASE_MOVED:
4626 nfs4_schedule_lease_moved_recovery(clp);
4627 break;
4628 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004629 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004630 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4631 return;
4632 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004633 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004634 return;
4635 }
4636 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004638 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004641static const struct rpc_call_ops nfs4_renew_ops = {
4642 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004643 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004644};
4645
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004646static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
4648 struct rpc_message msg = {
4649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4650 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004651 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004653 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004655 if (renew_flags == 0)
4656 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004657 if (!atomic_inc_not_zero(&clp->cl_count))
4658 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004659 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004660 if (data == NULL)
4661 return -ENOMEM;
4662 data->client = clp;
4663 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004664 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004665 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
4667
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004668static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
4670 struct rpc_message msg = {
4671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4672 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004673 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 };
4675 unsigned long now = jiffies;
4676 int status;
4677
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004678 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 if (status < 0)
4680 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004681 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 return 0;
4683}
4684
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004685static inline int nfs4_server_supports_acls(struct nfs_server *server)
4686{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004687 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004688}
4689
Trond Myklebust21f498c2012-08-24 10:59:25 -04004690/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4691 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004692 * the stack.
4693 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004694#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004695
Neil Hormane9e3d722011-03-04 19:26:03 -05004696static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004697 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004698{
4699 struct page *newpage, **spages;
4700 int rc = 0;
4701 size_t len;
4702 spages = pages;
4703
4704 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004705 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004706 newpage = alloc_page(GFP_KERNEL);
4707
4708 if (newpage == NULL)
4709 goto unwind;
4710 memcpy(page_address(newpage), buf, len);
4711 buf += len;
4712 buflen -= len;
4713 *pages++ = newpage;
4714 rc++;
4715 } while (buflen != 0);
4716
4717 return rc;
4718
4719unwind:
4720 for(; rc > 0; rc--)
4721 __free_page(spages[rc-1]);
4722 return -ENOMEM;
4723}
4724
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004725struct nfs4_cached_acl {
4726 int cached;
4727 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004728 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004729};
4730
4731static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004732{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004733 struct nfs_inode *nfsi = NFS_I(inode);
4734
4735 spin_lock(&inode->i_lock);
4736 kfree(nfsi->nfs4_acl);
4737 nfsi->nfs4_acl = acl;
4738 spin_unlock(&inode->i_lock);
4739}
4740
4741static void nfs4_zap_acl_attr(struct inode *inode)
4742{
4743 nfs4_set_cached_acl(inode, NULL);
4744}
4745
4746static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4747{
4748 struct nfs_inode *nfsi = NFS_I(inode);
4749 struct nfs4_cached_acl *acl;
4750 int ret = -ENOENT;
4751
4752 spin_lock(&inode->i_lock);
4753 acl = nfsi->nfs4_acl;
4754 if (acl == NULL)
4755 goto out;
4756 if (buf == NULL) /* user is just asking for length */
4757 goto out_len;
4758 if (acl->cached == 0)
4759 goto out;
4760 ret = -ERANGE; /* see getxattr(2) man page */
4761 if (acl->len > buflen)
4762 goto out;
4763 memcpy(buf, acl->data, acl->len);
4764out_len:
4765 ret = acl->len;
4766out:
4767 spin_unlock(&inode->i_lock);
4768 return ret;
4769}
4770
Sachin Prabhu5794d212012-04-17 14:36:40 +01004771static 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 +00004772{
4773 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004774 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004775
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004776 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004777 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004778 if (acl == NULL)
4779 goto out;
4780 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004781 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004782 } else {
4783 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4784 if (acl == NULL)
4785 goto out;
4786 acl->cached = 0;
4787 }
4788 acl->len = acl_len;
4789out:
4790 nfs4_set_cached_acl(inode, acl);
4791}
4792
Andy Adamsonbf118a32011-12-07 11:55:27 -05004793/*
4794 * The getxattr API returns the required buffer length when called with a
4795 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4796 * the required buf. On a NULL buf, we send a page of data to the server
4797 * guessing that the ACL request can be serviced by a page. If so, we cache
4798 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4799 * the cache. If not so, we throw away the page, and cache the required
4800 * length. The next getxattr call will then produce another round trip to
4801 * the server, this time with the input buf of the required size.
4802 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004803static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004804{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004805 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004806 struct nfs_getaclargs args = {
4807 .fh = NFS_FH(inode),
4808 .acl_pages = pages,
4809 .acl_len = buflen,
4810 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004811 struct nfs_getaclres res = {
4812 .acl_len = buflen,
4813 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004814 struct rpc_message msg = {
4815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4816 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004817 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004818 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004819 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4820 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004821
Andy Adamsonbf118a32011-12-07 11:55:27 -05004822 /* As long as we're doing a round trip to the server anyway,
4823 * let's be prepared for a page of acl data. */
4824 if (npages == 0)
4825 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004826 if (npages > ARRAY_SIZE(pages))
4827 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004828
Andy Adamsonbf118a32011-12-07 11:55:27 -05004829 for (i = 0; i < npages; i++) {
4830 pages[i] = alloc_page(GFP_KERNEL);
4831 if (!pages[i])
4832 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004833 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004834
4835 /* for decoding across pages */
4836 res.acl_scratch = alloc_page(GFP_KERNEL);
4837 if (!res.acl_scratch)
4838 goto out_free;
4839
Andy Adamsonbf118a32011-12-07 11:55:27 -05004840 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004841
Peng Taode040be2012-01-10 22:42:47 +08004842 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004843 __func__, buf, buflen, npages, args.acl_len);
4844 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4845 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004846 if (ret)
4847 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004848
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004849 /* Handle the case where the passed-in buffer is too short */
4850 if (res.acl_flags & NFS4_ACL_TRUNC) {
4851 /* Did the user only issue a request for the acl length? */
4852 if (buf == NULL)
4853 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004854 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004855 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004856 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004857 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004858 if (buf) {
4859 if (res.acl_len > buflen) {
4860 ret = -ERANGE;
4861 goto out_free;
4862 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004863 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004864 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004865out_ok:
4866 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004867out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004868 for (i = 0; i < npages; i++)
4869 if (pages[i])
4870 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004871 if (res.acl_scratch)
4872 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004873 return ret;
4874}
4875
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004876static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4877{
4878 struct nfs4_exception exception = { };
4879 ssize_t ret;
4880 do {
4881 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004882 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004883 if (ret >= 0)
4884 break;
4885 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4886 } while (exception.retry);
4887 return ret;
4888}
4889
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004890static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4891{
4892 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004893 int ret;
4894
4895 if (!nfs4_server_supports_acls(server))
4896 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004897 ret = nfs_revalidate_inode(server, inode);
4898 if (ret < 0)
4899 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004900 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4901 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004902 ret = nfs4_read_cached_acl(inode, buf, buflen);
4903 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004904 /* -ENOENT is returned if there is no ACL or if there is an ACL
4905 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004906 return ret;
4907 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004908}
4909
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004910static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004911{
4912 struct nfs_server *server = NFS_SERVER(inode);
4913 struct page *pages[NFS4ACL_MAXPAGES];
4914 struct nfs_setaclargs arg = {
4915 .fh = NFS_FH(inode),
4916 .acl_pages = pages,
4917 .acl_len = buflen,
4918 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004919 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004920 struct rpc_message msg = {
4921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4922 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004923 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004924 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004925 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004926 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004927
4928 if (!nfs4_server_supports_acls(server))
4929 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004930 if (npages > ARRAY_SIZE(pages))
4931 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004932 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004933 if (i < 0)
4934 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004935 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004936 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004937
4938 /*
4939 * Free each page after tx, so the only ref left is
4940 * held by the network stack
4941 */
4942 for (; i > 0; i--)
4943 put_page(pages[i-1]);
4944
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004945 /*
4946 * Acl update can result in inode attribute update.
4947 * so mark the attribute cache invalid.
4948 */
4949 spin_lock(&inode->i_lock);
4950 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4951 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004952 nfs_access_zap_cache(inode);
4953 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004954 return ret;
4955}
4956
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004957static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4958{
4959 struct nfs4_exception exception = { };
4960 int err;
4961 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004962 err = __nfs4_proc_set_acl(inode, buf, buflen);
4963 trace_nfs4_set_acl(inode, err);
4964 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004965 &exception);
4966 } while (exception.retry);
4967 return err;
4968}
4969
David Quigleyaa9c2662013-05-22 12:50:44 -04004970#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4971static int _nfs4_get_security_label(struct inode *inode, void *buf,
4972 size_t buflen)
4973{
4974 struct nfs_server *server = NFS_SERVER(inode);
4975 struct nfs_fattr fattr;
4976 struct nfs4_label label = {0, 0, buflen, buf};
4977
4978 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004979 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004980 .fh = NFS_FH(inode),
4981 .bitmask = bitmask,
4982 };
4983 struct nfs4_getattr_res res = {
4984 .fattr = &fattr,
4985 .label = &label,
4986 .server = server,
4987 };
4988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004990 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004991 .rpc_resp = &res,
4992 };
4993 int ret;
4994
4995 nfs_fattr_init(&fattr);
4996
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004997 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004998 if (ret)
4999 return ret;
5000 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5001 return -ENOENT;
5002 if (buflen < label.len)
5003 return -ERANGE;
5004 return 0;
5005}
5006
5007static int nfs4_get_security_label(struct inode *inode, void *buf,
5008 size_t buflen)
5009{
5010 struct nfs4_exception exception = { };
5011 int err;
5012
5013 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5014 return -EOPNOTSUPP;
5015
5016 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005017 err = _nfs4_get_security_label(inode, buf, buflen);
5018 trace_nfs4_get_security_label(inode, err);
5019 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005020 &exception);
5021 } while (exception.retry);
5022 return err;
5023}
5024
5025static int _nfs4_do_set_security_label(struct inode *inode,
5026 struct nfs4_label *ilabel,
5027 struct nfs_fattr *fattr,
5028 struct nfs4_label *olabel)
5029{
5030
5031 struct iattr sattr = {0};
5032 struct nfs_server *server = NFS_SERVER(inode);
5033 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005034 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005035 .fh = NFS_FH(inode),
5036 .iap = &sattr,
5037 .server = server,
5038 .bitmask = bitmask,
5039 .label = ilabel,
5040 };
5041 struct nfs_setattrres res = {
5042 .fattr = fattr,
5043 .label = olabel,
5044 .server = server,
5045 };
5046 struct rpc_message msg = {
5047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005048 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005049 .rpc_resp = &res,
5050 };
5051 int status;
5052
Jeff Layton12207f62013-11-01 10:49:32 -04005053 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005054
Jeff Layton12207f62013-11-01 10:49:32 -04005055 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005056 if (status)
5057 dprintk("%s failed: %d\n", __func__, status);
5058
5059 return status;
5060}
5061
5062static int nfs4_do_set_security_label(struct inode *inode,
5063 struct nfs4_label *ilabel,
5064 struct nfs_fattr *fattr,
5065 struct nfs4_label *olabel)
5066{
5067 struct nfs4_exception exception = { };
5068 int err;
5069
5070 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005071 err = _nfs4_do_set_security_label(inode, ilabel,
5072 fattr, olabel);
5073 trace_nfs4_set_security_label(inode, err);
5074 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005075 &exception);
5076 } while (exception.retry);
5077 return err;
5078}
5079
5080static int
Al Viro59301222016-05-27 10:19:30 -04005081nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005082{
5083 struct nfs4_label ilabel, *olabel = NULL;
5084 struct nfs_fattr fattr;
5085 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005086 int status;
5087
5088 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5089 return -EOPNOTSUPP;
5090
5091 nfs_fattr_init(&fattr);
5092
5093 ilabel.pi = 0;
5094 ilabel.lfs = 0;
5095 ilabel.label = (char *)buf;
5096 ilabel.len = buflen;
5097
5098 cred = rpc_lookup_cred();
5099 if (IS_ERR(cred))
5100 return PTR_ERR(cred);
5101
5102 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5103 if (IS_ERR(olabel)) {
5104 status = -PTR_ERR(olabel);
5105 goto out;
5106 }
5107
5108 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5109 if (status == 0)
5110 nfs_setsecurity(inode, &fattr, olabel);
5111
5112 nfs4_label_free(olabel);
5113out:
5114 put_rpccred(cred);
5115 return status;
5116}
5117#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5118
5119
Chuck Leverf0920752012-05-21 22:45:41 -04005120static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5121 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005122{
5123 __be32 verf[2];
5124
Chuck Lever2c820d92012-05-21 22:45:33 -04005125 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5126 /* An impossible timestamp guarantees this value
5127 * will never match a generated boot time. */
5128 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005129 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005130 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005131 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005132 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5133 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005134 }
Chuck Levercd937102012-03-02 17:14:31 -05005135 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5136}
5137
Jeff Laytona3192682015-06-09 19:43:59 -04005138static int
5139nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005140{
Jeff Laytona3192682015-06-09 19:43:59 -04005141 size_t len;
5142 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005143
Trond Myklebustceb3a162015-01-03 15:16:04 -05005144 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005145 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005146
Jeff Laytona3192682015-06-09 19:43:59 -04005147 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005148 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005149 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5150 1 +
5151 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5152 1;
5153 rcu_read_unlock();
5154
5155 if (len > NFS4_OPAQUE_LIMIT + 1)
5156 return -EINVAL;
5157
5158 /*
5159 * Since this string is allocated at mount time, and held until the
5160 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5161 * about a memory-reclaim deadlock.
5162 */
5163 str = kmalloc(len, GFP_KERNEL);
5164 if (!str)
5165 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005166
Chuck Levere984a552012-09-14 17:24:21 -04005167 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005168 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005169 clp->cl_ipaddr,
5170 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5171 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005172 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005173
Jeff Laytona3192682015-06-09 19:43:59 -04005174 clp->cl_owner_id = str;
5175 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005176}
5177
Jeff Layton873e3852015-06-09 19:44:00 -04005178static int
5179nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005180{
Jeff Layton873e3852015-06-09 19:44:00 -04005181 size_t len;
5182 char *str;
5183
5184 len = 10 + 10 + 1 + 10 + 1 +
5185 strlen(nfs4_client_id_uniquifier) + 1 +
5186 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5187
5188 if (len > NFS4_OPAQUE_LIMIT + 1)
5189 return -EINVAL;
5190
5191 /*
5192 * Since this string is allocated at mount time, and held until the
5193 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5194 * about a memory-reclaim deadlock.
5195 */
5196 str = kmalloc(len, GFP_KERNEL);
5197 if (!str)
5198 return -ENOMEM;
5199
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005200 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005201 clp->rpc_ops->version, clp->cl_minorversion,
5202 nfs4_client_id_uniquifier,
5203 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005204 clp->cl_owner_id = str;
5205 return 0;
5206}
5207
5208static int
5209nfs4_init_uniform_client_string(struct nfs_client *clp)
5210{
Jeff Layton873e3852015-06-09 19:44:00 -04005211 size_t len;
5212 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005213
5214 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005215 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005216
5217 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005218 return nfs4_init_uniquifier_client_string(clp);
5219
5220 len = 10 + 10 + 1 + 10 + 1 +
5221 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5222
5223 if (len > NFS4_OPAQUE_LIMIT + 1)
5224 return -EINVAL;
5225
5226 /*
5227 * Since this string is allocated at mount time, and held until the
5228 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5229 * about a memory-reclaim deadlock.
5230 */
5231 str = kmalloc(len, GFP_KERNEL);
5232 if (!str)
5233 return -ENOMEM;
5234
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005235 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005236 clp->rpc_ops->version, clp->cl_minorversion,
5237 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005238 clp->cl_owner_id = str;
5239 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005240}
5241
Chuck Lever706cb8d2014-03-12 12:51:47 -04005242/*
5243 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5244 * services. Advertise one based on the address family of the
5245 * clientaddr.
5246 */
5247static unsigned int
5248nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5249{
5250 if (strchr(clp->cl_ipaddr, ':') != NULL)
5251 return scnprintf(buf, len, "tcp6");
5252 else
5253 return scnprintf(buf, len, "tcp");
5254}
5255
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005256static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5257{
5258 struct nfs4_setclientid *sc = calldata;
5259
5260 if (task->tk_status == 0)
5261 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5262}
5263
5264static const struct rpc_call_ops nfs4_setclientid_ops = {
5265 .rpc_call_done = nfs4_setclientid_done,
5266};
5267
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005268/**
5269 * nfs4_proc_setclientid - Negotiate client ID
5270 * @clp: state data structure
5271 * @program: RPC program for NFSv4 callback service
5272 * @port: IP port number for NFS4 callback service
5273 * @cred: RPC credential to use for this call
5274 * @res: where to place the result
5275 *
5276 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5277 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005278int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5279 unsigned short port, struct rpc_cred *cred,
5280 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281{
5282 nfs4_verifier sc_verifier;
5283 struct nfs4_setclientid setclientid = {
5284 .sc_verifier = &sc_verifier,
5285 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005286 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 };
5288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5290 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005291 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005292 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005294 struct rpc_task *task;
5295 struct rpc_task_setup task_setup_data = {
5296 .rpc_client = clp->cl_rpcclient,
5297 .rpc_message = &msg,
5298 .callback_ops = &nfs4_setclientid_ops,
5299 .callback_data = &setclientid,
5300 .flags = RPC_TASK_TIMEOUT,
5301 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005302 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
Chuck Leverde734832012-07-11 16:30:50 -04005304 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005305 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005306
5307 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5308 status = nfs4_init_uniform_client_string(clp);
5309 else
Jeff Laytona3192682015-06-09 19:43:59 -04005310 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005311
5312 if (status)
5313 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005314
Chuck Leverde734832012-07-11 16:30:50 -04005315 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005316 setclientid.sc_netid_len =
5317 nfs4_init_callback_netid(clp,
5318 setclientid.sc_netid,
5319 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005320 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005321 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 clp->cl_ipaddr, port >> 8, port & 255);
5323
Jeff Layton3a6bb732015-06-09 19:43:57 -04005324 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005325 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005326 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005327 task = rpc_run_task(&task_setup_data);
5328 if (IS_ERR(task)) {
5329 status = PTR_ERR(task);
5330 goto out;
5331 }
5332 status = task->tk_status;
5333 if (setclientid.sc_cred) {
5334 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5335 put_rpccred(setclientid.sc_cred);
5336 }
5337 rpc_put_task(task);
5338out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005339 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005340 dprintk("NFS reply setclientid: %d\n", status);
5341 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005344/**
5345 * nfs4_proc_setclientid_confirm - Confirm client ID
5346 * @clp: state data structure
5347 * @res: result of a previous SETCLIENTID
5348 * @cred: RPC credential to use for this call
5349 *
5350 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5351 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005352int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005353 struct nfs4_setclientid_res *arg,
5354 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 struct rpc_message msg = {
5357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005358 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005359 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 int status;
5362
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005363 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5364 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5365 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005366 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005367 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005368 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 return status;
5370}
5371
Trond Myklebustfe650402006-01-03 09:55:18 +01005372struct nfs4_delegreturndata {
5373 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005374 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005375 struct nfs_fh fh;
5376 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005377 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005378 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005379 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005380 struct inode *inode;
5381 bool roc;
5382 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005383};
5384
Trond Myklebustfe650402006-01-03 09:55:18 +01005385static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5386{
5387 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005388
Trond Myklebust14516c32010-07-31 14:29:06 -04005389 if (!nfs4_sequence_done(task, &data->res.seq_res))
5390 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005391
Trond Myklebustca8acf82013-08-13 10:36:56 -04005392 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005393 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005394 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005395 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005396 case -NFS4ERR_ADMIN_REVOKED:
5397 case -NFS4ERR_DELEG_REVOKED:
5398 case -NFS4ERR_BAD_STATEID:
5399 case -NFS4ERR_OLD_STATEID:
5400 case -NFS4ERR_STALE_STATEID:
5401 case -NFS4ERR_EXPIRED:
5402 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005403 if (data->roc)
5404 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005405 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005406 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005407 if (nfs4_async_handle_error(task, data->res.server,
5408 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005409 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005410 return;
5411 }
5412 }
5413 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005414}
5415
5416static void nfs4_delegreturn_release(void *calldata)
5417{
Peng Tao039b7562014-07-03 13:05:02 +08005418 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005419 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005420
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005421 if (inode) {
5422 if (data->roc)
5423 pnfs_roc_release(inode);
5424 nfs_iput_and_deactive(inode);
5425 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005426 kfree(calldata);
5427}
5428
Andy Adamson938e1012009-04-01 09:22:28 -04005429static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5430{
5431 struct nfs4_delegreturndata *d_data;
5432
5433 d_data = (struct nfs4_delegreturndata *)data;
5434
Peng Tao500d7012015-09-22 11:35:22 +08005435 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5436 return;
5437
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005438 if (d_data->roc)
5439 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005440
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005441 nfs4_setup_sequence(d_data->res.server,
5442 &d_data->args.seq_args,
5443 &d_data->res.seq_res,
5444 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005445}
Andy Adamson938e1012009-04-01 09:22:28 -04005446
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005447static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005448 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005449 .rpc_call_done = nfs4_delegreturn_done,
5450 .rpc_release = nfs4_delegreturn_release,
5451};
5452
Trond Myklebuste6f81072008-01-24 18:14:34 -05005453static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005454{
5455 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005456 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005457 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005458 struct rpc_message msg = {
5459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5460 .rpc_cred = cred,
5461 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005462 struct rpc_task_setup task_setup_data = {
5463 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005464 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005465 .callback_ops = &nfs4_delegreturn_ops,
5466 .flags = RPC_TASK_ASYNC,
5467 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005468 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005469
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005470 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005471 if (data == NULL)
5472 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005473 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005474
5475 nfs4_state_protect(server->nfs_client,
5476 NFS_SP4_MACH_CRED_CLEANUP,
5477 &task_setup_data.rpc_client, &msg);
5478
Trond Myklebustfe650402006-01-03 09:55:18 +01005479 data->args.fhandle = &data->fh;
5480 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005481 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005482 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005483 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005484 data->res.fattr = &data->fattr;
5485 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005486 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005487 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005488 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005489 data->inode = nfs_igrab_and_active(inode);
5490 if (data->inode)
5491 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005492
Trond Myklebustc970aa82007-07-14 15:39:59 -04005493 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005494 msg.rpc_argp = &data->args;
5495 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005496 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005497 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005498 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005499 if (!issync)
5500 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005501 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005502 if (status != 0)
5503 goto out;
5504 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005505 if (status == 0)
5506 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5507 else
5508 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005509out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005510 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005511 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512}
5513
Trond Myklebuste6f81072008-01-24 18:14:34 -05005514int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
5516 struct nfs_server *server = NFS_SERVER(inode);
5517 struct nfs4_exception exception = { };
5518 int err;
5519 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005520 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005521 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 switch (err) {
5523 case -NFS4ERR_STALE_STATEID:
5524 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 case 0:
5526 return 0;
5527 }
5528 err = nfs4_handle_exception(server, err, &exception);
5529 } while (exception.retry);
5530 return err;
5531}
5532
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5534{
5535 struct inode *inode = state->inode;
5536 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005537 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005538 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005540 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005542 struct nfs_lockt_res res = {
5543 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 };
5545 struct rpc_message msg = {
5546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5547 .rpc_argp = &arg,
5548 .rpc_resp = &res,
5549 .rpc_cred = state->owner->so_cred,
5550 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 struct nfs4_lock_state *lsp;
5552 int status;
5553
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005554 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005555 status = nfs4_set_lock_state(state, request);
5556 if (status != 0)
5557 goto out;
5558 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005559 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005560 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005561 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005562 switch (status) {
5563 case 0:
5564 request->fl_type = F_UNLCK;
5565 break;
5566 case -NFS4ERR_DENIED:
5567 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005569 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005570 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 return status;
5573}
5574
5575static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5576{
5577 struct nfs4_exception exception = { };
5578 int err;
5579
5580 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005581 err = _nfs4_proc_getlk(state, cmd, request);
5582 trace_nfs4_get_lock(request, state, cmd, err);
5583 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 &exception);
5585 } while (exception.retry);
5586 return err;
5587}
5588
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005589struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005590 struct nfs_locku_args arg;
5591 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005592 struct nfs4_lock_state *lsp;
5593 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005594 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005595 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005596 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005597};
5598
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005599static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5600 struct nfs_open_context *ctx,
5601 struct nfs4_lock_state *lsp,
5602 struct nfs_seqid *seqid)
5603{
5604 struct nfs4_unlockdata *p;
5605 struct inode *inode = lsp->ls_state->inode;
5606
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005607 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005608 if (p == NULL)
5609 return NULL;
5610 p->arg.fh = NFS_FH(inode);
5611 p->arg.fl = &p->fl;
5612 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005613 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005614 p->lsp = lsp;
5615 atomic_inc(&lsp->ls_count);
5616 /* Ensure we don't close file until we're done freeing locks! */
5617 p->ctx = get_nfs_open_context(ctx);
5618 memcpy(&p->fl, fl, sizeof(p->fl));
5619 p->server = NFS_SERVER(inode);
5620 return p;
5621}
5622
Trond Myklebust06f814a2006-01-03 09:55:07 +01005623static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005624{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005625 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005626 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005627 nfs4_put_lock_state(calldata->lsp);
5628 put_nfs_open_context(calldata->ctx);
5629 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005630}
5631
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005632static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005633{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005634 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005635
Trond Myklebust14516c32010-07-31 14:29:06 -04005636 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5637 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005638 switch (task->tk_status) {
5639 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005640 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005641 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005642 if (nfs4_update_lock_stateid(calldata->lsp,
5643 &calldata->res.stateid))
5644 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005645 case -NFS4ERR_BAD_STATEID:
5646 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005647 case -NFS4ERR_STALE_STATEID:
5648 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005649 if (!nfs4_stateid_match(&calldata->arg.stateid,
5650 &calldata->lsp->ls_stateid))
5651 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005652 break;
5653 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005654 if (nfs4_async_handle_error(task, calldata->server,
5655 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005656 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005657 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005658 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005659}
5660
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005661static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005662{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005663 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005665 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005666 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005667 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005668 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005669 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005670 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005671 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005672 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005673 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005674 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005675 &calldata->res.seq_res,
5676 task) != 0)
5677 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005678 return;
5679out_no_action:
5680 task->tk_action = NULL;
5681out_wait:
5682 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683}
5684
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005685static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005686 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005687 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005688 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005689};
5690
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005691static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5692 struct nfs_open_context *ctx,
5693 struct nfs4_lock_state *lsp,
5694 struct nfs_seqid *seqid)
5695{
5696 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005697 struct rpc_message msg = {
5698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5699 .rpc_cred = ctx->cred,
5700 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005701 struct rpc_task_setup task_setup_data = {
5702 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005703 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005704 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005705 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005706 .flags = RPC_TASK_ASYNC,
5707 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005708
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005709 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5710 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5711
Frank Filz137d6ac2007-07-09 15:32:29 -07005712 /* Ensure this is an unlock - when canceling a lock, the
5713 * canceled lock is passed in, and it won't be an unlock.
5714 */
5715 fl->fl_type = F_UNLCK;
5716
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005717 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5718 if (data == NULL) {
5719 nfs_free_seqid(seqid);
5720 return ERR_PTR(-ENOMEM);
5721 }
5722
Chuck Levera9c92d62013-08-09 12:48:18 -04005723 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005724 msg.rpc_argp = &data->arg;
5725 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005726 task_setup_data.callback_data = data;
5727 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005728}
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5731{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005732 struct inode *inode = state->inode;
5733 struct nfs4_state_owner *sp = state->owner;
5734 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005735 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005736 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005737 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005738 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005739 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005740 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Trond Myklebust9b073572006-06-29 16:38:34 -04005742 status = nfs4_set_lock_state(state, request);
5743 /* Unlock _before_ we do the RPC call */
5744 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005745 /* Exclude nfs_delegation_claim_locks() */
5746 mutex_lock(&sp->so_delegreturn_mutex);
5747 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005748 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005749 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005750 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005751 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005752 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005753 }
5754 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005755 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005756 if (status != 0)
5757 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005758 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005759 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005760 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5761 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005762 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5763 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005764 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005765 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005766 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005767 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005768 status = PTR_ERR(task);
5769 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005770 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005771 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005772 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005773out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005774 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005775 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005776 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005779struct nfs4_lockdata {
5780 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005781 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005782 struct nfs4_lock_state *lsp;
5783 struct nfs_open_context *ctx;
5784 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005785 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005786 int rpc_status;
5787 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005788 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005789};
5790
5791static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005792 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5793 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005794{
5795 struct nfs4_lockdata *p;
5796 struct inode *inode = lsp->ls_state->inode;
5797 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005798 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005799
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005800 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005801 if (p == NULL)
5802 return NULL;
5803
5804 p->arg.fh = NFS_FH(inode);
5805 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005806 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005807 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005808 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005809 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5810 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005811 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005812 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005813 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005814 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005815 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005816 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005817 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005818 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005819 atomic_inc(&lsp->ls_count);
5820 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005821 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005822 memcpy(&p->fl, fl, sizeof(p->fl));
5823 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005824out_free_seqid:
5825 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005826out_free:
5827 kfree(p);
5828 return NULL;
5829}
5830
5831static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5832{
5833 struct nfs4_lockdata *data = calldata;
5834 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Harvey Harrison3110ff82008-05-02 13:42:44 -07005836 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005837 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005838 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005839 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005840 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005841 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005842 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005843 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005844 nfs4_stateid_copy(&data->arg.open_stateid,
5845 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005846 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005847 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005848 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005849 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005850 nfs4_stateid_copy(&data->arg.lock_stateid,
5851 &data->lsp->ls_stateid);
5852 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005853 if (!nfs4_valid_open_stateid(state)) {
5854 data->rpc_status = -EBADF;
5855 task->tk_action = NULL;
5856 goto out_release_open_seqid;
5857 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005858 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005859 if (nfs4_setup_sequence(data->server,
5860 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005861 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005862 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005863 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005864out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005865 nfs_release_seqid(data->arg.open_seqid);
5866out_release_lock_seqid:
5867 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005868out_wait:
5869 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005870 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005871}
5872
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005873static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5874{
5875 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005876 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005877
Harvey Harrison3110ff82008-05-02 13:42:44 -07005878 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005879
Trond Myklebust14516c32010-07-31 14:29:06 -04005880 if (!nfs4_sequence_done(task, &data->res.seq_res))
5881 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005882
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005883 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005884 switch (task->tk_status) {
5885 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005886 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005887 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005888 if (data->arg.new_lock) {
5889 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04005890 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005891 rpc_restart_call_prepare(task);
5892 break;
5893 }
5894 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005895 if (data->arg.new_lock_owner != 0) {
5896 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5897 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5898 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5899 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5900 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005901 break;
5902 case -NFS4ERR_BAD_STATEID:
5903 case -NFS4ERR_OLD_STATEID:
5904 case -NFS4ERR_STALE_STATEID:
5905 case -NFS4ERR_EXPIRED:
5906 if (data->arg.new_lock_owner != 0) {
5907 if (!nfs4_stateid_match(&data->arg.open_stateid,
5908 &lsp->ls_state->open_stateid))
5909 rpc_restart_call_prepare(task);
5910 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5911 &lsp->ls_stateid))
5912 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005913 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005914 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005915}
5916
5917static void nfs4_lock_release(void *calldata)
5918{
5919 struct nfs4_lockdata *data = calldata;
5920
Harvey Harrison3110ff82008-05-02 13:42:44 -07005921 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005922 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005923 if (data->cancelled != 0) {
5924 struct rpc_task *task;
5925 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5926 data->arg.lock_seqid);
5927 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005928 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005929 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930 } else
5931 nfs_free_seqid(data->arg.lock_seqid);
5932 nfs4_put_lock_state(data->lsp);
5933 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005934 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005936 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005937}
5938
5939static const struct rpc_call_ops nfs4_lock_ops = {
5940 .rpc_call_prepare = nfs4_lock_prepare,
5941 .rpc_call_done = nfs4_lock_done,
5942 .rpc_release = nfs4_lock_release,
5943};
5944
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005945static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5946{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005947 switch (error) {
5948 case -NFS4ERR_ADMIN_REVOKED:
5949 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005950 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005951 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005952 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005953 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005954 break;
5955 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005956 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005957 case -NFS4ERR_EXPIRED:
5958 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005959 };
5960}
5961
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005962static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963{
5964 struct nfs4_lockdata *data;
5965 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005966 struct rpc_message msg = {
5967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5968 .rpc_cred = state->owner->so_cred,
5969 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005970 struct rpc_task_setup task_setup_data = {
5971 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005972 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005973 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005974 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005975 .flags = RPC_TASK_ASYNC,
5976 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977 int ret;
5978
Harvey Harrison3110ff82008-05-02 13:42:44 -07005979 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005980 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005981 fl->fl_u.nfs4_fl.owner,
5982 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005983 if (data == NULL)
5984 return -ENOMEM;
5985 if (IS_SETLKW(cmd))
5986 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005987 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005988 msg.rpc_argp = &data->arg;
5989 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005990 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005991 if (recovery_type > NFS_LOCK_NEW) {
5992 if (recovery_type == NFS_LOCK_RECLAIM)
5993 data->arg.reclaim = NFS_LOCK_RECLAIM;
5994 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005995 } else
5996 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005997 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005998 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000 ret = nfs4_wait_for_completion_rpc_task(task);
6001 if (ret == 0) {
6002 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006003 if (ret)
6004 nfs4_handle_setlk_error(data->server, data->lsp,
6005 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006006 } else
6007 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006008 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006009 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006010 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006011 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012}
6013
6014static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6015{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006016 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006017 struct nfs4_exception exception = {
6018 .inode = state->inode,
6019 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006020 int err;
6021
6022 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006023 /* Cache the lock if possible... */
6024 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6025 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006026 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006027 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006028 break;
6029 nfs4_handle_exception(server, err, &exception);
6030 } while (exception.retry);
6031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032}
6033
6034static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6035{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006036 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006037 struct nfs4_exception exception = {
6038 .inode = state->inode,
6039 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006040 int err;
6041
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006042 err = nfs4_set_lock_state(state, request);
6043 if (err != 0)
6044 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006045 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006046 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6047 return 0;
6048 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006049 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006050 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6051 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006052 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006053 switch (err) {
6054 default:
6055 goto out;
6056 case -NFS4ERR_GRACE:
6057 case -NFS4ERR_DELAY:
6058 nfs4_handle_exception(server, err, &exception);
6059 err = 0;
6060 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006061 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006062out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064}
6065
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006066#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006067/**
6068 * nfs41_check_expired_locks - possibly free a lock stateid
6069 *
6070 * @state: NFSv4 state for an inode
6071 *
6072 * Returns NFS_OK if recovery for this stateid is now finished.
6073 * Otherwise a negative NFS4ERR value is returned.
6074 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006075static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006076{
Chuck Levereb64cf92012-07-11 16:30:05 -04006077 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006078 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006079 struct nfs_server *server = NFS_SERVER(state->inode);
6080
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006081 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006082 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006083 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6084
6085 status = nfs41_test_stateid(server,
6086 &lsp->ls_stateid,
6087 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006088 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006089 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006090 /* Free the stateid unless the server
6091 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006092 if (status != -NFS4ERR_BAD_STATEID)
6093 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006094 &lsp->ls_stateid,
6095 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006096 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006097 ret = status;
6098 }
6099 }
6100 };
6101
6102 return ret;
6103}
6104
6105static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6106{
6107 int status = NFS_OK;
6108
6109 if (test_bit(LK_STATE_IN_USE, &state->flags))
6110 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006111 if (status != NFS_OK)
6112 status = nfs4_lock_expired(state, request);
6113 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006114}
6115#endif
6116
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6118{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006119 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006120 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006121 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006122 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
Trond Myklebust01c3b862006-06-29 16:38:39 -04006124 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006125 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006126 if (status < 0)
6127 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006128 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006129 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006130 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006131 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006132 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006133 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006134 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006135 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006136 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006137 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006138 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006139 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006140 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006141 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006142out:
6143 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 return status;
6145}
6146
6147static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6148{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006149 struct nfs4_exception exception = {
6150 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006151 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 int err;
6154
6155 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006156 err = _nfs4_proc_setlk(state, cmd, request);
6157 if (err == -NFS4ERR_DENIED)
6158 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006160 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 } while (exception.retry);
6162 return err;
6163}
6164
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006165#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6166#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6167
6168static int
6169nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6170{
6171 int status = -ERESTARTSYS;
6172 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6173
6174 while(!signalled()) {
6175 status = nfs4_proc_setlk(state, cmd, request);
6176 if ((status != -EAGAIN) || IS_SETLK(cmd))
6177 break;
6178 freezable_schedule_timeout_interruptible(timeout);
6179 timeout *= 2;
6180 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6181 status = -ERESTARTSYS;
6182 }
6183 return status;
6184}
6185
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186static int
6187nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6188{
6189 struct nfs_open_context *ctx;
6190 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 int status;
6192
6193 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006194 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 state = ctx->state;
6196
6197 if (request->fl_start < 0 || request->fl_end < 0)
6198 return -EINVAL;
6199
Trond Myklebustd9531262009-07-21 19:22:38 -04006200 if (IS_GETLK(cmd)) {
6201 if (state != NULL)
6202 return nfs4_proc_getlk(state, F_GETLK, request);
6203 return 0;
6204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
6206 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6207 return -EINVAL;
6208
Trond Myklebustd9531262009-07-21 19:22:38 -04006209 if (request->fl_type == F_UNLCK) {
6210 if (state != NULL)
6211 return nfs4_proc_unlck(state, cmd, request);
6212 return 0;
6213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
Trond Myklebustd9531262009-07-21 19:22:38 -04006215 if (state == NULL)
6216 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006217
6218 if ((request->fl_flags & FL_POSIX) &&
6219 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6220 return -ENOLCK;
6221
Trond Myklebust55725512012-04-18 12:48:35 -04006222 /*
6223 * Don't rely on the VFS having checked the file open mode,
6224 * since it won't do this for flock() locks.
6225 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006226 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006227 case F_RDLCK:
6228 if (!(filp->f_mode & FMODE_READ))
6229 return -EBADF;
6230 break;
6231 case F_WRLCK:
6232 if (!(filp->f_mode & FMODE_WRITE))
6233 return -EBADF;
6234 }
6235
Jeff Layton1ea67db2016-09-17 18:17:37 -04006236 status = nfs4_set_lock_state(state, request);
6237 if (status != 0)
6238 return status;
6239
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006240 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241}
6242
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006243int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006244{
6245 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006246 int err;
6247
6248 err = nfs4_set_lock_state(state, fl);
6249 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006250 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006251 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006252 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006253}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006254
Trond Myklebustcf470c32012-03-07 13:49:12 -05006255struct nfs_release_lockowner_data {
6256 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006257 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006258 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006259 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006260 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006261};
6262
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006263static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6264{
6265 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006266 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006267 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6268 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006269 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006270 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006271}
6272
6273static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6274{
6275 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006276 struct nfs_server *server = data->server;
6277
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006278 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006279
6280 switch (task->tk_status) {
6281 case 0:
6282 renew_lease(server, data->timestamp);
6283 break;
6284 case -NFS4ERR_STALE_CLIENTID:
6285 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006286 nfs4_schedule_lease_recovery(server->nfs_client);
6287 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006288 case -NFS4ERR_LEASE_MOVED:
6289 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006290 if (nfs4_async_handle_error(task, server,
6291 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006292 rpc_restart_call_prepare(task);
6293 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006294}
6295
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006296static void nfs4_release_lockowner_release(void *calldata)
6297{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006298 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006299 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006300 kfree(calldata);
6301}
6302
Trond Myklebust17280172012-03-11 13:11:00 -04006303static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006304 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6305 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006306 .rpc_release = nfs4_release_lockowner_release,
6307};
6308
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006309static void
6310nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006311{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006312 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006313 struct rpc_message msg = {
6314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6315 };
6316
6317 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006318 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006319
Trond Myklebustcf470c32012-03-07 13:49:12 -05006320 data = kmalloc(sizeof(*data), GFP_NOFS);
6321 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006322 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006323 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006324 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006325 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6326 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6327 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006328
Trond Myklebustcf470c32012-03-07 13:49:12 -05006329 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006330 msg.rpc_resp = &data->res;
6331 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006332 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006333}
6334
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006335#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6336
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006337static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006338 struct dentry *unused, struct inode *inode,
6339 const char *key, const void *buf,
6340 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006341{
Al Viro59301222016-05-27 10:19:30 -04006342 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006343}
6344
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006345static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006346 struct dentry *unused, struct inode *inode,
6347 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006348{
Al Virob2968212016-04-10 20:48:24 -04006349 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006350}
6351
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006352static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006353{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006354 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006355}
6356
David Quigleyc9bccef2013-05-22 12:50:45 -04006357#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006358
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006359static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006360 struct dentry *unused, struct inode *inode,
6361 const char *key, const void *buf,
6362 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006363{
6364 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006365 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006366
6367 return -EOPNOTSUPP;
6368}
6369
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006370static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006371 struct dentry *unused, struct inode *inode,
6372 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006373{
6374 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006375 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006376 return -EOPNOTSUPP;
6377}
6378
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006379static ssize_t
6380nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006381{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006382 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006383
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006384 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6385 len = security_inode_listsecurity(inode, list, list_len);
6386 if (list_len && len > list_len)
6387 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006388 }
6389 return len;
6390}
6391
6392static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6393 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006394 .get = nfs4_xattr_get_nfs4_label,
6395 .set = nfs4_xattr_set_nfs4_label,
6396};
David Quigleyc9bccef2013-05-22 12:50:45 -04006397
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006398#else
6399
6400static ssize_t
6401nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6402{
6403 return 0;
6404}
6405
6406#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006407
Andy Adamson533eb462011-06-13 18:25:56 -04006408/*
6409 * nfs_fhget will use either the mounted_on_fileid or the fileid
6410 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006411static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6412{
Andy Adamson533eb462011-06-13 18:25:56 -04006413 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6414 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6415 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006416 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006417 return;
6418
6419 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006420 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006421 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6422 fattr->nlink = 2;
6423}
6424
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006425static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6426 const struct qstr *name,
6427 struct nfs4_fs_locations *fs_locations,
6428 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006429{
6430 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006431 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006432 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006433 };
6434 struct nfs4_fs_locations_arg args = {
6435 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006436 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006437 .page = page,
6438 .bitmask = bitmask,
6439 };
Benny Halevy22958462009-04-01 09:22:02 -04006440 struct nfs4_fs_locations_res res = {
6441 .fs_locations = fs_locations,
6442 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006443 struct rpc_message msg = {
6444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6445 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006446 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006447 };
6448 int status;
6449
Harvey Harrison3110ff82008-05-02 13:42:44 -07006450 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006451
6452 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6453 * is not supported */
6454 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6455 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6456 else
6457 bitmask[0] |= FATTR4_WORD0_FILEID;
6458
Trond Myklebustc228fd32007-01-13 02:28:11 -05006459 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006460 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006461 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006462 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006463 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006464 return status;
6465}
6466
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006467int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6468 const struct qstr *name,
6469 struct nfs4_fs_locations *fs_locations,
6470 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006471{
6472 struct nfs4_exception exception = { };
6473 int err;
6474 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006475 err = _nfs4_proc_fs_locations(client, dir, name,
6476 fs_locations, page);
6477 trace_nfs4_get_fs_locations(dir, name, err);
6478 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006479 &exception);
6480 } while (exception.retry);
6481 return err;
6482}
6483
Chuck Leverb03d7352013-10-17 14:12:50 -04006484/*
6485 * This operation also signals the server that this client is
6486 * performing migration recovery. The server can stop returning
6487 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6488 * appended to this compound to identify the client ID which is
6489 * performing recovery.
6490 */
6491static int _nfs40_proc_get_locations(struct inode *inode,
6492 struct nfs4_fs_locations *locations,
6493 struct page *page, struct rpc_cred *cred)
6494{
6495 struct nfs_server *server = NFS_SERVER(inode);
6496 struct rpc_clnt *clnt = server->client;
6497 u32 bitmask[2] = {
6498 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6499 };
6500 struct nfs4_fs_locations_arg args = {
6501 .clientid = server->nfs_client->cl_clientid,
6502 .fh = NFS_FH(inode),
6503 .page = page,
6504 .bitmask = bitmask,
6505 .migration = 1, /* skip LOOKUP */
6506 .renew = 1, /* append RENEW */
6507 };
6508 struct nfs4_fs_locations_res res = {
6509 .fs_locations = locations,
6510 .migration = 1,
6511 .renew = 1,
6512 };
6513 struct rpc_message msg = {
6514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6515 .rpc_argp = &args,
6516 .rpc_resp = &res,
6517 .rpc_cred = cred,
6518 };
6519 unsigned long now = jiffies;
6520 int status;
6521
6522 nfs_fattr_init(&locations->fattr);
6523 locations->server = server;
6524 locations->nlocations = 0;
6525
6526 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6527 nfs4_set_sequence_privileged(&args.seq_args);
6528 status = nfs4_call_sync_sequence(clnt, server, &msg,
6529 &args.seq_args, &res.seq_res);
6530 if (status)
6531 return status;
6532
6533 renew_lease(server, now);
6534 return 0;
6535}
6536
6537#ifdef CONFIG_NFS_V4_1
6538
6539/*
6540 * This operation also signals the server that this client is
6541 * performing migration recovery. The server can stop asserting
6542 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6543 * performing this operation is identified in the SEQUENCE
6544 * operation in this compound.
6545 *
6546 * When the client supports GETATTR(fs_locations_info), it can
6547 * be plumbed in here.
6548 */
6549static int _nfs41_proc_get_locations(struct inode *inode,
6550 struct nfs4_fs_locations *locations,
6551 struct page *page, struct rpc_cred *cred)
6552{
6553 struct nfs_server *server = NFS_SERVER(inode);
6554 struct rpc_clnt *clnt = server->client;
6555 u32 bitmask[2] = {
6556 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6557 };
6558 struct nfs4_fs_locations_arg args = {
6559 .fh = NFS_FH(inode),
6560 .page = page,
6561 .bitmask = bitmask,
6562 .migration = 1, /* skip LOOKUP */
6563 };
6564 struct nfs4_fs_locations_res res = {
6565 .fs_locations = locations,
6566 .migration = 1,
6567 };
6568 struct rpc_message msg = {
6569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6570 .rpc_argp = &args,
6571 .rpc_resp = &res,
6572 .rpc_cred = cred,
6573 };
6574 int status;
6575
6576 nfs_fattr_init(&locations->fattr);
6577 locations->server = server;
6578 locations->nlocations = 0;
6579
6580 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6581 nfs4_set_sequence_privileged(&args.seq_args);
6582 status = nfs4_call_sync_sequence(clnt, server, &msg,
6583 &args.seq_args, &res.seq_res);
6584 if (status == NFS4_OK &&
6585 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6586 status = -NFS4ERR_LEASE_MOVED;
6587 return status;
6588}
6589
6590#endif /* CONFIG_NFS_V4_1 */
6591
6592/**
6593 * nfs4_proc_get_locations - discover locations for a migrated FSID
6594 * @inode: inode on FSID that is migrating
6595 * @locations: result of query
6596 * @page: buffer
6597 * @cred: credential to use for this operation
6598 *
6599 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6600 * operation failed, or a negative errno if a local error occurred.
6601 *
6602 * On success, "locations" is filled in, but if the server has
6603 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6604 * asserted.
6605 *
6606 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6607 * from this client that require migration recovery.
6608 */
6609int nfs4_proc_get_locations(struct inode *inode,
6610 struct nfs4_fs_locations *locations,
6611 struct page *page, struct rpc_cred *cred)
6612{
6613 struct nfs_server *server = NFS_SERVER(inode);
6614 struct nfs_client *clp = server->nfs_client;
6615 const struct nfs4_mig_recovery_ops *ops =
6616 clp->cl_mvops->mig_recovery_ops;
6617 struct nfs4_exception exception = { };
6618 int status;
6619
6620 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6621 (unsigned long long)server->fsid.major,
6622 (unsigned long long)server->fsid.minor,
6623 clp->cl_hostname);
6624 nfs_display_fhandle(NFS_FH(inode), __func__);
6625
6626 do {
6627 status = ops->get_locations(inode, locations, page, cred);
6628 if (status != -NFS4ERR_DELAY)
6629 break;
6630 nfs4_handle_exception(server, status, &exception);
6631 } while (exception.retry);
6632 return status;
6633}
6634
Chuck Lever44c99932013-10-17 14:13:30 -04006635/*
6636 * This operation also signals the server that this client is
6637 * performing "lease moved" recovery. The server can stop
6638 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6639 * is appended to this compound to identify the client ID which is
6640 * performing recovery.
6641 */
6642static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6643{
6644 struct nfs_server *server = NFS_SERVER(inode);
6645 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6646 struct rpc_clnt *clnt = server->client;
6647 struct nfs4_fsid_present_arg args = {
6648 .fh = NFS_FH(inode),
6649 .clientid = clp->cl_clientid,
6650 .renew = 1, /* append RENEW */
6651 };
6652 struct nfs4_fsid_present_res res = {
6653 .renew = 1,
6654 };
6655 struct rpc_message msg = {
6656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6657 .rpc_argp = &args,
6658 .rpc_resp = &res,
6659 .rpc_cred = cred,
6660 };
6661 unsigned long now = jiffies;
6662 int status;
6663
6664 res.fh = nfs_alloc_fhandle();
6665 if (res.fh == NULL)
6666 return -ENOMEM;
6667
6668 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6669 nfs4_set_sequence_privileged(&args.seq_args);
6670 status = nfs4_call_sync_sequence(clnt, server, &msg,
6671 &args.seq_args, &res.seq_res);
6672 nfs_free_fhandle(res.fh);
6673 if (status)
6674 return status;
6675
6676 do_renew_lease(clp, now);
6677 return 0;
6678}
6679
6680#ifdef CONFIG_NFS_V4_1
6681
6682/*
6683 * This operation also signals the server that this client is
6684 * performing "lease moved" recovery. The server can stop asserting
6685 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6686 * this operation is identified in the SEQUENCE operation in this
6687 * compound.
6688 */
6689static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6690{
6691 struct nfs_server *server = NFS_SERVER(inode);
6692 struct rpc_clnt *clnt = server->client;
6693 struct nfs4_fsid_present_arg args = {
6694 .fh = NFS_FH(inode),
6695 };
6696 struct nfs4_fsid_present_res res = {
6697 };
6698 struct rpc_message msg = {
6699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6700 .rpc_argp = &args,
6701 .rpc_resp = &res,
6702 .rpc_cred = cred,
6703 };
6704 int status;
6705
6706 res.fh = nfs_alloc_fhandle();
6707 if (res.fh == NULL)
6708 return -ENOMEM;
6709
6710 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6711 nfs4_set_sequence_privileged(&args.seq_args);
6712 status = nfs4_call_sync_sequence(clnt, server, &msg,
6713 &args.seq_args, &res.seq_res);
6714 nfs_free_fhandle(res.fh);
6715 if (status == NFS4_OK &&
6716 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6717 status = -NFS4ERR_LEASE_MOVED;
6718 return status;
6719}
6720
6721#endif /* CONFIG_NFS_V4_1 */
6722
6723/**
6724 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6725 * @inode: inode on FSID to check
6726 * @cred: credential to use for this operation
6727 *
6728 * Server indicates whether the FSID is present, moved, or not
6729 * recognized. This operation is necessary to clear a LEASE_MOVED
6730 * condition for this client ID.
6731 *
6732 * Returns NFS4_OK if the FSID is present on this server,
6733 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6734 * NFS4ERR code if some error occurred on the server, or a
6735 * negative errno if a local failure occurred.
6736 */
6737int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6738{
6739 struct nfs_server *server = NFS_SERVER(inode);
6740 struct nfs_client *clp = server->nfs_client;
6741 const struct nfs4_mig_recovery_ops *ops =
6742 clp->cl_mvops->mig_recovery_ops;
6743 struct nfs4_exception exception = { };
6744 int status;
6745
6746 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6747 (unsigned long long)server->fsid.major,
6748 (unsigned long long)server->fsid.minor,
6749 clp->cl_hostname);
6750 nfs_display_fhandle(NFS_FH(inode), __func__);
6751
6752 do {
6753 status = ops->fsid_present(inode, cred);
6754 if (status != -NFS4ERR_DELAY)
6755 break;
6756 nfs4_handle_exception(server, status, &exception);
6757 } while (exception.retry);
6758 return status;
6759}
6760
Andy Adamson5ec16a82013-08-08 10:57:55 -04006761/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006762 * If 'use_integrity' is true and the state managment nfs_client
6763 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6764 * and the machine credential as per RFC3530bis and RFC5661 Security
6765 * Considerations sections. Otherwise, just use the user cred with the
6766 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006767 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006768static 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 +00006769{
6770 int status;
6771 struct nfs4_secinfo_arg args = {
6772 .dir_fh = NFS_FH(dir),
6773 .name = name,
6774 };
6775 struct nfs4_secinfo_res res = {
6776 .flavors = flavors,
6777 };
6778 struct rpc_message msg = {
6779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6780 .rpc_argp = &args,
6781 .rpc_resp = &res,
6782 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006783 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006784 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006785
6786 if (use_integrity) {
6787 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006788 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6789 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006790 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006791
6792 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006793
6794 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6795 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6796
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006797 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6798 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006799 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006800
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006801 if (cred)
6802 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006803
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006804 return status;
6805}
6806
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006807int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6808 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006809{
6810 struct nfs4_exception exception = { };
6811 int err;
6812 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006813 err = -NFS4ERR_WRONGSEC;
6814
6815 /* try to use integrity protection with machine cred */
6816 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6817 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6818
6819 /*
6820 * if unable to use integrity protection, or SECINFO with
6821 * integrity protection returns NFS4ERR_WRONGSEC (which is
6822 * disallowed by spec, but exists in deployed servers) use
6823 * the current filesystem's rpc_client and the user cred.
6824 */
6825 if (err == -NFS4ERR_WRONGSEC)
6826 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6827
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006828 trace_nfs4_secinfo(dir, name, err);
6829 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006830 &exception);
6831 } while (exception.retry);
6832 return err;
6833}
6834
Andy Adamson557134a2009-04-01 09:21:53 -04006835#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006836/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006837 * Check the exchange flags returned by the server for invalid flags, having
6838 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6839 * DS flags set.
6840 */
6841static int nfs4_check_cl_exchange_flags(u32 flags)
6842{
6843 if (flags & ~EXCHGID4_FLAG_MASK_R)
6844 goto out_inval;
6845 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6846 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6847 goto out_inval;
6848 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6849 goto out_inval;
6850 return NFS_OK;
6851out_inval:
6852 return -NFS4ERR_INVAL;
6853}
6854
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006855static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006856nfs41_same_server_scope(struct nfs41_server_scope *a,
6857 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006858{
6859 if (a->server_scope_sz == b->server_scope_sz &&
6860 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6861 return true;
6862
6863 return false;
6864}
6865
Andy Adamson02a95de2016-02-05 16:08:37 -05006866static void
6867nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6868{
6869}
6870
6871static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6872 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6873};
6874
Andy Adamson357f54d2010-12-14 10:11:57 -05006875/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006876 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006877 *
6878 * The 4.1 client currently uses the same TCP connection for the
6879 * fore and backchannel.
6880 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006881static
6882int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6883 struct rpc_xprt *xprt,
6884 struct nfs_client *clp,
6885 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006886{
6887 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006888 struct nfs41_bind_conn_to_session_args args = {
6889 .client = clp,
6890 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6891 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006892 struct nfs41_bind_conn_to_session_res res;
6893 struct rpc_message msg = {
6894 .rpc_proc =
6895 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006896 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006897 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006898 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006899 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006900 struct rpc_task_setup task_setup_data = {
6901 .rpc_client = clnt,
6902 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006903 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006904 .rpc_message = &msg,
6905 .flags = RPC_TASK_TIMEOUT,
6906 };
6907 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006908
6909 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006910
Trond Myklebust71a097c2015-02-18 09:27:18 -08006911 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6912 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6913 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006914
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006915 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6916 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6917 args.dir = NFS4_CDFC4_FORE;
6918
6919 task = rpc_run_task(&task_setup_data);
6920 if (!IS_ERR(task)) {
6921 status = task->tk_status;
6922 rpc_put_task(task);
6923 } else
6924 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006925 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006926 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006927 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006928 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6929 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6930 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006931 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006932 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006933 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006934 dprintk("NFS: %s: Unexpected direction from server\n",
6935 __func__);
6936 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006937 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006938 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006939 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006940 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6941 __func__);
6942 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006943 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006944 }
6945 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006946out:
6947 dprintk("<-- %s status= %d\n", __func__, status);
6948 return status;
6949}
6950
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006951struct rpc_bind_conn_calldata {
6952 struct nfs_client *clp;
6953 struct rpc_cred *cred;
6954};
6955
6956static int
6957nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6958 struct rpc_xprt *xprt,
6959 void *calldata)
6960{
6961 struct rpc_bind_conn_calldata *p = calldata;
6962
6963 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6964}
6965
6966int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6967{
6968 struct rpc_bind_conn_calldata data = {
6969 .clp = clp,
6970 .cred = cred,
6971 };
6972 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6973 nfs4_proc_bind_conn_to_session_callback, &data);
6974}
6975
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006976/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006977 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6978 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006979 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006980static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6981 .how = SP4_MACH_CRED,
6982 .enforce.u.words = {
6983 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6984 1 << (OP_EXCHANGE_ID - 32) |
6985 1 << (OP_CREATE_SESSION - 32) |
6986 1 << (OP_DESTROY_SESSION - 32) |
6987 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006988 },
6989 .allow.u.words = {
6990 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006991 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006992 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006993 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006994 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006995 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006996 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006997 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006998 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006999 1 << (OP_FREE_STATEID - 32) |
7000 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007001 }
7002};
7003
7004/*
7005 * Select the state protection mode for client `clp' given the server results
7006 * from exchange_id in `sp'.
7007 *
7008 * Returns 0 on success, negative errno otherwise.
7009 */
7010static int nfs4_sp4_select_mode(struct nfs_client *clp,
7011 struct nfs41_state_protection *sp)
7012{
7013 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7014 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7015 1 << (OP_EXCHANGE_ID - 32) |
7016 1 << (OP_CREATE_SESSION - 32) |
7017 1 << (OP_DESTROY_SESSION - 32) |
7018 1 << (OP_DESTROY_CLIENTID - 32)
7019 };
7020 unsigned int i;
7021
7022 if (sp->how == SP4_MACH_CRED) {
7023 /* Print state protect result */
7024 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7025 for (i = 0; i <= LAST_NFS4_OP; i++) {
7026 if (test_bit(i, sp->enforce.u.longs))
7027 dfprintk(MOUNT, " enforce op %d\n", i);
7028 if (test_bit(i, sp->allow.u.longs))
7029 dfprintk(MOUNT, " allow op %d\n", i);
7030 }
7031
7032 /* make sure nothing is on enforce list that isn't supported */
7033 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7034 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7035 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7036 return -EINVAL;
7037 }
7038 }
7039
7040 /*
7041 * Minimal mode - state operations are allowed to use machine
7042 * credential. Note this already happens by default, so the
7043 * client doesn't have to do anything more than the negotiation.
7044 *
7045 * NOTE: we don't care if EXCHANGE_ID is in the list -
7046 * we're already using the machine cred for exchange_id
7047 * and will never use a different cred.
7048 */
7049 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7050 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7051 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7052 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7053 dfprintk(MOUNT, "sp4_mach_cred:\n");
7054 dfprintk(MOUNT, " minimal mode enabled\n");
7055 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7056 } else {
7057 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7058 return -EINVAL;
7059 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007060
7061 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007062 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7063 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007064 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7065 dfprintk(MOUNT, " cleanup mode enabled\n");
7066 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7067 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007068
Andrew Elble99ade3c2015-12-02 09:39:51 -05007069 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7070 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7071 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7072 &clp->cl_sp4_flags);
7073 }
7074
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007075 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7076 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7077 dfprintk(MOUNT, " secinfo mode enabled\n");
7078 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7079 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007080
7081 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7082 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7083 dfprintk(MOUNT, " stateid mode enabled\n");
7084 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7085 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007086
7087 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7088 dfprintk(MOUNT, " write mode enabled\n");
7089 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7090 }
7091
7092 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7093 dfprintk(MOUNT, " commit mode enabled\n");
7094 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7095 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007096 }
7097
7098 return 0;
7099}
7100
Andy Adamson8d89bd72016-09-09 09:22:18 -04007101struct nfs41_exchange_id_data {
7102 struct nfs41_exchange_id_res res;
7103 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007104 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007105 int rpc_status;
7106};
7107
7108static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7109{
7110 struct nfs41_exchange_id_data *cdata =
7111 (struct nfs41_exchange_id_data *)data;
7112 struct nfs_client *clp = cdata->args.client;
7113 int status = task->tk_status;
7114
7115 trace_nfs4_exchange_id(clp, status);
7116
7117 if (status == 0)
7118 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007119
7120 if (cdata->xprt && status == 0) {
7121 status = nfs4_detect_session_trunking(clp, &cdata->res,
7122 cdata->xprt);
7123 goto out;
7124 }
7125
Andy Adamson8d89bd72016-09-09 09:22:18 -04007126 if (status == 0)
7127 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7128
7129 if (status == 0) {
7130 clp->cl_clientid = cdata->res.clientid;
7131 clp->cl_exchange_flags = cdata->res.flags;
7132 /* Client ID is not confirmed */
7133 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7134 clear_bit(NFS4_SESSION_ESTABLISHED,
7135 &clp->cl_session->session_state);
7136 clp->cl_seqid = cdata->res.seqid;
7137 }
7138
7139 kfree(clp->cl_serverowner);
7140 clp->cl_serverowner = cdata->res.server_owner;
7141 cdata->res.server_owner = NULL;
7142
7143 /* use the most recent implementation id */
7144 kfree(clp->cl_implid);
7145 clp->cl_implid = cdata->res.impl_id;
7146 cdata->res.impl_id = NULL;
7147
7148 if (clp->cl_serverscope != NULL &&
7149 !nfs41_same_server_scope(clp->cl_serverscope,
7150 cdata->res.server_scope)) {
7151 dprintk("%s: server_scope mismatch detected\n",
7152 __func__);
7153 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7154 kfree(clp->cl_serverscope);
7155 clp->cl_serverscope = NULL;
7156 }
7157
7158 if (clp->cl_serverscope == NULL) {
7159 clp->cl_serverscope = cdata->res.server_scope;
7160 cdata->res.server_scope = NULL;
7161 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007162 /* Save the EXCHANGE_ID verifier session trunk tests */
7163 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7164 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007165 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007166out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007167 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007168 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007169}
7170
7171static void nfs4_exchange_id_release(void *data)
7172{
7173 struct nfs41_exchange_id_data *cdata =
7174 (struct nfs41_exchange_id_data *)data;
7175
7176 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007177 if (cdata->xprt) {
7178 xprt_put(cdata->xprt);
7179 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7180 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007181 kfree(cdata->res.impl_id);
7182 kfree(cdata->res.server_scope);
7183 kfree(cdata->res.server_owner);
7184 kfree(cdata);
7185}
7186
7187static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7188 .rpc_call_done = nfs4_exchange_id_done,
7189 .rpc_release = nfs4_exchange_id_release,
7190};
7191
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007192/*
7193 * _nfs4_proc_exchange_id()
7194 *
7195 * Wrapper for EXCHANGE_ID operation.
7196 */
7197static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007198 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007199{
7200 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007201 struct rpc_message msg = {
7202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007203 .rpc_cred = cred,
7204 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007205 struct rpc_task_setup task_setup_data = {
7206 .rpc_client = clp->cl_rpcclient,
7207 .callback_ops = &nfs4_exchange_id_call_ops,
7208 .rpc_message = &msg,
7209 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7210 };
7211 struct nfs41_exchange_id_data *calldata;
7212 struct rpc_task *task;
7213 int status = -EIO;
7214
7215 if (!atomic_inc_not_zero(&clp->cl_count))
7216 goto out;
7217
7218 status = -ENOMEM;
7219 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7220 if (!calldata)
7221 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007222
Andy Adamsonad0849a2016-09-09 09:22:28 -04007223 if (!xprt)
7224 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007225
7226 status = nfs4_init_uniform_client_string(clp);
7227 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007228 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007229
7230 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007231 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007232 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007233
Andy Adamson8d89bd72016-09-09 09:22:18 -04007234 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7235 GFP_NOFS);
7236 status = -ENOMEM;
7237 if (unlikely(calldata->res.server_owner == NULL))
7238 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007239
Andy Adamson8d89bd72016-09-09 09:22:18 -04007240 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007241 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007242 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007243 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007244
Andy Adamson8d89bd72016-09-09 09:22:18 -04007245 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7246 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007247 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007248
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007249 switch (sp4_how) {
7250 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007251 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007252 break;
7253
7254 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007255 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007256 break;
7257
7258 default:
7259 /* unsupported! */
7260 WARN_ON_ONCE(1);
7261 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007262 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007263 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007264 if (xprt) {
7265 calldata->xprt = xprt;
7266 task_setup_data.rpc_xprt = xprt;
7267 task_setup_data.flags =
7268 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7269 calldata->args.verifier = &clp->cl_confirm;
7270 } else {
7271 calldata->args.verifier = &verifier;
7272 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007273 calldata->args.client = clp;
7274#ifdef CONFIG_NFS_V4_1_MIGRATION
7275 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7276 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7277 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7278#else
7279 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7280 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7281#endif
7282 msg.rpc_argp = &calldata->args;
7283 msg.rpc_resp = &calldata->res;
7284 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007285
Andy Adamson8d89bd72016-09-09 09:22:18 -04007286 task = rpc_run_task(&task_setup_data);
7287 if (IS_ERR(task)) {
7288 status = PTR_ERR(task);
7289 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007290 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007291
Andy Adamsonad0849a2016-09-09 09:22:28 -04007292 if (!xprt) {
7293 status = rpc_wait_for_completion_task(task);
7294 if (!status)
7295 status = calldata->rpc_status;
7296 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007297 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007298
Andy Adamson8d89bd72016-09-09 09:22:18 -04007299 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007300out:
Chuck Lever177313f2012-05-21 22:44:58 -04007301 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007302 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007303 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007304 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007305 clp->cl_implid->date.seconds,
7306 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007307 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007308 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007309
7310out_impl_id:
7311 kfree(calldata->res.impl_id);
7312out_server_scope:
7313 kfree(calldata->res.server_scope);
7314out_server_owner:
7315 kfree(calldata->res.server_owner);
7316out_calldata:
7317 kfree(calldata);
7318 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007319}
7320
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007321/*
7322 * nfs4_proc_exchange_id()
7323 *
7324 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7325 *
7326 * Since the clientid has expired, all compounds using sessions
7327 * associated with the stale clientid will be returning
7328 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7329 * be in some phase of session reset.
7330 *
7331 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7332 */
7333int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7334{
7335 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7336 int status;
7337
7338 /* try SP4_MACH_CRED if krb5i/p */
7339 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7340 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007341 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007342 if (!status)
7343 return 0;
7344 }
7345
7346 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007347 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007348}
7349
Andy Adamson04fa2c62016-09-09 09:22:29 -04007350/**
7351 * nfs4_test_session_trunk
7352 *
7353 * This is an add_xprt_test() test function called from
7354 * rpc_clnt_setup_test_and_add_xprt.
7355 *
7356 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7357 * and is dereferrenced in nfs4_exchange_id_release
7358 *
7359 * Upon success, add the new transport to the rpc_clnt
7360 *
7361 * @clnt: struct rpc_clnt to get new transport
7362 * @xprt: the rpc_xprt to test
7363 * @data: call data for _nfs4_proc_exchange_id.
7364 */
7365int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7366 void *data)
7367{
7368 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7369 u32 sp4_how;
7370
7371 dprintk("--> %s try %s\n", __func__,
7372 xprt->address_strings[RPC_DISPLAY_ADDR]);
7373
7374 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7375
7376 /* Test connection for session trunking. Async exchange_id call */
7377 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7378}
7379EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7380
Trond Myklebust66245532012-05-25 17:18:09 -04007381static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7382 struct rpc_cred *cred)
7383{
7384 struct rpc_message msg = {
7385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7386 .rpc_argp = clp,
7387 .rpc_cred = cred,
7388 };
7389 int status;
7390
7391 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007392 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007393 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007394 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007395 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7396 return status;
7397}
7398
7399static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7400 struct rpc_cred *cred)
7401{
7402 unsigned int loop;
7403 int ret;
7404
7405 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7406 ret = _nfs4_proc_destroy_clientid(clp, cred);
7407 switch (ret) {
7408 case -NFS4ERR_DELAY:
7409 case -NFS4ERR_CLIENTID_BUSY:
7410 ssleep(1);
7411 break;
7412 default:
7413 return ret;
7414 }
7415 }
7416 return 0;
7417}
7418
7419int nfs4_destroy_clientid(struct nfs_client *clp)
7420{
7421 struct rpc_cred *cred;
7422 int ret = 0;
7423
7424 if (clp->cl_mvops->minor_version < 1)
7425 goto out;
7426 if (clp->cl_exchange_flags == 0)
7427 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007428 if (clp->cl_preserve_clid)
7429 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007430 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007431 ret = nfs4_proc_destroy_clientid(clp, cred);
7432 if (cred)
7433 put_rpccred(cred);
7434 switch (ret) {
7435 case 0:
7436 case -NFS4ERR_STALE_CLIENTID:
7437 clp->cl_exchange_flags = 0;
7438 }
7439out:
7440 return ret;
7441}
7442
Andy Adamson2050f0c2009-04-01 09:22:30 -04007443struct nfs4_get_lease_time_data {
7444 struct nfs4_get_lease_time_args *args;
7445 struct nfs4_get_lease_time_res *res;
7446 struct nfs_client *clp;
7447};
7448
7449static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7450 void *calldata)
7451{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007452 struct nfs4_get_lease_time_data *data =
7453 (struct nfs4_get_lease_time_data *)calldata;
7454
7455 dprintk("--> %s\n", __func__);
7456 /* just setup sequence, do not trigger session recovery
7457 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007458 nfs41_setup_sequence(data->clp->cl_session,
7459 &data->args->la_seq_args,
7460 &data->res->lr_seq_res,
7461 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007462 dprintk("<-- %s\n", __func__);
7463}
7464
7465/*
7466 * Called from nfs4_state_manager thread for session setup, so don't recover
7467 * from sequence operation or clientid errors.
7468 */
7469static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7470{
7471 struct nfs4_get_lease_time_data *data =
7472 (struct nfs4_get_lease_time_data *)calldata;
7473
7474 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007475 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7476 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007477 switch (task->tk_status) {
7478 case -NFS4ERR_DELAY:
7479 case -NFS4ERR_GRACE:
7480 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7481 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7482 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007483 /* fall through */
7484 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007485 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007486 return;
7487 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007488 dprintk("<-- %s\n", __func__);
7489}
7490
Trond Myklebust17280172012-03-11 13:11:00 -04007491static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007492 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7493 .rpc_call_done = nfs4_get_lease_time_done,
7494};
7495
7496int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7497{
7498 struct rpc_task *task;
7499 struct nfs4_get_lease_time_args args;
7500 struct nfs4_get_lease_time_res res = {
7501 .lr_fsinfo = fsinfo,
7502 };
7503 struct nfs4_get_lease_time_data data = {
7504 .args = &args,
7505 .res = &res,
7506 .clp = clp,
7507 };
7508 struct rpc_message msg = {
7509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7510 .rpc_argp = &args,
7511 .rpc_resp = &res,
7512 };
7513 struct rpc_task_setup task_setup = {
7514 .rpc_client = clp->cl_rpcclient,
7515 .rpc_message = &msg,
7516 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007517 .callback_data = &data,
7518 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007519 };
7520 int status;
7521
Chuck Levera9c92d62013-08-09 12:48:18 -04007522 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007523 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007524 dprintk("--> %s\n", __func__);
7525 task = rpc_run_task(&task_setup);
7526
7527 if (IS_ERR(task))
7528 status = PTR_ERR(task);
7529 else {
7530 status = task->tk_status;
7531 rpc_put_task(task);
7532 }
7533 dprintk("<-- %s return %d\n", __func__, status);
7534
7535 return status;
7536}
7537
Andy Adamsonfc931582009-04-01 09:22:31 -04007538/*
7539 * Initialize the values to be used by the client in CREATE_SESSION
7540 * If nfs4_init_session set the fore channel request and response sizes,
7541 * use them.
7542 *
7543 * Set the back channel max_resp_sz_cached to zero to force the client to
7544 * always set csa_cachethis to FALSE because the current implementation
7545 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7546 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007547static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7548 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007549{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007550 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007551 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007552
Andy Adamson18aad3d2013-06-26 12:21:49 -04007553 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7554 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7555
Andy Adamsonfc931582009-04-01 09:22:31 -04007556 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007557 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7558 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007559 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007560 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007561
7562 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007563 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007564 __func__,
7565 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007566 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007567
7568 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007569 args->bc_attrs.max_rqst_sz = max_bc_payload;
7570 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007571 args->bc_attrs.max_resp_sz_cached = 0;
7572 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007573 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007574
7575 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7576 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7577 __func__,
7578 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7579 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7580 args->bc_attrs.max_reqs);
7581}
7582
Trond Myklebust79969dd2015-02-18 11:30:18 -08007583static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7584 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007585{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007586 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007587 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007588
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007589 if (rcvd->max_resp_sz > sent->max_resp_sz)
7590 return -EINVAL;
7591 /*
7592 * Our requested max_ops is the minimum we need; we're not
7593 * prepared to break up compounds into smaller pieces than that.
7594 * So, no point even trying to continue if the server won't
7595 * cooperate:
7596 */
7597 if (rcvd->max_ops < sent->max_ops)
7598 return -EINVAL;
7599 if (rcvd->max_reqs == 0)
7600 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007601 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7602 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007603 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007604}
7605
Trond Myklebust79969dd2015-02-18 11:30:18 -08007606static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7607 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007608{
7609 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007610 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007611
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007612 if (!(res->flags & SESSION4_BACK_CHAN))
7613 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007614 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7615 return -EINVAL;
7616 if (rcvd->max_resp_sz < sent->max_resp_sz)
7617 return -EINVAL;
7618 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7619 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007620 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007621 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007622 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007623 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007624out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007625 return 0;
7626}
Andy Adamson8d353012009-04-01 09:22:32 -04007627
Andy Adamson8d353012009-04-01 09:22:32 -04007628static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007629 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007630{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007631 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007632
Trond Myklebust79969dd2015-02-18 11:30:18 -08007633 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007634 if (ret)
7635 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007636 return nfs4_verify_back_channel_attrs(args, res);
7637}
7638
7639static void nfs4_update_session(struct nfs4_session *session,
7640 struct nfs41_create_session_res *res)
7641{
7642 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007643 /* Mark client id and session as being confirmed */
7644 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7645 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007646 session->flags = res->flags;
7647 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007648 if (res->flags & SESSION4_BACK_CHAN)
7649 memcpy(&session->bc_attrs, &res->bc_attrs,
7650 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007651}
7652
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007653static int _nfs4_proc_create_session(struct nfs_client *clp,
7654 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007655{
7656 struct nfs4_session *session = clp->cl_session;
7657 struct nfs41_create_session_args args = {
7658 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007659 .clientid = clp->cl_clientid,
7660 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007661 .cb_program = NFS4_CALLBACK,
7662 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007663 struct nfs41_create_session_res res;
7664
Andy Adamsonfc931582009-04-01 09:22:31 -04007665 struct rpc_message msg = {
7666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7667 .rpc_argp = &args,
7668 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007669 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007670 };
7671 int status;
7672
Chuck Lever6b26cc82016-05-02 14:40:40 -04007673 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007674 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007675
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007676 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007677 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007678
Trond Myklebustb519d402016-09-11 14:50:01 -04007679 switch (status) {
7680 case -NFS4ERR_STALE_CLIENTID:
7681 case -NFS4ERR_DELAY:
7682 case -ETIMEDOUT:
7683 case -EACCES:
7684 case -EAGAIN:
7685 goto out;
7686 };
7687
7688 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007689 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007690 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007691 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007692 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007693 if (status)
7694 goto out;
7695 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007696 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007697out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007698 return status;
7699}
7700
7701/*
7702 * Issues a CREATE_SESSION operation to the server.
7703 * It is the responsibility of the caller to verify the session is
7704 * expired before calling this routine.
7705 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007706int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007707{
7708 int status;
7709 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007710 struct nfs4_session *session = clp->cl_session;
7711
7712 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7713
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007714 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007715 if (status)
7716 goto out;
7717
Andy Adamsonaacd5532011-11-09 13:58:21 -05007718 /* Init or reset the session slot tables */
7719 status = nfs4_setup_session_slot_tables(session);
7720 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007721 if (status)
7722 goto out;
7723
7724 ptr = (unsigned *)&session->sess_id.data[0];
7725 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7726 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007727out:
7728 dprintk("<-- %s\n", __func__);
7729 return status;
7730}
7731
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007732/*
7733 * Issue the over-the-wire RPC DESTROY_SESSION.
7734 * The caller must serialize access to this routine.
7735 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007736int nfs4_proc_destroy_session(struct nfs4_session *session,
7737 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007738{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007739 struct rpc_message msg = {
7740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7741 .rpc_argp = session,
7742 .rpc_cred = cred,
7743 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007744 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007745
7746 dprintk("--> nfs4_proc_destroy_session\n");
7747
7748 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007749 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7750 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007751
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007752 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007753 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007754
7755 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007756 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007757 "Session has been destroyed regardless...\n", status);
7758
7759 dprintk("<-- nfs4_proc_destroy_session\n");
7760 return status;
7761}
7762
Trond Myklebust7b38c362012-05-23 13:23:31 -04007763/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007764 * Renew the cl_session lease.
7765 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007766struct nfs4_sequence_data {
7767 struct nfs_client *clp;
7768 struct nfs4_sequence_args args;
7769 struct nfs4_sequence_res res;
7770};
7771
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007772static void nfs41_sequence_release(void *data)
7773{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007774 struct nfs4_sequence_data *calldata = data;
7775 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007776
Alexandros Batsakis71358402010-02-05 03:45:05 -08007777 if (atomic_read(&clp->cl_count) > 1)
7778 nfs4_schedule_state_renewal(clp);
7779 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007780 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007781}
7782
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007783static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7784{
7785 switch(task->tk_status) {
7786 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007787 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7788 return -EAGAIN;
7789 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007790 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007791 }
7792 return 0;
7793}
7794
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007795static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007796{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007797 struct nfs4_sequence_data *calldata = data;
7798 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007799
Trond Myklebust14516c32010-07-31 14:29:06 -04007800 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7801 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007802
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007803 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007804 if (task->tk_status < 0) {
7805 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007806 if (atomic_read(&clp->cl_count) == 1)
7807 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007808
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007809 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7810 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007811 return;
7812 }
7813 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007814 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007815out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007816 dprintk("<-- %s\n", __func__);
7817}
7818
7819static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7820{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007821 struct nfs4_sequence_data *calldata = data;
7822 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007823 struct nfs4_sequence_args *args;
7824 struct nfs4_sequence_res *res;
7825
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007826 args = task->tk_msg.rpc_argp;
7827 res = task->tk_msg.rpc_resp;
7828
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007829 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007830}
7831
7832static const struct rpc_call_ops nfs41_sequence_ops = {
7833 .rpc_call_done = nfs41_sequence_call_done,
7834 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007835 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007836};
7837
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007838static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7839 struct rpc_cred *cred,
7840 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007841{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007842 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007843 struct rpc_message msg = {
7844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7845 .rpc_cred = cred,
7846 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007847 struct rpc_task_setup task_setup_data = {
7848 .rpc_client = clp->cl_rpcclient,
7849 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007850 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007851 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007852 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007853
Alexandros Batsakis71358402010-02-05 03:45:05 -08007854 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007855 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007856 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007857 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007858 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007859 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007860 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007861 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007862 if (is_privileged)
7863 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007864 msg.rpc_argp = &calldata->args;
7865 msg.rpc_resp = &calldata->res;
7866 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007867 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007868
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007869 return rpc_run_task(&task_setup_data);
7870}
7871
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007872static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007873{
7874 struct rpc_task *task;
7875 int ret = 0;
7876
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007877 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007878 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007879 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007880 if (IS_ERR(task))
7881 ret = PTR_ERR(task);
7882 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007883 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007884 dprintk("<-- %s status=%d\n", __func__, ret);
7885 return ret;
7886}
7887
7888static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7889{
7890 struct rpc_task *task;
7891 int ret;
7892
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007893 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007894 if (IS_ERR(task)) {
7895 ret = PTR_ERR(task);
7896 goto out;
7897 }
7898 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007899 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007900 ret = task->tk_status;
7901 rpc_put_task(task);
7902out:
7903 dprintk("<-- %s status=%d\n", __func__, ret);
7904 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007905}
7906
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007907struct nfs4_reclaim_complete_data {
7908 struct nfs_client *clp;
7909 struct nfs41_reclaim_complete_args arg;
7910 struct nfs41_reclaim_complete_res res;
7911};
7912
7913static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7914{
7915 struct nfs4_reclaim_complete_data *calldata = data;
7916
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007917 nfs41_setup_sequence(calldata->clp->cl_session,
7918 &calldata->arg.seq_args,
7919 &calldata->res.seq_res,
7920 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007921}
7922
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007923static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7924{
7925 switch(task->tk_status) {
7926 case 0:
7927 case -NFS4ERR_COMPLETE_ALREADY:
7928 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7929 break;
7930 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007932 /* fall through */
7933 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007934 return -EAGAIN;
7935 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007936 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007937 }
7938 return 0;
7939}
7940
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007941static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7942{
7943 struct nfs4_reclaim_complete_data *calldata = data;
7944 struct nfs_client *clp = calldata->clp;
7945 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7946
7947 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007948 if (!nfs41_sequence_done(task, res))
7949 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007950
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007951 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007952 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7953 rpc_restart_call_prepare(task);
7954 return;
7955 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007956 dprintk("<-- %s\n", __func__);
7957}
7958
7959static void nfs4_free_reclaim_complete_data(void *data)
7960{
7961 struct nfs4_reclaim_complete_data *calldata = data;
7962
7963 kfree(calldata);
7964}
7965
7966static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7967 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7968 .rpc_call_done = nfs4_reclaim_complete_done,
7969 .rpc_release = nfs4_free_reclaim_complete_data,
7970};
7971
7972/*
7973 * Issue a global reclaim complete.
7974 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007975static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7976 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007977{
7978 struct nfs4_reclaim_complete_data *calldata;
7979 struct rpc_task *task;
7980 struct rpc_message msg = {
7981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007982 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007983 };
7984 struct rpc_task_setup task_setup_data = {
7985 .rpc_client = clp->cl_rpcclient,
7986 .rpc_message = &msg,
7987 .callback_ops = &nfs4_reclaim_complete_call_ops,
7988 .flags = RPC_TASK_ASYNC,
7989 };
7990 int status = -ENOMEM;
7991
7992 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007993 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007994 if (calldata == NULL)
7995 goto out;
7996 calldata->clp = clp;
7997 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007998
Chuck Levera9c92d62013-08-09 12:48:18 -04007999 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008000 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008001 msg.rpc_argp = &calldata->arg;
8002 msg.rpc_resp = &calldata->res;
8003 task_setup_data.callback_data = calldata;
8004 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008005 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008006 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008007 goto out;
8008 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008009 status = nfs4_wait_for_completion_rpc_task(task);
8010 if (status == 0)
8011 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008012 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008013 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008014out:
8015 dprintk("<-- %s status=%d\n", __func__, status);
8016 return status;
8017}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008018
8019static void
8020nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8021{
8022 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008023 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008024 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008025
8026 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008027 nfs41_setup_sequence(session, &lgp->args.seq_args,
8028 &lgp->res.seq_res, task);
8029 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008030}
8031
8032static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8033{
8034 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008035
8036 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008037 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008038 dprintk("<-- %s\n", __func__);
8039}
8040
8041static int
8042nfs4_layoutget_handle_exception(struct rpc_task *task,
8043 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8044{
Trond Myklebustee314c22012-10-01 17:25:48 -07008045 struct inode *inode = lgp->args.inode;
8046 struct nfs_server *server = NFS_SERVER(inode);
8047 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008048 int nfs4err = task->tk_status;
8049 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008050 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008051
Boaz Harroshed7e5422014-01-22 20:34:54 +02008052 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008053
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008054 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008055 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008056 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008057
8058 /*
8059 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8060 * on the file. set tk_status to -ENODATA to tell upper layer to
8061 * retry go inband.
8062 */
8063 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008064 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008065 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008066 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008067 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8068 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8069 */
8070 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008071 status = -EOVERFLOW;
8072 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008073 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008074 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008075 * (or clients) writing to the same RAID stripe except when
8076 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008077 *
8078 * Treat it like we would RECALLCONFLICT -- we retry for a little
8079 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008080 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008081 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008082 if (lgp->args.minlength == 0) {
8083 status = -EOVERFLOW;
8084 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008085 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008086 status = -EBUSY;
8087 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008088 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008089 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008090 break;
Trond Myklebustee314c22012-10-01 17:25:48 -07008091 case -NFS4ERR_EXPIRED:
8092 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008093 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008094 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008095 lo = NFS_I(inode)->layout;
8096 /* If the open stateid was bad, then recover it. */
8097 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8098 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008099 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008100 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008101 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04008102 break;
8103 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008104
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008105 /*
8106 * Mark the bad layout state as invalid, then retry
8107 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008108 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008109 spin_unlock(&inode->i_lock);
8110 pnfs_free_lseg_list(&head);
8111 status = -EAGAIN;
8112 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008113 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008114
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008115 err = nfs4_handle_exception(server, nfs4err, exception);
8116 if (!status) {
8117 if (exception->retry)
8118 status = -EAGAIN;
8119 else
8120 status = err;
8121 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008122out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008123 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008124 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008125}
8126
Idan Kedar85541162012-08-02 11:47:10 +03008127static size_t max_response_pages(struct nfs_server *server)
8128{
8129 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8130 return nfs_page_array_len(0, max_resp_sz);
8131}
8132
8133static void nfs4_free_pages(struct page **pages, size_t size)
8134{
8135 int i;
8136
8137 if (!pages)
8138 return;
8139
8140 for (i = 0; i < size; i++) {
8141 if (!pages[i])
8142 break;
8143 __free_page(pages[i]);
8144 }
8145 kfree(pages);
8146}
8147
8148static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8149{
8150 struct page **pages;
8151 int i;
8152
8153 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8154 if (!pages) {
8155 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8156 return NULL;
8157 }
8158
8159 for (i = 0; i < size; i++) {
8160 pages[i] = alloc_page(gfp_flags);
8161 if (!pages[i]) {
8162 dprintk("%s: failed to allocate page\n", __func__);
8163 nfs4_free_pages(pages, size);
8164 return NULL;
8165 }
8166 }
8167
8168 return pages;
8169}
8170
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008171static void nfs4_layoutget_release(void *calldata)
8172{
8173 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008174 struct inode *inode = lgp->args.inode;
8175 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008176 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008177
8178 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008179 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008180 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008181 put_nfs_open_context(lgp->args.ctx);
8182 kfree(calldata);
8183 dprintk("<-- %s\n", __func__);
8184}
8185
8186static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8187 .rpc_call_prepare = nfs4_layoutget_prepare,
8188 .rpc_call_done = nfs4_layoutget_done,
8189 .rpc_release = nfs4_layoutget_release,
8190};
8191
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008192struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008193nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008194{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008195 struct inode *inode = lgp->args.inode;
8196 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008197 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008198 struct rpc_task *task;
8199 struct rpc_message msg = {
8200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8201 .rpc_argp = &lgp->args,
8202 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008203 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008204 };
8205 struct rpc_task_setup task_setup_data = {
8206 .rpc_client = server->client,
8207 .rpc_message = &msg,
8208 .callback_ops = &nfs4_layoutget_call_ops,
8209 .callback_data = lgp,
8210 .flags = RPC_TASK_ASYNC,
8211 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008212 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008213 struct nfs4_exception exception = {
8214 .inode = inode,
8215 .timeout = *timeout,
8216 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008217 int status = 0;
8218
8219 dprintk("--> %s\n", __func__);
8220
Peng Tao4bd5a982014-11-17 11:05:17 +08008221 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8222 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8223
Idan Kedar85541162012-08-02 11:47:10 +03008224 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8225 if (!lgp->args.layout.pages) {
8226 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008227 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008228 }
8229 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8230
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008231 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008232 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008233 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008234
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008235 task = rpc_run_task(&task_setup_data);
8236 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008237 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008238 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008239 if (status == 0) {
8240 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8241 *timeout = exception.timeout;
8242 }
8243
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008244 trace_nfs4_layoutget(lgp->args.ctx,
8245 &lgp->args.range,
8246 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008247 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008248 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008249
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008250 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8251 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008252 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008253 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008254 rpc_put_task(task);
8255 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008256 if (status)
8257 return ERR_PTR(status);
8258 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008259}
8260
Benny Halevycbe82602011-05-22 19:52:37 +03008261static void
8262nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8263{
8264 struct nfs4_layoutreturn *lrp = calldata;
8265
8266 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008267 nfs41_setup_sequence(lrp->clp->cl_session,
8268 &lrp->args.seq_args,
8269 &lrp->res.seq_res,
8270 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008271}
8272
8273static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8274{
8275 struct nfs4_layoutreturn *lrp = calldata;
8276 struct nfs_server *server;
8277
8278 dprintk("--> %s\n", __func__);
8279
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008280 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008281 return;
8282
8283 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008284 switch (task->tk_status) {
8285 default:
8286 task->tk_status = 0;
8287 case 0:
8288 break;
8289 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008290 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008291 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008292 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008293 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008294 return;
8295 }
Benny Halevycbe82602011-05-22 19:52:37 +03008296 dprintk("<-- %s\n", __func__);
8297}
8298
8299static void nfs4_layoutreturn_release(void *calldata)
8300{
8301 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008302 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008303 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008304
8305 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008306 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008307 if (lrp->res.lrs_present) {
8308 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8309 &lrp->args.range,
8310 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008311 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008312 } else
8313 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008314 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008315 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008316 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008317 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008318 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008319 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008320 kfree(calldata);
8321 dprintk("<-- %s\n", __func__);
8322}
8323
8324static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8325 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8326 .rpc_call_done = nfs4_layoutreturn_done,
8327 .rpc_release = nfs4_layoutreturn_release,
8328};
8329
Peng Tao6c166052014-11-17 09:30:40 +08008330int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008331{
8332 struct rpc_task *task;
8333 struct rpc_message msg = {
8334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8335 .rpc_argp = &lrp->args,
8336 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008337 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008338 };
8339 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008340 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008341 .rpc_message = &msg,
8342 .callback_ops = &nfs4_layoutreturn_call_ops,
8343 .callback_data = lrp,
8344 };
Peng Tao6c166052014-11-17 09:30:40 +08008345 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008346
Andrew Elble99ade3c2015-12-02 09:39:51 -05008347 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8348 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8349 &task_setup_data.rpc_client, &msg);
8350
Benny Halevycbe82602011-05-22 19:52:37 +03008351 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008352 if (!sync) {
8353 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8354 if (!lrp->inode) {
8355 nfs4_layoutreturn_release(lrp);
8356 return -EAGAIN;
8357 }
8358 task_setup_data.flags |= RPC_TASK_ASYNC;
8359 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008360 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008361 task = rpc_run_task(&task_setup_data);
8362 if (IS_ERR(task))
8363 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008364 if (sync)
8365 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008366 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008367 dprintk("<-- %s status=%d\n", __func__, status);
8368 rpc_put_task(task);
8369 return status;
8370}
8371
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008372static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008373_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8374 struct pnfs_device *pdev,
8375 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008376{
8377 struct nfs4_getdeviceinfo_args args = {
8378 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008379 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8380 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008381 };
8382 struct nfs4_getdeviceinfo_res res = {
8383 .pdev = pdev,
8384 };
8385 struct rpc_message msg = {
8386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8387 .rpc_argp = &args,
8388 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008389 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008390 };
8391 int status;
8392
8393 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008394 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008395 if (res.notification & ~args.notify_types)
8396 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008397 if (res.notification != args.notify_types)
8398 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008399
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008400 dprintk("<-- %s status=%d\n", __func__, status);
8401
8402 return status;
8403}
8404
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008405int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8406 struct pnfs_device *pdev,
8407 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008408{
8409 struct nfs4_exception exception = { };
8410 int err;
8411
8412 do {
8413 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008414 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008415 &exception);
8416 } while (exception.retry);
8417 return err;
8418}
8419EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8420
Andy Adamson863a3c62011-03-23 13:27:54 +00008421static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8422{
8423 struct nfs4_layoutcommit_data *data = calldata;
8424 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008425 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008426
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008427 nfs41_setup_sequence(session,
8428 &data->args.seq_args,
8429 &data->res.seq_res,
8430 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008431}
8432
8433static void
8434nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8435{
8436 struct nfs4_layoutcommit_data *data = calldata;
8437 struct nfs_server *server = NFS_SERVER(data->args.inode);
8438
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008439 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008440 return;
8441
8442 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008443 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8444 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8445 case -NFS4ERR_BADLAYOUT: /* no layout */
8446 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008447 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008448 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008449 break;
8450 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008451 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008452 rpc_restart_call_prepare(task);
8453 return;
8454 }
8455 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008456}
8457
8458static void nfs4_layoutcommit_release(void *calldata)
8459{
8460 struct nfs4_layoutcommit_data *data = calldata;
8461
Andy Adamsondb29c082011-07-30 20:52:38 -04008462 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008463 nfs_post_op_update_inode_force_wcc(data->args.inode,
8464 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008465 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008466 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008467 kfree(data);
8468}
8469
8470static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8471 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8472 .rpc_call_done = nfs4_layoutcommit_done,
8473 .rpc_release = nfs4_layoutcommit_release,
8474};
8475
8476int
Andy Adamsonef311532011-03-12 02:58:10 -05008477nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008478{
8479 struct rpc_message msg = {
8480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8481 .rpc_argp = &data->args,
8482 .rpc_resp = &data->res,
8483 .rpc_cred = data->cred,
8484 };
8485 struct rpc_task_setup task_setup_data = {
8486 .task = &data->task,
8487 .rpc_client = NFS_CLIENT(data->args.inode),
8488 .rpc_message = &msg,
8489 .callback_ops = &nfs4_layoutcommit_ops,
8490 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008491 };
8492 struct rpc_task *task;
8493 int status = 0;
8494
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008495 dprintk("NFS: initiating layoutcommit call. sync %d "
8496 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008497 data->args.lastbytewritten,
8498 data->args.inode->i_ino);
8499
Trond Myklebust472e2592015-02-05 16:50:30 -05008500 if (!sync) {
8501 data->inode = nfs_igrab_and_active(data->args.inode);
8502 if (data->inode == NULL) {
8503 nfs4_layoutcommit_release(data);
8504 return -EAGAIN;
8505 }
8506 task_setup_data.flags = RPC_TASK_ASYNC;
8507 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008508 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008509 task = rpc_run_task(&task_setup_data);
8510 if (IS_ERR(task))
8511 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008512 if (sync)
8513 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008514 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008515 dprintk("%s: status %d\n", __func__, status);
8516 rpc_put_task(task);
8517 return status;
8518}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008519
Andy Adamson97431202013-08-08 10:57:56 -04008520/**
8521 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8522 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8523 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008524static int
8525_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008526 struct nfs_fsinfo *info,
8527 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008528{
8529 struct nfs41_secinfo_no_name_args args = {
8530 .style = SECINFO_STYLE_CURRENT_FH,
8531 };
8532 struct nfs4_secinfo_res res = {
8533 .flavors = flavors,
8534 };
8535 struct rpc_message msg = {
8536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8537 .rpc_argp = &args,
8538 .rpc_resp = &res,
8539 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008540 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008541 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008542 int status;
8543
8544 if (use_integrity) {
8545 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008546 cred = nfs4_get_clid_cred(server->nfs_client);
8547 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008548 }
8549
8550 dprintk("--> %s\n", __func__);
8551 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8552 &res.seq_res, 0);
8553 dprintk("<-- %s status=%d\n", __func__, status);
8554
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008555 if (cred)
8556 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008557
8558 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008559}
8560
8561static int
8562nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8563 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8564{
8565 struct nfs4_exception exception = { };
8566 int err;
8567 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008568 /* first try using integrity protection */
8569 err = -NFS4ERR_WRONGSEC;
8570
8571 /* try to use integrity protection with machine cred */
8572 if (_nfs4_is_integrity_protected(server->nfs_client))
8573 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8574 flavors, true);
8575
8576 /*
8577 * if unable to use integrity protection, or SECINFO with
8578 * integrity protection returns NFS4ERR_WRONGSEC (which is
8579 * disallowed by spec, but exists in deployed servers) use
8580 * the current filesystem's rpc_client and the user cred.
8581 */
8582 if (err == -NFS4ERR_WRONGSEC)
8583 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8584 flavors, false);
8585
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008586 switch (err) {
8587 case 0:
8588 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008589 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008590 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008591 default:
8592 err = nfs4_handle_exception(server, err, &exception);
8593 }
8594 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008595out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008596 return err;
8597}
8598
8599static int
8600nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8601 struct nfs_fsinfo *info)
8602{
8603 int err;
8604 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008605 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008606 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008607 struct nfs4_secinfo4 *secinfo;
8608 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008609
8610 page = alloc_page(GFP_KERNEL);
8611 if (!page) {
8612 err = -ENOMEM;
8613 goto out;
8614 }
8615
8616 flavors = page_address(page);
8617 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8618
8619 /*
8620 * Fall back on "guess and check" method if
8621 * the server doesn't support SECINFO_NO_NAME
8622 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008623 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008624 err = nfs4_find_root_sec(server, fhandle, info);
8625 goto out_freepage;
8626 }
8627 if (err)
8628 goto out_freepage;
8629
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008630 for (i = 0; i < flavors->num_flavors; i++) {
8631 secinfo = &flavors->flavors[i];
8632
8633 switch (secinfo->flavor) {
8634 case RPC_AUTH_NULL:
8635 case RPC_AUTH_UNIX:
8636 case RPC_AUTH_GSS:
8637 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8638 &secinfo->flavor_info);
8639 break;
8640 default:
8641 flavor = RPC_AUTH_MAXFLAVOR;
8642 break;
8643 }
8644
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008645 if (!nfs_auth_info_match(&server->auth_info, flavor))
8646 flavor = RPC_AUTH_MAXFLAVOR;
8647
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008648 if (flavor != RPC_AUTH_MAXFLAVOR) {
8649 err = nfs4_lookup_root_sec(server, fhandle,
8650 info, flavor);
8651 if (!err)
8652 break;
8653 }
8654 }
8655
8656 if (flavor == RPC_AUTH_MAXFLAVOR)
8657 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008658
8659out_freepage:
8660 put_page(page);
8661 if (err == -EACCES)
8662 return -EPERM;
8663out:
8664 return err;
8665}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008666
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008667static int _nfs41_test_stateid(struct nfs_server *server,
8668 nfs4_stateid *stateid,
8669 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008670{
8671 int status;
8672 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008673 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008674 };
8675 struct nfs41_test_stateid_res res;
8676 struct rpc_message msg = {
8677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8678 .rpc_argp = &args,
8679 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008680 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008681 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008682 struct rpc_clnt *rpc_client = server->client;
8683
8684 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8685 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008686
Chuck Lever38527b12012-07-11 16:30:23 -04008687 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008688 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008689 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008690 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008691 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008692 if (status != NFS_OK) {
8693 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008694 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008695 }
8696 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008697 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008698}
8699
Chuck Lever38527b12012-07-11 16:30:23 -04008700/**
8701 * nfs41_test_stateid - perform a TEST_STATEID operation
8702 *
8703 * @server: server / transport on which to perform the operation
8704 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008705 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008706 *
8707 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8708 * Otherwise a negative NFS4ERR value is returned if the operation
8709 * failed or the state ID is not currently valid.
8710 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008711static int nfs41_test_stateid(struct nfs_server *server,
8712 nfs4_stateid *stateid,
8713 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008714{
8715 struct nfs4_exception exception = { };
8716 int err;
8717 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008718 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008719 if (err != -NFS4ERR_DELAY)
8720 break;
8721 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008722 } while (exception.retry);
8723 return err;
8724}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008725
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008726struct nfs_free_stateid_data {
8727 struct nfs_server *server;
8728 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008729 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008730};
8731
8732static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8733{
8734 struct nfs_free_stateid_data *data = calldata;
8735 nfs41_setup_sequence(nfs4_get_session(data->server),
8736 &data->args.seq_args,
8737 &data->res.seq_res,
8738 task);
8739}
8740
8741static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8742{
8743 struct nfs_free_stateid_data *data = calldata;
8744
8745 nfs41_sequence_done(task, &data->res.seq_res);
8746
8747 switch (task->tk_status) {
8748 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008749 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008750 rpc_restart_call_prepare(task);
8751 }
8752}
8753
8754static void nfs41_free_stateid_release(void *calldata)
8755{
8756 kfree(calldata);
8757}
8758
Trond Myklebust17f26b12013-08-21 15:48:42 -04008759static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008760 .rpc_call_prepare = nfs41_free_stateid_prepare,
8761 .rpc_call_done = nfs41_free_stateid_done,
8762 .rpc_release = nfs41_free_stateid_release,
8763};
8764
8765static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8766 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008767 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008768 bool privileged)
8769{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008770 struct rpc_message msg = {
8771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008772 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008773 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008774 struct rpc_task_setup task_setup = {
8775 .rpc_client = server->client,
8776 .rpc_message = &msg,
8777 .callback_ops = &nfs41_free_stateid_ops,
8778 .flags = RPC_TASK_ASYNC,
8779 };
8780 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008781
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008782 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8783 &task_setup.rpc_client, &msg);
8784
Chuck Lever38527b12012-07-11 16:30:23 -04008785 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008786 data = kmalloc(sizeof(*data), GFP_NOFS);
8787 if (!data)
8788 return ERR_PTR(-ENOMEM);
8789 data->server = server;
8790 nfs4_stateid_copy(&data->args.stateid, stateid);
8791
8792 task_setup.callback_data = data;
8793
8794 msg.rpc_argp = &data->args;
8795 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008796 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008797 if (privileged)
8798 nfs4_set_sequence_privileged(&data->args.seq_args);
8799
8800 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008801}
8802
Chuck Lever38527b12012-07-11 16:30:23 -04008803/**
8804 * nfs41_free_stateid - perform a FREE_STATEID operation
8805 *
8806 * @server: server / transport on which to perform the operation
8807 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008808 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008809 *
8810 * Returns NFS_OK if the server freed "stateid". Otherwise a
8811 * negative NFS4ERR value is returned.
8812 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008813static int nfs41_free_stateid(struct nfs_server *server,
8814 nfs4_stateid *stateid,
8815 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008816{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008817 struct rpc_task *task;
8818 int ret;
8819
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008820 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008821 if (IS_ERR(task))
8822 return PTR_ERR(task);
8823 ret = rpc_wait_for_completion_task(task);
8824 if (!ret)
8825 ret = task->tk_status;
8826 rpc_put_task(task);
8827 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008828}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008829
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008830static void
8831nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008832{
8833 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008834 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008835
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008836 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008837 nfs4_free_lock_state(server, lsp);
8838 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008839 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008840 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008841}
8842
Trond Myklebust36281ca2012-03-04 18:13:56 -05008843static bool nfs41_match_stateid(const nfs4_stateid *s1,
8844 const nfs4_stateid *s2)
8845{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008846 if (s1->type != s2->type)
8847 return false;
8848
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008849 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008850 return false;
8851
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008852 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008853 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008854 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008855 return true;
8856
8857 return false;
8858}
8859
Andy Adamson557134a2009-04-01 09:21:53 -04008860#endif /* CONFIG_NFS_V4_1 */
8861
Trond Myklebust36281ca2012-03-04 18:13:56 -05008862static bool nfs4_match_stateid(const nfs4_stateid *s1,
8863 const nfs4_stateid *s2)
8864{
Trond Myklebustf597c532012-03-04 18:13:56 -05008865 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008866}
8867
8868
Trond Myklebust17280172012-03-11 13:11:00 -04008869static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008870 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008871 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 .recover_open = nfs4_open_reclaim,
8873 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008874 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008875 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876};
8877
Andy Adamson591d71c2009-04-01 09:22:47 -04008878#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008879static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008880 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8881 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8882 .recover_open = nfs4_open_reclaim,
8883 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008884 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008885 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008886 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008887};
8888#endif /* CONFIG_NFS_V4_1 */
8889
Trond Myklebust17280172012-03-11 13:11:00 -04008890static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008891 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008892 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008893 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008895 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896};
8897
Andy Adamson591d71c2009-04-01 09:22:47 -04008898#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008899static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008900 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8901 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008902 .recover_open = nfs41_open_expired,
8903 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008904 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008905};
8906#endif /* CONFIG_NFS_V4_1 */
8907
Trond Myklebust17280172012-03-11 13:11:00 -04008908static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008909 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008910 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008911 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008912};
8913
8914#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008915static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008916 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008917 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008918 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008919};
8920#endif
8921
Chuck Leverec011fe2013-10-17 14:12:39 -04008922static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008923 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008924 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008925};
8926
8927#if defined(CONFIG_NFS_V4_1)
8928static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008929 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008930 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008931};
8932#endif /* CONFIG_NFS_V4_1 */
8933
Trond Myklebust97dc1352010-06-16 09:52:26 -04008934static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8935 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008936 .init_caps = NFS_CAP_READDIRPLUS
8937 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008938 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008939 .init_client = nfs40_init_client,
8940 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008941 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008942 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008943 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008944 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008945 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008946 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8947 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8948 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008949 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008950};
8951
8952#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008953static struct nfs_seqid *
8954nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8955{
8956 return NULL;
8957}
8958
Trond Myklebust97dc1352010-06-16 09:52:26 -04008959static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8960 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008961 .init_caps = NFS_CAP_READDIRPLUS
8962 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008963 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008964 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008965 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008966 .init_client = nfs41_init_client,
8967 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008968 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008969 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008970 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008971 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008972 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04008973 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008974 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8975 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8976 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008977 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008978};
8979#endif
8980
Steve Dickson42c2c422013-05-22 12:50:38 -04008981#if defined(CONFIG_NFS_V4_2)
8982static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8983 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008984 .init_caps = NFS_CAP_READDIRPLUS
8985 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008986 | NFS_CAP_POSIX_LOCK
8987 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008988 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008989 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008990 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008991 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008992 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008993 | NFS_CAP_LAYOUTSTATS
8994 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008995 .init_client = nfs41_init_client,
8996 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008997 .match_stateid = nfs41_match_stateid,
8998 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008999 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009000 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009001 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009002 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009003 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9004 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9005 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009006 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009007};
9008#endif
9009
Trond Myklebust97dc1352010-06-16 09:52:26 -04009010const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9011 [0] = &nfs_v4_0_minor_ops,
9012#if defined(CONFIG_NFS_V4_1)
9013 [1] = &nfs_v4_1_minor_ops,
9014#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009015#if defined(CONFIG_NFS_V4_2)
9016 [2] = &nfs_v4_2_minor_ops,
9017#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009018};
9019
Trond Myklebust13997822016-07-24 17:10:52 -04009020static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009021{
9022 ssize_t error, error2;
9023
9024 error = generic_listxattr(dentry, list, size);
9025 if (error < 0)
9026 return error;
9027 if (list) {
9028 list += error;
9029 size -= error;
9030 }
9031
9032 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9033 if (error2 < 0)
9034 return error2;
9035 return error + error2;
9036}
9037
Trond Myklebust17f26b12013-08-21 15:48:42 -04009038static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009039 .create = nfs_create,
9040 .lookup = nfs_lookup,
9041 .atomic_open = nfs_atomic_open,
9042 .link = nfs_link,
9043 .unlink = nfs_unlink,
9044 .symlink = nfs_symlink,
9045 .mkdir = nfs_mkdir,
9046 .rmdir = nfs_rmdir,
9047 .mknod = nfs_mknod,
9048 .rename = nfs_rename,
9049 .permission = nfs_permission,
9050 .getattr = nfs_getattr,
9051 .setattr = nfs_setattr,
9052 .getxattr = generic_getxattr,
9053 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009054 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009055 .removexattr = generic_removexattr,
9056};
9057
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009058static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009059 .permission = nfs_permission,
9060 .getattr = nfs_getattr,
9061 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009062 .getxattr = generic_getxattr,
9063 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009064 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009065 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009066};
9067
David Howells509de812006-08-22 20:06:11 -04009068const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069 .version = 4, /* protocol version */
9070 .dentry_ops = &nfs4_dentry_operations,
9071 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009072 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009073 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009074 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009075 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009076 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077 .getattr = nfs4_proc_getattr,
9078 .setattr = nfs4_proc_setattr,
9079 .lookup = nfs4_proc_lookup,
9080 .access = nfs4_proc_access,
9081 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 .create = nfs4_proc_create,
9083 .remove = nfs4_proc_remove,
9084 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009085 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009086 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009087 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009088 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009089 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090 .link = nfs4_proc_link,
9091 .symlink = nfs4_proc_symlink,
9092 .mkdir = nfs4_proc_mkdir,
9093 .rmdir = nfs4_proc_remove,
9094 .readdir = nfs4_proc_readdir,
9095 .mknod = nfs4_proc_mknod,
9096 .statfs = nfs4_proc_statfs,
9097 .fsinfo = nfs4_proc_fsinfo,
9098 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009099 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009101 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009103 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009105 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009107 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009108 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009110 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009111 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009112 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009113 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009114 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009115 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009116 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009117 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009118 .create_server = nfs4_create_server,
9119 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120};
9121
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009122static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009123 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009124 .list = nfs4_xattr_list_nfs4_acl,
9125 .get = nfs4_xattr_get_nfs4_acl,
9126 .set = nfs4_xattr_set_nfs4_acl,
9127};
9128
9129const struct xattr_handler *nfs4_xattr_handlers[] = {
9130 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009131#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9132 &nfs4_xattr_nfs4_label_handler,
9133#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009134 NULL
9135};
9136
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137/*
9138 * Local variables:
9139 * c-basic-offset: 8
9140 * End:
9141 */